[英]Cannot call DLL function
我一直在嘗試調用僅顯示 MessageBox 的 DLL。 我遇到了加載程序找不到 function 的問題。 當程序運行時,什么也沒有發生。 嘗試使用我知道肯定有效的 user32.dll。 SwapMouseButton function 一切正常。 我已經定義了我的進口和出口。 也許他們沒有正確完成? 我的呼叫轉換錯了嗎?
這是我的進口和出口:
#ifndef INDLL_H
#define INDLL_H
#ifdef EXPORTING_DLL
extern __declspec(dllexport) void HelloWorld();
#else
extern __declspec(dllimport) void HelloWorld();
#endif
#endif
Hello World DLL(嘗試從 DLL 加載程序顯示 MessageBox):
/* Hello World DLL */
#include "stdafx.h"
#define EXPORTING_DLL
#include "sampleDLL.h"
#include "pch.h"
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
void HelloWorld()
{
MessageBox(NULL, TEXT("Hello World"),
TEXT("In a DLL"), MB_OK);
}
DLL 加載程序(可能有錯誤的轉換):
/* DLL Loader */
#include <windows.h>
#include <iostream>
typedef int(__stdcall* f_funci)();
int main()
{
HINSTANCE hGetProcIDDLL = LoadLibrary("PATH\\LoadME.dll");
if (!hGetProcIDDLL) {
return EXIT_FAILURE;
}
// resolve function
f_funci functon = (f_funci)GetProcAddress(hGetProcIDDLL, "HelloWorld");
if (!functon) {
std::cout << "Function Could Not Be Located" << std::endl;
return EXIT_FAILURE;
}
std::cout << "returned " << functon() << std::endl;
return EXIT_SUCCESS;
}
您的 DLL function 沒有指定調用約定,因此它將使用編譯器的默認值,這很可能是__cdecl
NOT __stdcall
。 但是您的 EXE 將f_funci
定義為使用__stdcall
。 因此,即使可以找到 function,也可能無法正確調用它。
仔細檢查 DLL 導出表中的函數名稱修改。 我很可能被導出為"_HelloWorld"
而不是"HelloWorld"
。 考慮在編譯 DLL 時使用.DEF
文件,以控制 function 的導出方式。
此外,在 C++ 中編譯時,請考慮將函數的聲明包裝在extern "C"
中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.