现在有一个DLL,用VC++写的。需要MFC类库支持。这个类不是COM组件,无法直接引用。我如何从C#调用C++的Dll中类中的方法?我现在有个头文件。但是这个头文件只能由VC++打开。
class __declspec(dllexport) KernelProcess
{
KernelProcess();
virtual ~KernelProcess;
void Init();
int Method_1(....);
int Method_2(....);
}应该是接口声明
class __declspec(dllexport) KernelProcess
{
KernelProcess();
virtual ~KernelProcess;
void Init();
int Method_1(....);
int Method_2(....);
}应该是接口声明
http://www.cnblogs.com/Chase/archive/2010/05/31/1748596.html
你的DLL有没有导出函数呢?
用Depends.exe查看一下呢,若没有导出,没办法吧……
根据这个类库的作者所说,这个DLL需要MFC支持。我也尝试过了。如果创建一个VC++6.0的WIN32工程(没有MFC支持)编译会出现错误。尝试Depends察看过了。但是似乎没有任何可用信息。
1、用C++写一个常规dll,C#调用常规dll,常规dll调用扩展dll。
2、不用那个扩展dll。
[DllImport("coredll.dll")]
public static extern uint GetTickCount();
“用C++写一个常规dll,C#调用常规dll,常规dll调用扩展dll。”
和上述的几位意见一致。
http://topic.csdn.net/u/20110413/18/8dbe1747-1cf8-4f5b-8475-972b2d5064a5.html
C++ DLL:#ifdef __cplusplus // If used by C++ code,
extern "C" { // we need to export the C interface
#endif#ifdef ZKADLL_EXPORTS
#define ZKADLL_API __declspec(dllexport)
#else
#define ZKADLL_API __declspec(dllimport)
#endif
namespace ZKA
{ class ZKADLL_API ZM30X
{
public:
ZM30X();
~ZM30X();
bool OpenDevice(string ipaddr);
}
}用C封装的DLL:#ifdef __cplusplus
extern "C" {
#endif#ifdef ZKADLL_EXPORTS
#define ZKADLL_API __declspec(dllexport)
#else
#define ZKADLL_API __declspec(dllimport)
#endif
namespace ZKA{
ZM30X* CreateZM30X()
{
return new ZM30X();
} void DisposeZM30X(ZM30X* zm30x)
{
if(zm30x != NULL)
{
delete zm30x;
zm30x = NULL;
}
} BOOL ZM30X_OpenDevice(ZM30X* zm30x, char* ipaddr)
{
if(zm30x != NULL)
{
return zm30x->OpenDevice(ipaddr);
}
else
{
return OBJECT_NOT_EXIST;
}
}
}用C#封装的DLL:namespace ZKA
{
/// <summary>
/// ZKA.ZM30X
/// </summary>
public class ZM30X : IDisposable
{
[DllImport("ZKA32.dll")]
static private extern IntPtr CreateZM30X(); [DllImport("ZKA32.dll")]
static private extern void DisposeZM30X(IntPtr zm30x); [DllImport("ZKA32.dll", EntryPoint = "ZM30X_OpenDevice")]
static private extern bool CppOpenDevice(IntPtr zm30x, string ipaddr); /// <summary>
/// creat object
/// </summary>
/// <returns>
/// none
/// </returns>
public ZM30X()
{
// We have to Create an instance of this class through an exported function
this.pNativeObject = CreateZM30X();
} /// <summary>
/// dispose object
/// </summary>
/// <returns>
/// none
/// </returns>
public void Dispose()
{
Dispose(true);
} /// <summary>
/// dispose object
/// </summary>
/// <returns>
/// none
/// </returns>
protected virtual void Dispose(bool bDisposing)
{
if (this.pNativeObject != IntPtr.Zero)
{
// Call the DLL Export to dispose this class
DisposeZM30X(this.pNativeObject);
this.pNativeObject = IntPtr.Zero;
} if (bDisposing)
{
// No need to call the finalizer since we've now cleaned
// up the unmanaged memory
GC.SuppressFinalize(this);
}
} /// <summary>
/// This finalizer is called when Garbage collection occurs, but only if the IDisposable.Dispose method wasn't already called.
/// </summary>
/// <returns>
/// none
/// </returns>
~ZM30X()
{
Dispose(false);
} #region Wrapper methods /// <summary>
/// Open device
/// </summary>
/// <returns>
/// true - open device succeed, false - open device failed
/// </returns>
public bool OpenDevice(string ipaddr)
{
return CppOpenDevice(this.pNativeObject, ipaddr);
}
}
}接下来直接调用就可以了。