gpt4 book ai didi

c# - char** 从 C++ 到 C# 的回调并发回数据

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:37 24 4
gpt4 key购买 nike

我有一个问题,我希望你们能帮我解决这个问题。我对 C# 编程比较陌生,从未使用过 C++,所以我无法弄清楚以下问题:

我正在使用 SDK(用 C++ 编写)从特定类型的设备接收和请求 DDNS 更新。设置回调有效,回调在预期时间触发。

文档说回调包含一个 char** 参数,但我不知道如何处理它。我尝试了很多选项,应用程序不崩溃的唯一方法是对该参数使用 IntPtr。但是当我这样做时,我无法为该 IntPtr 设置值。

我使用回调找到的原始代码:

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;
}

我目前使用的是:

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

这适用于 pResult 和 pSdkPort(因此我可以发回数据)但不能对 pIPAddr 做任何事情。

有什么帮助吗?谢谢

最佳答案

通常当您看到char**时,表示“此参数将用于返回一个字符串”。

为此类参数编写 P/Invoke 时,通常可以使用 ref string(如果字符串是输入,或输入和输出)或 out string (如果仅输出字符串)。

您需要知道的另一件事是 native 方法是否需要 ANSI 或 Unicode 字符串。

要指定要使用的字符串编码,请使用 CharSet 属性参数:

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

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

等等。

关于c# - char** 从 C++ 到 C# 的回调并发回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19630317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com