Unmanaged Code again

  • Thread starter Thread starter juergen
  • Start date Start date
J

juergen

hello,

i want to use a c dll in c#.
it is only one method but it doesn't work.

this is the c method in the dll:

int vciMulticastSearch(int iColorNumber, int maxRequests, PMCAST_DATA pData, int maxData,
char *pszIpLan,char *pszIpWlan);

PMCAST_DATA is a structure pointer:

typedef struct
{
int iColor;
int iState;
int iMedium;
int isLAN;
int ipMask;
int iModeWLan;
char szSN[16];
char szMacLan[18];
char szMacWLan[18];
char szIP[16];
char szTester[32];
char szApplication[32];
char szText[32];
int iMediumTester;
} MCAST_DATA, *PMCAST_DATA;

i tried this c# method:

[DllImport("my.dll", ExactSpelling=true)]
public static extern int vciMulticastSearch(int iColorNumber, int maxRequests, MCAST_DATA [] pData,
int maxData, [MarshalAs(UnmanagedType.LPStr)] string pszIpLan,
[MarshalAs(UnmanagedType.LPStr)] string pszIpWlan);

i defined the struct in c# too:

public struct MCAST_DATA
{
.....
public int iModeWLan;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)] public string szSN];
.....
}


but the problem is that i need to initialize a array of that struct in c# like that:
MCAST_DATA [] data = new MCAST_DATA[10];

and then get the pointer of that array and give it to the vciMulticastSearch method.
how can i do that??

i tried the intptr of the first element in the array but the array never gets filled.

please help.

bye bembi
 
juergen,

If you are going to have the function populate the array, then you need
to pass the pData parameter as an IntPtr, and assign the memory block to
that parameter yourself. You will also have to do the marshaling to/from
the method yourself, calling the PtrToStructure method to populate the
array.


Hope this helps.
 
thanks for the reply.
i tried this:

IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(MCAST_DATA)));
int result = vciMulticastSearch(255, 1, ptr, 10, "", "");
MCAST_DATA data = (MCAST_DATA) Marshal.PtrToStructure(ptr,
typeof(MCAST_DATA));

But i get an exception at the last line:
object reference not set to an instance of an object.

what am i doing wrong?

bye bembi
 
Back
Top