C#调用C++DLL返回值是个抽象类指针,怎么调用其中的函数(不对外发布)

C#调用C++DLL返回值是个抽象类指针,如何调用其中的函数(不对外发布)?
C#调用C++DLL返回值是个抽象类指针,如何调用其中的函数(不对外发布)?
------解决思路----------------------
给你一个样例。
C++ 接口
struct ICForCS2
{
virtual int Login(const char* user_name, const char* password, int login_type, LoginInfo *plogin_info, const char* end_point_url, const char* local_ip = NULL) = 0;
virtual int GetUdiskInfo2(s_udiskinfo *a, int max) = 0;


};


C++ 的获取接口的 API。
CFORCS_API void GetICForCS2(ICForCS2 **p);


C# 定义对应的接口
	[StructLayout(LayoutKind.Sequential)]
struct INativeCSForC2
{
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate int _LoginHandler(IntPtr @this, [In, MarshalAs(UnmanagedType.LPStr)] string user_name, [In, MarshalAs(UnmanagedType.LPStr)] string password, [In, MarshalAs(UnmanagedType.I4)]int login_type, [In, Out] ref LoginInfo plogin_info, [In, MarshalAs(UnmanagedType.LPStr)] string end_point_url, [In, MarshalAs(UnmanagedType.LPStr)] string local_ip);
public _LoginHandler Login;

[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate int _GetUdiskInfoHandler([In] IntPtr @this, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] s_udiskinfo[] info, int max);
public _GetUdiskInfoHandler GetUdiskInfo;
}


C# 定义这个接口。
	interface ICSForC22
{
int Login([In, MarshalAs(UnmanagedType.LPStr)] string user_name, [In, MarshalAs(UnmanagedType.LPStr)] string password, [In, MarshalAs(UnmanagedType.I4)]int login_type, [In, Out] ref LoginInfo plogin_info, [In, MarshalAs(UnmanagedType.LPStr)] string end_point_url, [In, MarshalAs(UnmanagedType.LPStr)] string local_ip);
int GetUdiskInfo(s_udiskinfo[] info);
}


添加一个包装类。
	class NativeCSForC2 : ICSForC22
{
public NativeVTable<INativeCSForC2> _native;
int ICSForC22.Login(string user_name, string password, int login_type, ref LoginInfo login_info, string end_point_url, string local_ip)
{
return _native._nativeInterface.Login(_native._ptrInterface, user_name, password, login_type, ref login_info, end_point_url, local_ip);
}

int ICSForC22.GetUdiskInfo(s_udiskinfo[] info)
{
return _native._nativeInterface.GetUdiskInfo(_native._ptrInterface, info, info.Length);
}

}


C# 定义 API 函数对应的方法。
	
[DllImport(....)]
private static extern void _GetICForCS2([Out] out IntPtr ptrInterface);
public static NativeCSForC2 GetICForCS2()
{
IntPtr ptrInterface;
_GetICForCS2(out ptrInterface);
return new NativeCSForC2
{
_native = new NativeVTable<INativeCSForC2>(ptrInterface),
};
}


如果使用很多的话,定义一个 模板包装
	class NativeVTable< I > : IDisposable
{
public IntPtr _ptrInterface;
public I _nativeInterface;
private Action< IntPtr > _release = null;
public NativeVTable(IntPtr ptrInterface)
{
System.Diagnostics.Debug.Assert(ptrInterface != IntPtr.Zero);
_ptrInterface = ptrInterface;
_nativeInterface = (I)Marshal.PtrToStructure(Marshal.ReadIntPtr(ptrInterface, 0), typeof(I));
}
public NativeVTable(IntPtr ptrInterface, Action< IntPtr > release) : this(ptrInterface)
{
_release = release;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (_release != null)
{
_release(_ptrInterface);
}
disposed = true;
}
}

~NativeVTable()
{
Dispose(false);
}

}





最后使用:
	var vv = (ICSForC22)NativeImport.GetICForCS2();