今天動態呼叫vc寫的dll時遇到個怪事,以前一直沒這個問題。具體如下,有一個XXXX.dll,dll內有函式int xxFunc(int x,char *str),我呼叫時
//定義
typedef int(__stdcall *XXFUNC)(int x,char *str);
XXFUNC xxFunc;
//在呼叫的函式中
int callfunc(int x,char* str)
{
int rtn = 0;
AnsiString dllName = "XXXX.dll";
HINSTANCE hInstance = LoadLibrary(dllName.c_str());
FARPROC fp = GetProcAddress(hInstance,"rdcompany"); //除錯時這里的fp!=NULL
if(fp ==NULL)
{
//提示
}
xxFunc = (XXFUNC)fp;//除錯時在這里xxFunc==NULL,為什么?
rtn = xxFunc(x,str);
retun rtn;
}
開發環境:bcb6.0
作業系統環境:win10 home basic
uj5u.com熱心網友回復:
// 定義
typedef int __stdcall MyFunction (int x, char *str);
int rtn = 0;
String dllName = "XXXX.dll";
HINSTANCE hInstance = LoadLibrary(dllName.c_str());
MyFunction * pMyFunction = (MyFunction*) GetProcAddress(hInstance, "rdcompany"); // 函式名稱要正確
if (pMyFunction == NULL)
{
// 提示
}
rtn = pMyFunction(1, "aa");
FreeLibrary(hInstance);
hInstance = NULL;
uj5u.com熱心網友回復:
函式名稱肯定是沒問題的,fp!=NULL這已經能說明問題了,如果函式名稱不對,fp為NULL
uj5u.com熱心網友回復:
先判斷 LoadLibrary 是否成功,再判斷 GetProcAddress 是否成功。如果失敗,用 GetLastError 獲取詳細錯誤碼。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/88649.html
上一篇:試利用T1定時/計數器,實作使P2.0口輸出周期為80ms的方波,設晶振頻率為6MHZ。
下一篇:表單間復雜關系的處理
