wrapper for unmanaged c++ DLL

  • Thread starter Thread starter Chris Bruckner
  • Start date Start date
C

Chris Bruckner

Hi,

I have trouble with a C# wrapper for an unmanaged DLL. The code is:

int WINAPI DcpDll_GetFoundDevices
(int piIdx, /*[in] Device index in the internal
list(0..MAX_MEMBER)*/
char far *psName, /*[out] Device Name*/
char far *psIP, /*[out] Device Ip Address*/
char far *psSubnet,/*[out] Device Subnet Mask*/
char far *psMAC, /*[out] Device MAC Address*/
char far *psType) /*[out] Device Type*/

So far all I've got is:
[DllImport("DcpDll.dll")]
private static extern int DcpDll_GetFoundDevices
(int piIdx, ?? psName, ?? psIP, ?? psSubnet, ??
psMAC, ?? psType);

I just can't figure out how to do these char far* in C#. Can somebody
help? I'm getting desperate here.

Yours,
Chris
 
Chris Bruckner said:
Hi,

I have trouble with a C# wrapper for an unmanaged DLL. The code is:

int WINAPI DcpDll_GetFoundDevices
(int piIdx, /*[in] Device index in the internal
list(0..MAX_MEMBER)*/
char far *psName, /*[out] Device Name*/
char far *psIP, /*[out] Device Ip Address*/
char far *psSubnet,/*[out] Device Subnet Mask*/
char far *psMAC, /*[out] Device MAC Address*/
char far *psType) /*[out] Device Type*/

So far all I've got is:
[DllImport("DcpDll.dll")]
private static extern int DcpDll_GetFoundDevices
(int piIdx, ?? psName, ?? psIP, ?? psSubnet, ??
psMAC, ?? psType);

I just can't figure out how to do these char far* in C#. Can somebody
help? I'm getting desperate here.

You should be passing byte arrays... don't know what size the name or type
should be but I'd guess the IP address and subnet mask are both 4 bytes and
the MAC address is 6, unless they're in ASCII text format in which case give
16 bytes for IP address and subnet mask and 18 bytes for MAC address.
 
You should be passing byte arrays... don't know what size the name or type
should be but I'd guess the IP address and subnet mask are both 4 bytes and
the MAC address is 6, unless they're in ASCII text format in which case give
16 bytes for IP address and subnet mask and 18 bytes for MAC address.

It's ASCII text format.

Still got an "A call to PInvoke function
'DCPConnector::DcpDll_GetFoundDevices' has unbalanced the stack."
error.

What I've got now:
[DllImport("DcpDll.dll", CharSet=CharSet.Ansi)]
private static extern int DcpDll_GetFoundDevices
(int piIdx,
byte[] psName,
byte[] psIP,
byte[] psSubnet,
byte[] psMAC,
byte[] psType);

public void GetDeviceList(Boolean save)
{
int MAX_MEMBER = 500;
int iReturn;
byte[] sName = new byte[260];
byte[] sIP = new byte[16];
byte[] sSubnet = new byte[16];
byte[] sMAC = new byte[18];
byte[] sType = new byte[260];

for (int i = 0; i < MAX_MEMBER; i++)
{
if (save)
{
iReturn = DcpDll_GetFoundDevices(i, sName,
sIP, sSubnet, sMAC, sType);
}
}
}
 
Chris Bruckner said:
It's ASCII text format.

Still got an "A call to PInvoke function
'DCPConnector::DcpDll_GetFoundDevices' has unbalanced the stack."
error.

I would specify the calling convention explicitly. You showed the C header
using WINAPI, which would be CallingConvention.StdCall.
What I've got now:
[DllImport("DcpDll.dll", CharSet=CharSet.Ansi)]
private static extern int DcpDll_GetFoundDevices
(int piIdx,
byte[] psName,
byte[] psIP,
byte[] psSubnet,
byte[] psMAC,
byte[] psType);

public void GetDeviceList(Boolean save)
{
int MAX_MEMBER = 500;
int iReturn;
byte[] sName = new byte[260];
byte[] sIP = new byte[16];
byte[] sSubnet = new byte[16];
byte[] sMAC = new byte[18];
byte[] sType = new byte[260];

for (int i = 0; i < MAX_MEMBER; i++)
{
if (save)
{
iReturn = DcpDll_GetFoundDevices(i, sName,
sIP, sSubnet, sMAC, sType);
}
}
}
 
Back
Top