GetProcAddress 使用注意事项
使用 GetProcAddress Function 时,有以下几点需要特别留意:
1. 第二个参数类型是 LPCSTR,不是 ;
2. 用 __declspec(dllexport),按 C 名称修饰(extern "C")
导出的函数名,对于 __stdcall 和 __fastcall 调用约定是相同的;对 __cdecl 是不同的(导出的函数名没有前面的下划线);
3. 即使返回值不是 NULL,也有可能发生错误。当 .def 模块不是连续地从 1 开始编号 ordinal 值,那么,如果用一个无函数对应的 ordinal 值调用 GetProcAddress,就会发生错误,返回一个无效的非 NULL 地址;
4. 最好用函数名,而不是 ordinal 值调用 GetProcAddress,以避免不同版本 Dll 中某些函数不存在的情况。
注:确认 Dll 的导出函数名,可以用 DUMPBIN /EXPORTS dll_file_name.dll 命令,然后查看 name 列。
[cpp] view plain copy
- // The myPuts function writes a null-terminated string to
- // the standard output device.
- // The export mechanism used here is the __declspec(export)
- // method supported by Microsoft Visual Studio, but any
- // other export method supported by your development
- // environment may be substituted.
- #include <windows.h>
- #define EOF (-1)
- #ifdef __cplusplus // If used by C++ code,
- extern "C" { // we need to export the C interface
- #endif
- __declspec(dllexport) int __cdecl myPuts(LPTSTR lpszMsg) // __cdecl | __stdcall | __fastcall
- {
- DWORD cchWritten;
- HANDLE hStdout;
- BOOL fRet;
- // Get a handle to the standard output device.
- hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- if (INVALID_HANDLE_VALUE == hStdout)
- return EOF;
- // Write a null-terminated string to the standard output device.
- while (*lpszMsg != '\0')
- {
- fRet = WriteFile(hStdout, lpszMsg, 1, &cchWritten, NULL);
- if( (FALSE == fRet) || (1 != cchWritten) )
- return EOF;
- lpszMsg++;
- }
- return 1;
- }
- #ifdef __cplusplus
- }
- #endif
[cpp] view plain copy
- // A simple program that uses LoadLibrary and
- // GetProcAddress to access myPuts from Myputs.dll.
- #include <stdio.h>
- #include <windows.h>
- typedef int (__cdecl *MYPROC)(LPTSTR); // __cdecl | __stdcall | __fastcall
- VOID main(VOID)
- {
- HINSTANCE hinstLib;
- MYPROC ProcAdd;
- BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
- // Get a handle to the DLL module.
- hinstLib = LoadLibrary(TEXT("bin\\Myputs")); // 虽然 MSDN Library 说这里如果
- // 指定了路径,要用 backslashes (\),
- // 不要用 forward slashes (/),但
- // 其实用二者都可以。
- // 注:如果用 \,要用 \\。
- // If the handle is valid, try to get the function address.
- if (hinstLib != NULL)
- {
- ProcAdd = (MYPROC)GetProcAddress(hinstLib, "myPuts"); // __cdecl : myPuts
- // __stdcall : _myPuts@4
- // __fastcall: @myPuts@4
- // If the function address is valid, call the function.
- if (NULL != ProcAdd)
- {
- fRunTimeLinkSuccess = TRUE;
- (ProcAdd) (TEXT("Message via DLL function\n"));
- }
- // Free the DLL module.
- fFreeResult = FreeLibrary(hinstLib);
- }
- // If unable to call the DLL function, use an alternative.
- if (! fRunTimeLinkSuccess)
- printf("Message via alternative method\n");
- }
原文:http://blog.csdn.net/g5dsk/article/details/6680698
GetProcAddress 使用注意事项的更多相关文章
- 64位内核开发第二讲.内核编程注意事项,以及UNICODE_STRING
目录 一丶驱动是如何运行的 1.服务注册驱动 二丶Ring3跟Ring0通讯的几种方式 1.IOCTRL_CODE 控制代码的几种IO 2.非控制 缓冲区的三种方式. 三丶Ring3跟Ring0开发区 ...
- jQuery UI resizable使用注意事项、实时等比例拉伸及你不知道的技巧
这篇文章总结的是我在使用resizable插件的过程中,遇到的问题及变通应用的奇思妙想. 一.resizable使用注意事项 以下是我在jsfiddle上写的测试demo:http://jsfiddl ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- TODO:Golang指针使用注意事项
TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...
- app开发外包注意事项,2017最新资讯
我们见过很多创业者,栽在这app外包上.很多创业者对于app外包这件事情不是特别重视,以为将事情交给app外包公司就完事了,实际上不是的.无论是从选择app外包公司还是签订合同.售后维护等各方面都有许 ...
- favicon.ioc使用以及注意事项
1.效果 2.使用引入方法 2.1 注意事项:(把图标命名为favicon.ico,并且放在根目录下,同时使用Link标签,多重保险) 浏览器默认使用根目录下的favicon.ico 图标(如果你并没 ...
- ORACLE分区表梳理系列(二)- 分区表日常维护及注意事项(红字需要留意)
版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...
- 【原】Masonry+UIScrollView的使用注意事项
[原]Masonry+UIScrollView的使用注意事项 本文转载请注明出处 —— polobymulberry-博客园 1.问题描述 我想实现的使用在一个UIScrollView依次添加三个UI ...
- 《连载 | 物联网框架ServerSuperIO教程》- 5.轮询通讯模式开发及注意事项。附:网友制作的类库说明(CHM)
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
随机推荐
- POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan
http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...
- [Hdu4372] Count the Buildings
[Hdu4372] Count the Buildings Description There are N buildings standing in a straight line in the C ...
- 502 解决:[WARNING] fpm_children_bury
查过网上的资源,基本都是认为是php线程打开文件句柄受限导致的错误.具体的解决的办法如下: 1.提升服务器的文件句柄打开打开 /etc/security/limits.conf : (增加) * ...
- 关于JBoss日志中的报错Exception in thread "AWT-EventQueue-0"的解决记录
一.前情提要 操作系统:Windows Server 2008 R2,JDK版本:1.6.0_45,应用容器:JBoss 4.2.3 GA.所部署的应用均为Web型项目,没有任何图形相关的项目. 二. ...
- 删除JBOSS eap4.3下的jmx-console、web-console、ws-console、status服务
来源:http://iffiffj.iteye.com/blog/1404148 把下面代码保存为BAT文件,并放到JBOSS节点中运行. @echo off set HOME=%~dp0 set j ...
- bzoj 2300 动态维护上凸壳(不支持删除)
新技能GET. 用set保存点,然后只需要找前趋和后继就可以动态维护了. /************************************************************** ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- TeamViewer运行在Windows Server 2008下连接时错误提示:正在初始化显示参数
这个是使用远程桌面安装和使用Teamviewer的问题,解决方法: 实际上安装完成后TeamViewer有两个ID,一个是个人ID(就是上面卡住的780 567 914),另一个是服务器ID,我们通过 ...
- Android intent action大全
android.intent.action.ALL_APPSandroid.intent.action.ANSWERandroid.intent.action.ATTACH_DATAandroid.i ...
- Maven最佳实践 划分模块 配置多模块项目 pom modules
所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...