c++ - 为什么 GetProcAddress() 不起作用?

标签 c++ windows visual-studio-2010 dll

我对 DLL 编程还很陌生。

我已经创建了一个 DLL 项目,

在DLL工程SimpleH.h

namespace ME{
    class base
    {
    public:
        static __declspec(dllexport) void Hello();
    };
}
__declspec(dllexport) void HelloWorld();

DLL.cpp

#include "stdafx.h"
#include <iostream>
#include "SimpleH.h"
using namespace std;
namespace ME
{
    void base::Hello()
        {
            cout << "Hello World\n";
        }
}
void HelloWorld()
{
    cout << "Hello I am world\n";
}

我创建了一个.exe

Main.exe

    #include "stdafx.h"
    #include <stdlib.h>
    #include <Windows.h>
    #include <iostream>

    using namespace std;
    typedef void (*FunctionFunc)();
    typedef void (*FunctionAno)();
    int _tmain(int argc, _TCHAR* argv[])
    {

        FunctionFunc _FunctionFunc;
        FunctionAno _FunctionAno;
        HINSTANCE hInstLibrary = LoadLibrary(L"MyDLL.dll");
        if(hInstLibrary)
        {
            /*Problem Happens here*/
            _FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary, "Hello");
            _FunctionAno = (FunctionFunc)GetProcAddress(hInstLibrary, "HelloWorld");
/**/
            if(_FunctionFunc)
            {
                _FunctionFunc(); 
            }
            if(_FunctionAno)
            {
                _FunctionAno();
            }
          DWORD error = ::GetLastError();
          if (error)
          {
            LPVOID lpMsgBuf;
            LPVOID lpDisplayBuf;

            FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            error,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0, NULL );

          }
        }
        FreeLibrary(hInstLibrary);
        system("pause");
        return 0;
    }

hInstance 正在正确更新。 但是 GetProcAddress() 没有得到更新。 请帮我。 哪里出错了?

最佳答案

导出的函数具有“修饰名称”。使用 DUMPBIN 查看它们。

DUMPBIN/EXPORTS Your.dll

关于c++ - 为什么 GetProcAddress() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422550/

相关文章:

c++ - 我的(基于指针的)合并排序有什么问题?

c# - 调试和跟踪 Write/WriteLine 未输出到 Visual Studio 2010 C# Express 中的输出

c# - 可以将 C# 获取、设置代码转换为 C++

c++ - OpenCV 高斯模糊破坏了 Tesseract?

c++ - C++ 中的线性同余生成器、运算符 & 和 >>

c++ - 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的 vector ?

windows - 使用 Windows 批处理文件运行可执行文件

c# - 使用 CANCEL 选项 C# 防止 windows 关机

php - 在 Windows 上发出 PHP cURL 请求从代理中产生 "400 Bad Request"

c++ - C 在运行时发出警告?