调用非托管函数返回的char *烧焦 [英] calling unmanaged function char returns char *
问题描述
我在非托管的C / C ++ code(DLL)中的函数,返回包含字符数组的结构。我创建了C#结构来接收这个返回值uppon调用该函数。而uppon调用这个函数时得到System.Runtime.InteropServices.MarshalDirectiveException
这是C声明:
typedef结构T_SAMPLE_STRUCT {
INT NUM;
字符文本[20];
} SAMPLE_STRUCT;SAMPLE_STRUCT sampleFunction(SAMPLE_STRUCT SS);
这是C#声明:
结构SAMPLE_STRUCT
{
公众诠释NUM;
公共字符串文本;
}类Dllwrapper
{
函数[DllImport(samplecdll.dll)]
公共静态外部SAMPLE_STRUCT sampleFunction(SAMPLE_STRUCT SS);}
我使用1个字节的ASCII码。
有没有人有一个提示或如何做到这一点的解决方案?
诀窍转换C数组成员使用的MarshalAs(UnmanagedType.ByValTStr)。这可以用来告诉CLR到阵列编组作为一个内嵌部件相对于一个正常的非内嵌阵列。试试下面的签名。
<$p$p><$c$c>[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential,字符集= System.Runtime.InteropServices.CharSet.Ansi)公共结构T_SAMPLE_STRUCT { /// INT
公众诠释NUM; ///的char [20]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 20)]
公共字符串文本;
}公共部分类NativeMethods { ///返回类型:SAMPLE_STRUCT-&GT; T_SAMPLE_STRUCT
/// SS:SAMPLE_STRUCT-&GT; T_SAMPLE_STRUCT
[System.Runtime.InteropServices.DllImportAttribute(&所述;未知&gt;中,入口点=sampleFunction)]
公共静态外部T_SAMPLE_STRUCT sampleFunction(T_SAMPLE_STRUCT SS);}
这签名是由PInovke互操作助理(链接)在$ C $提供赞助商CPLEX。它可以从本地code最PInvoke的签名会自动转换成C#或VB.Net。
I have a function in unmanaged C/C++ code (dll) that returns a structure containing a char array. I created C# struct to receive this return value uppon calling the function. And uppon calling this function i get 'System.Runtime.InteropServices.MarshalDirectiveException'
This is C declaration:
typedef struct T_SAMPLE_STRUCT {
int num;
char text[20];
} SAMPLE_STRUCT;
SAMPLE_STRUCT sampleFunction( SAMPLE_STRUCT ss );
This is C# declaration:
struct SAMPLE_STRUCT
{
public int num;
public string text;
}
class Dllwrapper
{
[DllImport("samplecdll.dll")]
public static extern SAMPLE_STRUCT sampleFunction(SAMPLE_STRUCT ss);
}
I am using 1-byte ASCII.
Does anyone has a hint or a solution on how to do this?
The trick to converting a C array member is to use the MarshalAs(UnmanagedType.ByValTStr). This can be used to tell the CLR to marshal the array as an inlined member vs. a normal non-inlined array. Try the following signature.
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct T_SAMPLE_STRUCT {
/// int
public int num;
/// char[20]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=20)]
public string text;
}
public partial class NativeMethods {
/// Return Type: SAMPLE_STRUCT->T_SAMPLE_STRUCT
///ss: SAMPLE_STRUCT->T_SAMPLE_STRUCT
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="sampleFunction")]
public static extern T_SAMPLE_STRUCT sampleFunction(T_SAMPLE_STRUCT ss) ;
}
This signature is brought to you by the PInovke Interop Assistant (link) available on CodePlex. It can automatically translate most PInvoke signatures from native code to C# or VB.Net.
这篇关于调用非托管函数返回的char *烧焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!