C#调用C++ DLL 的传参问题

来源:互联网 发布:linux c 语言编程 编辑:程序博客网 时间:2024/04/29 21:44

如果DLL接口的输入参数为char**,也就是字符数组的数组(即字符串数组),此时在C#声明中不能直接传递string[],传递的应该是通过Encoding类对这个string[]进行编码后得到的一个char[]。
如果DLL接口的输出参数为char**,也就是字符数组的数组(即字符串数组),此时在C#声明中应该使用byte[]来做参数。然后通过Encoding类对这个byte[]进行解码,得到字符串。如下例所示:
C++ DLL接口:
long _stdcall USE_GetAgentGroupInfoEx(
/* [in] */ char** AgentID,
/* [in] */ long ArraySize,
/* [out] */ char** AgentName,
/* [out] */ long AgentStatus[],
/* [out] */ char** AgentDN,
/* [out] */ char** AgentIP);
C#中的声明
[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修饰,否则会出错。对于传出型参数,在调用前要记得分派足够的空间。

该文章转载自网络大本营:http://www.xrss.cn/Info/15356.Html