为什么GetProcAddress不工作? [英] Why GetProcAddress doesn't work?

查看:191
本文介绍了为什么GetProcAddress不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我创建一个名为 SimpleDll.dll 的简单DLL,其头文件:

First, I create a simple dll called SimpleDll.dll, its head file:

// SimpleDll.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif

MYLIBAPI int Add(int a. int b);

其源代码:

// SimpleDll.c
#include <windows.h>

#define MYLIBAPI __declspec(dllexport)
#include "SimpleDll.h"    

int Add(int a, int b)
{
    return a + b;
}

然后我在另一个项目中调用它,它工作正常:

Then I call it in another project, and it works fine:

// TestSimpleDll.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"

#pragma comment(lib, "SimpleDll.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    printf("%d", Add(10, 30));    // Give the expected result 40
    return 0;
}

但是,当我调用 GetProcAddress 得到它的地址,它不工作!

However, when I call GetProcAddress to get it's address, it doesn't work!

// TestSimpleDll2.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"

#pragma comment(lib, "SimpleDll.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    printf("%d", Add(10, 30));    // Give the expected result 40
    HMODULE hModule = GetModuleHandleA("SimpleDll.dll");    // hModule is found
    PROC add_proc       = GetProcAddress(hModule, "Add");     // but Add is not found !
    //  add_proc is NULL!
    return 0;
}

感谢您的协助。 (PS:我在Windows7上使用VS2010)

更新:

这是Depedency Walker为显示的SimpleDll.dll 档案:

Thanks for your help. (PS: I use VS2010 on Windows7)
Update:
This is what the depedency walker show for the SimpleDll.dll file:

>

推荐答案

如果您要导出GetProcAddress的名称,则应使用.def文件。否则,您将必须处理c ++名称错误和符号装饰。

You should use a .def file if you want to export the name for GetProcAddress. Otherwise you will have to deal with c++ name mangling and with symbol decorations.

您可以通过将函数声明为 externC来避免损坏,但避免装饰的唯一方法是要使用.DEF文件。

You can avoid mangling by declaring your function as extern "C", but the only way to avoid decorations is to use a .DEF file.

还有一件事 - 在Dependency walker中 - 使用F10在修饰和未修饰的名称之间切换。

One more thing - in Dependency walker - use F10 to toggle between decorated and undecorated names.

这篇关于为什么GetProcAddress不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆