简体   繁体   English

无法调用 DLL function

[英]Cannot call DLL function

I have been trying to call a DLL that simply displays a MessageBox.我一直在尝试调用仅显示 MessageBox 的 DLL。 I am running into issues where the loader will not locate the function.我遇到了加载程序找不到 function 的问题。 When the program is running, nothing happens.当程序运行时,什么也没有发生。 Tried using user32.dll which I know works for sure.尝试使用我知道肯定有效的 user32.dll。 Everything went fine with the SwapMouseButton function. SwapMouseButton function 一切正常。 I have defined my imports and exports.我已经定义了我的进口和出口。 Perhaps they were not done properly?也许他们没有正确完成? Were my calling conversions wrong?我的呼叫转换错了吗?

Here are my imports and exports:这是我的进口和出口:

#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 (trying to display MessageBox from DLL loader): 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 Loader (could have wrong conversions): 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;
} 

Your DLL function does not have a calling convention specified, so it will use the compiler's default, which is most likely __cdecl NOT __stdcall .您的 DLL function 没有指定调用约定,因此它将使用编译器的默认值,这很可能是__cdecl NOT __stdcall But your EXE is defining f_funci to use __stdcall .但是您的 EXE 将f_funci定义为使用__stdcall So, even if the function could be found, it likely won't be called correctly.因此,即使可以找到 function,也可能无法正确调用它。

Double-check the function's name mangling in the DLL's exports table.仔细检查 DLL 导出表中的函数名称修改 It i likely being exported as "_HelloWorld" instead of "HelloWorld" .我很可能被导出为"_HelloWorld"而不是"HelloWorld" Consider using a .DEF file when compiling the DLL in order to control how the function is exported.考虑在编译 DLL 时使用.DEF文件,以控制 function 的导出方式。

Also, consider wrapping the function's declaration in extern "C" when compiling in C++.此外,在 C++ 中编译时,请考虑将函数的声明包装在extern "C"中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM