SOLVED: need to call managed code from unmanaged c++ code !



Hi,

I managed to resolve the problems I've been facing so I thought I'd post it
here in case someone else runs into them.

1. I was using a class defintion - this is wrong (come to think about it)..
it should have been declared a Module [ as a result, the private members were
inaccessible and hece my "access violations". Decalring it as a module caused
the private members to be declared during the DLL Load and accessible
thence.] :)

2. the signature "int AppInitFn(int argc, char** argv)" should be
implemented as follows : Public Function(ByVal argc as Int32,
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPStr,
SizeParamIndex:=0)> ByVal argv() as string) as Int32

I was so sure that the "char** argv" passed through traditional C/C++ is a
string array but the "Access Violation" because of 1 kept throwing me off :)

I tried the "Public Function(ByVal argc as Int32, ByVal/byRef argv as
IntPtr) as Int32" but eventhough I got the value of argc as "3", i could only
marshal the first parameter through the Marshal.PtrToANSI call....

So the above defintion tell the Interop to marshal char** argv as a array of
LongPointers (LPArray) with subtype as LPStr (null terminated string) and use
the first parameter "argc" to define the size of this array... Phew.

Hope this helps.
--
Ravi Shankar


"Ravi Shankar" wrote:

Hi,

I have a requirement where I need to call methods written in VB.Net from a
C++ code. I have been going crazy reading various articles on the net w.r.t
interop and marshalling but an more confused now that before.

the signature I need to conform in the VB.Net DLL is
int MyAppInitFunc(int argc, char** argv)

To this extent I have tried the following
Public Function MyAppInitFunc(ByVal argc as integer, ByRef argv as string)
as Integer
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv() as string)
as Int32

... based on some posts I even tried
Public Function MyAppInitFunc(ByVal argc as Int32, ByVal argv as IntPtr) as
Int32

but have not been able to get anything to work.

Is there perhaps some reading I'm missing up on ?

Regards.

--
Ravi Shankar
.



Relevant Pages