C#调用C DLL时 关于参数为char *类型的转换

如果DLL接口的输入参数为char**,也就是字符数组的数组(即字符串数组),此时在C#声明中不能直接传递string[],传递的应该是通过Encoding类对这个string[]进行编码后得到的一个char[]。

如果DLL接口的输出参数为char**,也就是字符数组的数组(即字符串数组),此时在C#声明中应该使用byte[]来做参数。然后通过Encoding类对这个byte[]进行解码,得到字符串。如下例所示:

C++ DLL接口:

1
2
3
4
5
6
7
long _stdcall USE_GetAgentGroupInfoEx(
/* [in] */ char** AgentID,
/* [in] */ long ArraySize,
/* [out] */ char** AgentName,
/* [out] */ long AgentStatus[],
/* [out] */ char** AgentDN,
/* [out] */ char** AgentIP);

C#中的声明

1
2
3
4
5
6
7
8
9
10
[DllImport("ProxyClientDll")]
public static extern int USE_GetAgentGroupInfoEx
(
/*[in]*/ char[] allAgentID,
/*[in]*/ int agentCount,
/*[out]*/ byte[] AgentName,
/*[out]*/ int[] AgentStatus,
/*[out]*/ byte[] AgentDN,
/*[out]*/ byte[] AgentIP
);

C#中的调用:

Encoding encode = Encoding.Default;
byte[] tAgentID;
byte[] tAgentName;
int[] tAgentStatus;
byte[] tAgentDN;
byte[] tAgentIP;

int rslt = -1;
int agentCount;
   
//agentID:是个外部定义的string[]类型变量
//AgentName:是个外部定义的string[]类型变量
//AgtIDLength:是C++ DLL中定义的存放AgtID的char[]的长度
//AgtNameLength:是C++ DLL中定义的存放AgtName的char[]的长度
//AgtDNLength:是C++ DLL中定义的存放AgtDN的char[]的长度
//AgtIPLength:是C++ DLL中定义的存放AgtIP的char[]的长度
   
if (agentID.Length > 0)
{
   agentCount = agentID.Length;
   tAgentID = new byte[AgtIDLength * agentCount];
   tAgentName = new byte[AgtNameLength * agentCount];
   tAgentStatus = new int[agentCount];
   tAgentDN = new byte[AgtDNLength * agentCount];
   tAgentIP = new byte[AgtIPLength * agentCount];
   for(int i = 0; i<agentCount; i ++ )
   {
       encode.GetBytes(agentID[i]).CopyTo(tAgentID, i* AgtIDLength);
   }
   rslt = USE_GetAgentGroupInfoEx(encode.GetChars(tAgentID), agentCount,
   tAgentName, tAgentStatus, tAgentDN, tAgentIP);
   
   if(rslt == 0 )
   {
        for(int i = 0; i<agentCount; i ++ )
        {
            AgentName[i] = encode.GetString(tAgentName, i* AgtNameLength, AgtNameLength);
           //……
        }
   }
}

对于基本的数值类型的数据,如果是输入参数,则直接按值传递,如果是输出参数,则需要按引用传递(加ref 修饰)。

不论什么类型的数组,传递时实际上都是按引用传递,所以不需要再用ref修饰,否则会出错。对于传出型参数,在调用前要记得分派足够的空间。

C#调用C DLL时 关于参数为char *类型的转换

https://wurang.net/csharp_calling_c_dll_conversion_of_char/

作者

Wu Rang

发布于

2012-03-23

更新于

2021-12-06

许可协议

评论