Problems with Interop in C#

G

Guest

Subject: Problems with Interop in C#

We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
Net2 using Vs2005. Below are the signatures of the method that is the
problem. It is the last argument (e.g., "out obj" in C#) that is returned
corrupted. It should be an array of integers. In one simple test App this
argument does return an array of integers reliably. In the actual App it
returns one or two integer arrays but then it starts returning corrupted
data. The difference between the two App's is on of complexity and that the
actual App uses many more threads then the test App.

We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
file.

ANY suggestions would be GREATLY appreciated.

C# signature--
bool b=pdc.Get_ArrayData(out Transfer_Complete,out DataNum,out
SamplingPeriod,out obj);

IDL signature--
[id(0x00000012), helpstring("method Get_ArrayData")]
VARIANT_BOOL Get_ArrayData(
[out] long* Transfer_Complete,
[out] long* DataNum,
[out] double* SamplingPeriod,
[out] VARIANT* arrayData);
 
W

Willy Denoyette [MVP]

| Subject: Problems with Interop in C#
|
| We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
| Net2 using Vs2005. Below are the signatures of the method that is the
| problem. It is the last argument (e.g., "out obj" in C#) that is returned
| corrupted. It should be an array of integers. In one simple test App this
| argument does return an array of integers reliably. In the actual App it
| returns one or two integer arrays but then it starts returning corrupted
| data. The difference between the two App's is on of complexity and that
the
| actual App uses many more threads then the test App.
|
| We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
| file.
|
| ANY suggestions would be GREATLY appreciated.
|
| C# signature--
| bool b=pdc.Get_ArrayData(out Transfer_Complete,out DataNum,out
| SamplingPeriod,out obj);
|
| IDL signature--
| [id(0x00000012), helpstring("method Get_ArrayData")]
| VARIANT_BOOL Get_ArrayData(
| [out] long* Transfer_Complete,
| [out] long* DataNum,
| [out] double* SamplingPeriod,
| [out] VARIANT* arrayData);
|
| --
| John Olbert
|

Make sure your threads are initialized for STA.

Willy.
 
G

Guest

as far as im aware you cannot use arrays by reference across the interop
boundary
 
W

Willy Denoyette [MVP]

| as far as im aware you cannot use arrays by reference across the interop
| boundary
|
| --
| "If ignorance is bliss, then wipe the smile from my face."
|


Sure you can, the OP's case is a bit tricky as he is passing a VARIANT*, so
what you need is to pass an object reference (effectively a VARIANT*).

Consider following sample...

' SomeAXClass in VB6

Function GetArray(o As Variant) As Boolean
' no bound checks in this sample!!!!!
o(0) = 100
o(1) = 200
End Function

// C#...
....
SomeAXClass o = new SomeAXClass();
object arr = new object[2];
o.GetArray(ref arr);
foreach(object i in arr as object[])
Console.WriteLine(i.ToString());
...

The only problem I have with the OP's code is that I don't believe this is a
VB6 AX dll, see my reply to his posting for details on why.


Willy.
 
W

Willy Denoyette [MVP]

| Subject: Problems with Interop in C#
|
| We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
| Net2 using Vs2005. Below are the signatures of the method that is the
| problem. It is the last argument (e.g., "out obj" in C#) that is returned
| corrupted. It should be an array of integers. In one simple test App this
| argument does return an array of integers reliably. In the actual App it
| returns one or two integer arrays but then it starts returning corrupted
| data. The difference between the two App's is on of complexity and that
the
| actual App uses many more threads then the test App.
|
| We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
| file.
|
| ANY suggestions would be GREATLY appreciated.
|
| C# signature--
| bool b=pdc.Get_ArrayData(out Transfer_Complete,out DataNum,out
| SamplingPeriod,out obj);
|
| IDL signature--
| [id(0x00000012), helpstring("method Get_ArrayData")]
| VARIANT_BOOL Get_ArrayData(
| [out] long* Transfer_Complete,
| [out] long* DataNum,
| [out] double* SamplingPeriod,
| [out] VARIANT* arrayData);
|
| --
| John Olbert
|

Are you sure this (the above IDL) is from a VB6 AXCOM dll?, AFAIK VB6 can't
pass an argumant as out, it passes arguments as [In, Out] or [In]. Returns
are passed as [out, retval] and the IDL signature doesn't seem right for
VB6, it should return an HRESULT
If i was a VB6 AX object it's methods signature should look something like
this:

HRESULT GET_....([In, Out] long* ...., [in, out] VARIANT*, [in, retval]
VARIANT_BOOL*);


Willy.
 
G

Guest

All we have is the raw Dll. Is there a way to find out which compiler was used?

Thanks for the idea.
 
W

Willy Denoyette [MVP]

| All we have is the raw Dll. Is there a way to find out which compiler was
used?
|

Not really, all you can do to make sure it's a VB6 dll is run dumpbin.exe
and check the imports, a VB6 PE must import the VB runtime (MSVBVM60.dll).
PE files produced by other tools will not import the VB runtime, but you
won't be able to tell what language compiler was used to build the file.

Willy.
 
G

Guest

Off hand how do you make sure that one's .NET2 threads initialized for STA?

Thanks.
 
W

Willy Denoyette [MVP]

| Off hand how do you make sure that one's .NET2 threads initialized for
STA?
|
| Thanks.
| --
| John Olbert
|
|

When it's your main thread, you can apply the [STAThread] attribute to your
Main function.
When it's an auxiliary thread, initialize the thread instance as STA, using
the SetApartentState method before it is started.

Worker threads picked from the thread pool are MTA and cannot be changed.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top