Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 托管与非托管:如何封送 我使用一个包含此签名的方法的C++ DLL: int GetEnumerationString(int type, int value, const char** s ); /* ALLOCATES */_C#_Dllimport_Signature - Fatal编程技术网

C# 托管与非托管:如何封送 我使用一个包含此签名的方法的C++ DLL: int GetEnumerationString(int type, int value, const char** s ); /* ALLOCATES */

C# 托管与非托管:如何封送 我使用一个包含此签名的方法的C++ DLL: int GetEnumerationString(int type, int value, const char** s ); /* ALLOCATES */,c#,dllimport,signature,C#,Dllimport,Signature,如标题注释中所述,该方法分配指针。我在C#中尝试了以下签名: 但是当我运行我的程序时,我得到一个AccessViolationException 方法的正确签名是什么?如果非托管函数分配缓冲区,它必须使用CLR分配器,以便CLR拾取内存并在需要时释放内存 如果不可能,则必须返回指针: [DllImport("thedll.dll", EntryPoint="GetEnumerationString")] private static extern int GetEnumerationString

如标题注释中所述,该方法分配指针。我在C#中尝试了以下签名:

但是当我运行我的程序时,我得到一个
AccessViolationException


方法的正确签名是什么?

如果非托管函数分配缓冲区,它必须使用CLR分配器,以便CLR拾取内存并在需要时释放内存

如果不可能,则必须返回指针:

[DllImport("thedll.dll", EntryPoint="GetEnumerationString")]
private static extern int GetEnumerationString(int type, int value, out IntPtr s);

然后手动处理指向的内存(您必须知道分配程序是什么)。

我自己没有尝试过,但您是否尝试过从导入信号中删除“ref”ref'表示您正在通过引用传递字符串,但是尝试从非托管代码访问托管字符串可能会导致AccessViolationException
[DllImport("thedll.dll", EntryPoint="GetEnumerationString")]
private static extern int GetEnumerationString(int type, int value, out IntPtr s);