Original post address: http://blog.csdn.net/xiaogelee/archive/2007/12/04/1916716.aspx
Recently, when calling the C ++ DLL Interface in C #, I encountered some parameter passing problems. After several explorations, the problem was finally solved. Now I want to sort it out as follows.
P (rogrammer) Friends help:
If the input parameter of the DLL interface is Char **, that is, the array of character arrays (that is, the string array), the string [] cannot be directly passed in the C # declaration. the passed character is a char [] encoded by the encoding class.
If the output parameter of the DLL interface is Char **, that is, the array of character arrays (that is, the string array), byte [] should be used as the parameter in the C # declaration. Then, the byte [] is decoded through the encoding class to obtain the string. For example:
C ++ DLL interface:
Long _ stdcall use_getagentgroupinfoex (
/* [In] */Char ** agentid,
/* [In] */long arraysize,
/* [Out] */Char ** agentname,
/* [Out] */long agentstatus [],
/* [Out] */Char ** agentdn,
/* [Out] */Char ** agentip );
C # statement
[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
);
Call in C:
Encoding encode = Encoding. Default;
Byte [] tAgentID;
Byte [] tAgentName;
Int [] tAgentStatus;
Byte [] tAgentDN;
Byte [] tAgentIP;
Int rslt =-1;
Int agentCount;
// AgentID: an externally defined string [] Type Variable
// AgentName: an externally defined string [] Type Variable
// AgtIDLength: the length of the char [] for storing AgtID defined in C ++ DLL
// AgtNameLength: the length of the char [] for storing AgtName defined in C ++ DLL
// AgtDNLength: the length of the char [] for storing AgtDN defined in C ++ DLL
// AgtIPLength: the length of the char [] for storing AgtIP defined in C ++ DLL
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 );
//......
}
}
}
For basic numeric data, if it is an input parameter, it is passed by value directly. If it is an output parameter, it needs to be passed by reference (with ref modifier ).
No matter what type of array, it is actually passed by reference during the transfer, so it does not need to be modified by ref. Otherwise, an error will occur. For outgoing parameters, remember to assign sufficient space before calling.