Providing C # struct for COM breaks for VB6 application

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.

+4
source share
2 answers

I think I found a solution to this problem. I had the same problem, vb6 breaks when calling the interop library method by passing the structure. This is the project that I created to test the interaction of the DLL, so everything I had in my project was a form. But I had another project (main application) with the same link, and it works fine.

After reading Joel's post, I wanted to check its solution, and in fact id really worked (using the class instead of the structure). But I have other interops where I use frameworks, so I was very worried that my application could fail at any time. In addition, I did not want to do the extra work of creating and exporting an interface and class to replace the structure.

So, I took the code from my form and moved it to the general module in the module. He worked immediately. By the way, as I made the call in the main application, which worked fine.

I hope this can help others.

+2
source

Is there any magic for struct to COM to use in VB6?

The MSDN COM * Data Types article states that structures are supported. In particular, the MSDN article states that COM structures are defined as:

 ByRef VALUETYPE< MyStruct > 

There are also several articles on setting up your COM-called wrappers at the bottom of the page, you can view them.

  • Edit (2016): The original link was broken, so I fixed it to version 3.5.Net Framework.
+1
source

All Articles