做了一个DLL,里面有个函数叫Initialize()
我定义成
extern "C" VoiceDll * Initialize()
{
return new VoiceDll();
}
在Exe中引用
HMODULE hlib;
typedef BOOL *(*InitializeFunction)();
InitializeFunction lpfnRegisterServiceProcess;
hd=LoadLibrary("c:\\IPphoneDll.dll");
if (hd)
{
MessageBox("Sucess in LoadLibrary");
lpfnRegisterServiceProcess=(InitializeFunction) GetProcAddress(hlib,"Connect");
if(lpfnRegisterServiceProcess)
{
MessageBox("Sucess in GetProc");
m_voicedll=(VoiceDll *)(*lpfnRegisterServiceProcess)();
FreeLibrary(hd);
}
}
但是提取不出函数。
请教高手该如何定义?
我定义成
extern "C" VoiceDll * Initialize()
{
return new VoiceDll();
}
在Exe中引用
HMODULE hlib;
typedef BOOL *(*InitializeFunction)();
InitializeFunction lpfnRegisterServiceProcess;
hd=LoadLibrary("c:\\IPphoneDll.dll");
if (hd)
{
MessageBox("Sucess in LoadLibrary");
lpfnRegisterServiceProcess=(InitializeFunction) GetProcAddress(hlib,"Connect");
if(lpfnRegisterServiceProcess)
{
MessageBox("Sucess in GetProc");
m_voicedll=(VoiceDll *)(*lpfnRegisterServiceProcess)();
FreeLibrary(hd);
}
}
但是提取不出函数。
请教高手该如何定义?
lpfnRegisterServiceProcess=(InitializeFunction) GetProcAddress(hlib,"Initialize");
extern "c" _declspec(dllexport) VoiceDll * Initialize(); ?
//in dll fileextern "C"
{ __declspec(dllexport) VoiceDll* Initialize()
{
return new VoiceDll();
}}// in import file
HMODULE hlib;
typedef VoiceDll* (__stdcall* INITIALIZE)();
INITIALIZE initialize;
hlib = LoadLibrary("c:\\IPphoneDll.dll");
if (hlib!=NULL) {
AfxMessageBox("Sucess in LoadLibrary");
initialize = (INITIALIZE)GetProcAddress(hlib,"Initialize");
if(initialize!=NULL) {
MessageBox("Sucess in GetProc");
}
initialize();
}//--------------------
你在DLL中,你没有声明你的函数为导出函数,并且在导入函数的时候,函数名是错的,当然是导不出来的,并且调用的时候函数返回值类型也不对。
看来你应该先看看书了。:)
如果不是就要使用__declsepc(dllexport)
还要注意你的函数调用方式__stdcall,__cdecl还是__fastcalltypedef BOOL *(*InitializeFunction)();
改为
typedef VoiceDll* (__stdcall* INITIALIZE)();GetProcAddress(hlib,"Connect");
改为
GetProcAddress(hlib,"Initialize");