我在c#中有一个windows表单应用程序,它使用c ++中的函数.这是使用c ++包装器完成的.但是,该函数需要使用指针,而c#不允许使用带字符串数组的指针.可以做些什么来克服这个问题?我已经阅读了使用marshal但我不确定这是否适用于这种情况,如果是这样,应该如何与我的代码集成.以下是C#代码:
int elements = 10;
string [] sentence = new string[elements];
unsafe
{
fixed (string* psentence = &sentence[0])
{
CWrap.CWrap_Class1 contCp = new CWrap.CWrap_Class1(psentence, elements);
contCp.getsum();
}
}
Run Code Online (Sandbox Code Playgroud)
c ++函数声明:
funct::funct(string* sentence_array, int sentence_arraysize)
c ++包装器:
CWrap::CWrap_Class1::CWrap_Class1(string *sentence_array, int sentence_arraysize)
{
pcc = new funct(sentence_array, sentence_arraysize);
}
如果我理解正确的话,你想调用一个以字符串作为参数的 C 函数。
为此,您通常使用 PInvoke(平台调用),它使用手下的编组。
此示例接受一个字符串并返回一个字符串。
[DllImport(KMGIO_IMPORT,CallingConvention=CallingConvention.Cdecl)]
//DLL_EXPORT ushort CALLCONV cFunction(char* sendString, char* rcvString, ushort rcvLen);
private static extern UInt16 cFunction(string sendString, StringBuilder rcvString, UInt16 rcvLen);
public static string function(string sendString){
UInt16 bufSize = 5000;
StringBuilder retBuffer = new StringBuilder(bufSize);
cFunction(sendString, retBuffer, bufSize);
return retBuffer.ToString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5920 次 |
| 最近记录: |