我的C#项目必须引用C ++ dll,
struct中的SetConfig()是我的目标,头部看起来像
extern "C"
{
int HSAPI GetSDKVersion();
IHsFutuComm* HSAPI NewFuCommObj(void* lpReserved = NULL);
struct IHsFutuComm:public IHSKnown
{
virtual int HSAPI SetConfig(const char* szSection,const char* szName,const char* szVal) = 0;
...
}
}
而C#看起来像
class Program
{
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
public delegate Int32 GetSDKVersion();
public delegate IntPtr NewFuCommObj(IntPtr lpReserved);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct IHsFutuComm
{
[MarshalAs(UnmanagedType.FunctionPtr)]
public SetConfig pSetConfig;
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate Int32 SetConfig(string szSection, string szName, string szVal);
static void Main(string[] args)
{
var pDll = LoadLibrary("HsFutuSDK.dll");
var pGetSDKVersion = GetProcAddress(pDll, "GetSDKVersion");
var dGetSDKVersion = (GetSDKVersion)Marshal.GetDelegateForFunctionPointer(pGetSDKVersion, typeof(GetSDKVersion));
var sdkVersion = dGetSDKVersion();
if (sdkVersion == 0x10000019)
{
var pNewFuCommObj = GetProcAddress(pDll, "NewFuCommObj");
var dNewFuCommObj = Marshal.GetDelegateForFunctionPointer(pNewFuCommObj, typeof(NewFuCommObj)) as NewFuCommObj;
var pIHsFutuComm = dNewFuCommObj(IntPtr.Zero);
var IHsFutuComm1 = (IHsFutuComm)Marshal.PtrToStructure(pIHsFutuComm, typeof(IHsFutuComm));
//error here
var re = IHsFutuComm1.pSetConfig("futu", "server", "127.0.0.1:2800");
}
}
}
最后一行的错误是“尝试读取或写入受保护的内存。这通常表示其他内存已损坏。”
如何通过C#调用SetConfig()?
答案 0 :(得分:1)
在这个 SetConfig 是结构的(纯)虚拟成员函数,所以调用约定应该是 thiscall ,并且函数指针不在结构内部就像在你的C#代码中一样,但在它的vtable中。
c ++代码中结构的声明可能更像是C#中的接口,而不是C#中的结构。
对不起,我无法给出完整的答案,但我希望这会指出你正确的方向。