【发布时间】:2015-12-11 16:55:16
【问题描述】:
我在那个问题之前问过。
How to pass C++ Struct To C# DLL ?
现在我正在尝试嵌套结构元帅。
我已经替换了这样的结构。
typedef struct TKeyValue
{
char Key[15];
char Value[15];
} TKeyValue;
typedef struct MyStruct
{
TKeyValue *KeyValues[1];
} TMyStruct;
typedef int (__stdcall *DoSomething)(char [],char[],TMyStruct *);
调用DLL函数
void __fastcall TForm1::btnInvokeMethodClick(TObject *Sender)
{
wchar_t buf[256];
String DLL_FILENAME = "ClassLibrary1.dll";
HINSTANCE dllHandle = NULL;
dllHandle = LoadLibrary(DLL_FILENAME.c_str());
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
OutputDebugStringW(buf);
if(dllHandle == NULL) return;
DoSomething xDoSomething = (DoSomething)GetProcAddress(dllHandle, "DoSomething");
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
OutputDebugStringW(buf);
if (xDoSomething == NULL) return;
try
{
TMyStruct aMyStruct;
char parameter1[15];
char parameter2[15];
strcpy(parameter1,"value1");
strcpy(parameter2,"value2");
int result = xDoSomething(parameter1,parameter2,&aMyStruct);
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
OutputDebugStringW(buf);
//access violation at this line
ShowMessage(aMyStruct.KeyValue[0]->Key);
}
catch(EAccessViolation &err)
{
ShowMessage(err.Message);
}
FreeLibrary(dllHandle);
C#端
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct KeyValue
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public string Key;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public string Value;
}
[ComVisible(true)]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public KeyValue[] KeyValues;
}
[DllExport("DoSomething", CallingConvention = CallingConvention.StdCall)]
public static int DoSomething(string tcKnVkn, string sifre, ref MyStruct myStruct)
{
try
{
MyStruct.KeyValues[0].Key = "key 1";
MyStruct.KeyValues[0].Value = "value 1";
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
return 2;
}
当我编译我的项目并运行时,它在像aMyStruct.KeyValue[0]->Key那样访问 TKeyValue 的 Key 字段后引发访问冲突错误
我怎么了?
谢谢。
【问题讨论】:
-
删除除嵌套结构之外的所有内容。确保它有效。然后添加结构的嵌套数组。检查是否有效。等等。这就是调试的本质。首先隔离问题。
-
myStruct.KeyValues = new KeyValue[2]看起来很奇怪。编组器不分配吗?无论如何,在你减少它并隔离问题并在两边显示完整的代码之前,我不会进一步研究,minimal reproducible example。 -
我已经修改了我的问题
-
好吧,在 C++ 端,你有一个指向 struct 的指针数组,在 C# 端有一个 struct 数组。
-
稍后我会尝试调试。我很困惑。谢谢
标签: c# c++ struct pinvoke marshalling