Last Updated: 2009-08-11 2:30 PM EDT
A few days ago I posted this question about some very strange problems. Well, I understood what specifically makes it build on one machine so as not to work on others, and even came up with a workaround, but now this leaves me with a pleasant, specific question: why?
To reproduce the problem, I create a new InteropUserControl and do the following:
- Add a new
public struct MyStruct : - Give GUID and
ComVisible - Add the
GetMyStruct member to the GetMyStruct interface and implement it in InteropUserControl .
MyStruct :
[Guid("49E803EC-BED9-4a08-B42B-E0499864A169")] [ComVisible(true)] public struct MyStruct { public int mynumber; }
_InteropUserControl.GetMyStruct() :
[DispId(7)] void getMyStruct( int num, ref MyStruct data );
(I tried returning MyStruct instead of passing by reference.)
InteropUserControl.GetMyStruct() implementation:
public void getMyStruct( int num, ref MyStruct data ) { data = new MyStruct(); data.mynumber = num * 2; }
I also sign the assembly and install it in the GAC and register with Regasm. After adding it to a new VB6 project and adding a call to GetMyStruct() and compiling it on our build machine, it refuses to work on other machines.
To get around this, I had to set the class for COM instead of a structure and basically change GetMyStruct to this:
public void GetMyData( int num, MyClass data ) { data.mynumber = num * 2; }
In my actual project, I retrieve the structure inside, and then copy all the field values โโfrom the structure to the corresponding members in the class instance passed to the client method.
So why did the structure cause this behavior and the class worked fine? Is there any magic to host the structure for COM for use in VB6?
I think this may have something to do with OLE Automation.
Note. I also tried to return the structure, and not use the ref parameter, but that did not change the behavior.
Edit to add a link to the project template:
Interop Forms Toolkit 2.0 is the source template for the VB.NET project and dll. I am not referring to the dll, so you do not need to install this.
C # Template translations in CodeProject is what I used to create mine (project template, not element template). The VB.NET version automatically generates the __InteropUserControl event __InteropUserControl , the _InteropUserControl interface _InteropUserControl and several associated attributes. They are explicitly encoded in the C # version, and what about everything that differs between them.