我有这样定义的结构:如何调用dll C++ func,它需要参数并返回C#中的struct?
typedef struct
{
int number;
void *ptr;
}clist_t;
和在DLL中的两个函数:
DLL_API clist_t GetArgs1(const wchar_t* const name)
{
static clist_t arg;
arg.number = 1;
arg.ptr = sth(name);
return arg;
}
DLL_API clist_t GetArgs2()
{
static clist_t arg;
arg.number = 1;
arg.ptr = sth2();
return arg;
}
接着我在C#的包装,其与DLL名称初始化:
public class DllWrapper
{
private int m_dllHndl;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] // <-- added as suggested in comment
private delegate ArgsList ArgsDelegate();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] // <-- added as suggested in comment
private delegate ArgsList ArgsStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string name);
private ArgsStringDelegate GetFunc1 = null;
private ArgsDelegate GetFunc2 = null;
public DllWrapper(string dllPathName)
{
m_dllHndl = Win32APIWrapper.LoadLibrary(dllPathName);
if(m_dllHndl == 0)
{
throw new Exception("Could not load the library: " + dllPathName);
}
GetFunc1 = (ArgsStringDelegate)findFunc("GetArgs1", typeof(ArgsStringDelegate));
GetFunc2 = (ArgsDelegate)findFunc("GetArgs2", typeof(ArgsDelegate));
}
private Delegate findFunc(string name, Type t)
{
int func = Win32APIWrapper.GetProcAddress(m_dllHndl, name);
if (func == 0)
{
throw new Exception("Function not found in the library: " + name);
}
return Marshal.GetDelegateForFunctionPointer((IntPtr)func, t);
}
public ArgsList Get1(string name)
{
ArgsList list = GetFunc1(name);
return list; // <-- here list is corrupted
}
public ArgsList Get2()
{
ArgsList list = GetFunc2();
return list; // <-- here list is correct
}
}
ArgsList定义如下:
[StructLayout(LayoutKind.Sequential)]
public struct ArgsList
{
public int number;
public IntPtr ptr;
};
当我调用Get2()时,结果是正确的,list.number是1,指针可以解组。但是在Get1()之后,返回的结构就像:list.number = 0,list.ptr = 48,这显然是错误的。
这两种方法只有在缺少Get2参数时才有所不同。我检查了调试器,该字符串参数正确传递给DLL。然后struct clist_t被正确地填充到dll中,但是在返回时,当控件从dll传回C#时,返回的结构会以某种方式损坏。
你能给我一些提示什么问题? 只有没有参数时,struct为什么会正确返回?
编辑:我在使用extern "C"
在dll中声明函数时。
似乎问题是从dll返回结构的C#。参数传递正常,但返回时,结构没有正确地从C++镜像到C#。 – toomyem 2014-11-05 09:02:58