About parameter transfer when C ++ DLL is called in C #

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.