Specified cast is not valid errors

Hi all

Sometimes, but not always, I'm finding that the Layer.Add, Cell.GlueTo and Window.Select methods throw errors similar to:

Specified cast is not valid at System.StubHelpers.InterfaceMarshaler.ConvertToNative(Object objSrc, IntPtr itfMT, IntPtr classMT, Int32 flags) at Microsoft.Office.Interop.Visio.IVLayer.Add(Shape SheetObject, Int16 fPresMems)

I'm using VB2010, Visio 2007 and currently testing on Windows XP 32 bit. The program is a stand-alone .exe program using Visio via .NET interop. The same program, with the same data can run several times without problem and then once this error has been thrown, the same error will be thrown every time until the Visio file is closed and re-opened.

This VB2010 code was derived from a VB6 program which has been working fine for 2,000+ users over a period of about 15 years.

Unfortunately I've not been able to replicate these problems in a small test program which is making the debugging process rather tedious and it's another aspect of this problem which is worrying and confusing me.

The program is adding a 100 or so shapes from a stencil and gluing lines between them. I've tried playing with the settings of such properties as DeferRecalc, EventsEnabled, InhibitSelectChange, LiveDynamics, ShowChanges and UndoEnabled as I thought Visio might be having a problem updating the screen or allocating memory to the undo manager or something. I've also tried Application.DoEvents.

I've re-written some of my code to avoid using Window.Select which I was using to select shapes to create groups. I guess I could also re-write the code to avoid using GlueTo, but I don't think I can always avoid using Layer.Add.

Any thoughts or comments would be much appreciated.

Best regards

Roger Billsdon

August 13th, 2010 1:23am

Could you post some of the code, Roger?

 

Free Windows Admin Tool Kit Click here and download it now
August 13th, 2010 5:27pm

Hi Paul

Many thanks for your reply and I hope all is well with you.

I spent quite a lot of time trying to replicate the problem in a small test program created from scratch. All the offending methods (Window.Select, Layer.Add and Cell.GlueTo) worked fine in the test program, as indeed they often do in my main program. I carefully compared all the project references and everything else I could think of in both the test and my main program. I added loops in the test program so that it added many shapes, etc in an attempt to create some stress on the Visio UI, Undo manager, etc. Not being able to replicate the problem in a small test program is one aspect of this problem that is worrying and confusing me.

The main program is quite large (I guess about 60,000 lines of code) and contains code for many other functions. An alternative approach would be for me to take a copy of the main program and try chopping chunks out to degenerate it into a small test program. However, at present I have a looming project deadline so I'm now putting my effort into recoding to eliminate the Window.Select, Cell.GlueTo and, where possible, Layer.Add method calls. Fortunately Visio provides alternatives which seem to be working. For example, rather than Cell.GlueTo I'm setting the relevant cell formulae and instead of Window.Select to form group, I'm creating a shape, converting it into a group and then dropping the other shapes into this group. Back in my VB6 programs I had some problems with select and group so I think this conversion is worthwhile, but I don't recall having had any problems with Cell.GlueTo or Layer.Add. Because I don't understand what's causing the problem I'm concerned that this re-coding work is just fixing the symptoms without actually getting at the root cause.

In general I am finding rather more problems and performance issues with VB.NET Visio programs than I did over all the years I used VB3 and VB6 to program Visio.

Best regards

Roger

August 13th, 2010 7:12pm

A comment from the side having just gone through the "inappropriate" during applying a shape to a layer. It turned out (it appears) that visio was comparing the page associated with shape object to the page associated with the layers object and came to the conclusion (correctly) that the two pages were different. Both pages had the same layers names. The root problem was I should not have been using activepage to intialize the class, but rather the drawingpage I was creating.

My experience is the same, VB6 performance outruns VB.NET

al

 

Free Windows Admin Tool Kit Click here and download it now
August 14th, 2010 4:34pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
August 16th, 2010 12:05pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
Free Windows Admin Tool Kit Click here and download it now
August 16th, 2010 12:05pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
August 16th, 2010 12:05pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
Free Windows Admin Tool Kit Click here and download it now
August 16th, 2010 12:05pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
August 16th, 2010 12:05pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
Free Windows Admin Tool Kit Click here and download it now
August 16th, 2010 12:05pm

Hi Al

Many thanks for that idea. It's an interesting one and I thought it might have an impact in my program but, having searched through my code and done a few experiments, it doesn't seem to be causing this particular problem. Your idea also made me wonder if passing Visio page objects to methods as ByRef or ByVal parameters made any difference but, as would be expected, a few experiments seems to confirm it doesn't.

I'm continuing with re-coding to avoid the troublesome Cell.GlueTo, Window.Select and Layer.Add methods but I've still got an uneasy feeling this isn't getting at the root cause and that even after this work the same problem might pop up somewhere else.

Best regards

Roger

August 16th, 2010 1:07pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
Free Windows Admin Tool Kit Click here and download it now
August 16th, 2010 3:05pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
August 16th, 2010 3:05pm

Roger,

I also went into the how I passed page objects and have since concluded that (IMHO) that it did not make sense to me to pass a shape and a page in the same call since the shape object has a containedpage operand that points to the page that needs attention (cuts out the need for passing the page).

Good luck,

al

  • Marked as answer by Sally Tang Friday, August 20, 2010 7:05 AM
  • Unmarked as answer by Roger999 Wednesday, August 25, 2010 10:54 AM
Free Windows Admin Tool Kit Click here and download it now
August 16th, 2010 3:05pm

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
August 26th, 2010 11:27am

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2010 11:27am

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
August 26th, 2010 11:27am

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2010 11:27am

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
August 26th, 2010 11:27am

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2010 11:27am

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
August 26th, 2010 2:27pm

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2010 2:27pm

Hi all


It turned out that this problem was being caused by Redgate's Smart Assembly obfuscator. They're working on a fix which they expect to have available in September. Unfortunately this was too late for my project deadline so I decided to change to LogicNP Software's Crypto Obfuscator which seems a better choice for my requirements and their support is excellent.


Best regards
Roger

  • Marked as answer by Roger999 Thursday, August 26, 2010 11:28 AM
August 26th, 2010 2:27pm

Thanks for this. Turns out the same issue for me as well with Obfuscated code. Exempting the relevant types dealing with the interface fixed the problem. 
Free Windows Admin Tool Kit Click here and download it now
February 14th, 2015 11:31pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics