Question

I've got a problem and I hope you guys can help me with this. I'm relatively new to programming in C# and have never worked with c++, so i can't figure out the following problem:

I'm using a SDK (which is writen in c++) to receive and request DDNS updates from a specific type of device. Setting the callback is working and the callback is triggered at the expected time.

The documentation says the callback contains a char** parameter, but I don't know what do do with this. I've tried a lot of options and the only way the application doesn't crash is by using an IntPtr for that parameter. But when I do this I'm not able to set a value for that IntPtr.

Original code I found using the callback:

int CALLBACK CBGetDeviceAddr(int *pResult, char** pIPAddr,unsigned short *pSdkPort, char *szDeviceID, char*szDeviceName)   
{   
int nCount=g_pDeviceList->GetItemCount();   
memset(g_szIPAddr, 0, MAX_IP_LEN+1);   
*pIPAddr=g_szIPAddr;   

BOOL bRet=FALSE;   
for(int i = 0; i< nCount; i++)   
{   

    if (strcmp(szDeviceID,(LPCTSTR)g_pDeviceList->GetItemText(i, 2).GetBuffer(0)) == 0)//,g_pDeviceList->GetItemText(i,2).GetLength()   
    {   
        *pResult=1;   
        strcpy(*pIPAddr,g_pDeviceList->GetItemText(i,0).GetBuffer(0));   
        *pSdkPort = atoi(g_pDeviceList->GetItemText(i,4).GetBuffer(0));   
        TRACE("CALLBACK CBGetDeviceAddrInfo:in DeviceID=%s, DeviceName=%s,out DeviceIP=%s,DevicePort = %d\n",\   
            szDeviceID, szDeviceName, *pIPAddr, *pSdkPort);   
        bRet = TRUE;   
        break;   
    }   
}   

if (!bRet)   
{   
    *pResult=0;   
    *pIPAddr=NULL;   
    TRACE("CALLBACK CBGetDeviceAddrInfo:in DeviceID=%s, DeviceName=%s,out no such device online!!!\n",\   
        szDeviceID, szDeviceName);   
}   

return 0;   
}

What I'm currently using:

private int CBF_GetADeviceIP(ref int pResult, ref IntPtr pIPAddr, ref ushort pSdkPort, string szDeviceID, string szDeviceName)

This is working for pResult and pSdkPort (so I can send data back) but can't do anything with pIPAddr.

Any help? Thanks

Was it helpful?

Solution

Normally when you see char**, it means "This parameter will be used to return a string".

When writing the P/Invoke for such an argument, you can usually use ref string (if the string is input, or input and output) or out string (if the string is output only).

The other thing you need to know is whether the native method is expecting ANSI or Unicode strings.

To specify which string encoding to use, use the CharSet attribute parameter:

[DllImport("MyDLL.dll", CharSet = CharSet.Unicode)]

or

[DllImport("MyDLL.dll", CharSet = CharSet.Ansi)]

and so on.

OTHER TIPS

I'm not that good about C#, but isn't pIPAddr a pointer to a string?

So you should declare that parameter as ref string pIPAddr.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top