使用 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

 print?

  1. // The myPuts function writes a null-terminated string to  
  2. // the standard output device.  
  3.    
     
  4. // The export mechanism used here is the __declspec(export)  
  5. // method supported by Microsoft Visual Studio, but any  
  6. // other export method supported by your development  
  7. // environment may be substituted.  
  8.    
     
  9.    
     
  10. #include <windows.h>  
  11.    
     
  12. #define EOF (-1)  
  13.    
     
  14. #ifdef __cplusplus    // If used by C++ code,   
  15. extern "C" {          // we need to export the C interface  
  16. #endif  
  17.    
     
  18. __declspec(dllexportint __cdecl myPuts(LPTSTR lpszMsg) // __cdecl | __stdcall | __fastcall  
  19. {  
  20.     DWORD cchWritten;  
  21.     HANDLE hStdout;  
  22.     BOOL fRet;  
  23.    
     
  24.     // Get a handle to the standard output device.  
  25.    
     
  26.     hStdout = GetStdHandle(STD_OUTPUT_HANDLE);  
  27.     if (INVALID_HANDLE_VALUE == hStdout)  
  28.         return EOF;  
  29.    
     
  30.     // Write a null-terminated string to the standard output device.  
  31.    
     
  32.     while (*lpszMsg != '\0')  
  33.     {  
  34.         fRet = WriteFile(hStdout, lpszMsg, 1, &cchWritten, NULL);  
  35.         if( (FALSE == fRet) || (1 != cchWritten) )  
  36.             return EOF;  
  37.         lpszMsg++;  
  38.     }  
  39.    
     
  40.     return 1;  
  41. }  
  42.    
     
  43. #ifdef __cplusplus  
  44. }  
  45. #endif  

[cpp] view plain copy

 print?

  1. // A simple program that uses LoadLibrary and   
  2. // GetProcAddress to access myPuts from Myputs.dll.   
  3.    
     
  4. #include <stdio.h>   
  5. #include <windows.h>   
  6.    
     
  7. typedef int (__cdecl *MYPROC)(LPTSTR); // __cdecl | __stdcall | __fastcall  
  8.    
     
  9. VOID main(VOID)   
  10. {   
  11.     HINSTANCE hinstLib;   
  12.     MYPROC ProcAdd;   
  13.     BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;   
  14.    
     
  15.     // Get a handle to the DLL module.  
  16.    
     
  17.     hinstLib = LoadLibrary(TEXT("bin\\Myputs")); // 虽然 MSDN Library 说这里如果  
  18.                                                  // 指定了路径,要用 backslashes (\),  
  19.                                                  // 不要用 forward slashes (/),但  
  20.                                                  // 其实用二者都可以。  
  21.                                                  // 注:如果用 \,要用 \\。  
  22.    
     
  23.     // If the handle is valid, try to get the function address.  
  24.    
     
  25.     if (hinstLib != NULL)   
  26.     {   
  27.         ProcAdd = (MYPROC)GetProcAddress(hinstLib, "myPuts"); // __cdecl   : myPuts  
  28.                                                               // __stdcall : _myPuts@4  
  29.                                                               // __fastcall: @myPuts@4  
  30.    
     
  31.         // If the function address is valid, call the function.  
  32.    
     
  33.         if (NULL != ProcAdd)   
  34.         {  
  35.             fRunTimeLinkSuccess = TRUE;  
  36.             (ProcAdd) (TEXT("Message via DLL function\n"));   
  37.         }  
  38.    
     
  39.         // Free the DLL module.  
  40.    
     
  41.         fFreeResult = FreeLibrary(hinstLib);   
  42.     }   
  43.    
     
  44.     // If unable to call the DLL function, use an alternative.  
  45.    
     
  46.     if (! fRunTimeLinkSuccess)   
  47.         printf("Message via alternative method\n");   
  48. }  

原文:http://blog.csdn.net/g5dsk/article/details/6680698

GetProcAddress 使用注意事项的更多相关文章

  1. 64位内核开发第二讲.内核编程注意事项,以及UNICODE_STRING

    目录 一丶驱动是如何运行的 1.服务注册驱动 二丶Ring3跟Ring0通讯的几种方式 1.IOCTRL_CODE 控制代码的几种IO 2.非控制 缓冲区的三种方式. 三丶Ring3跟Ring0开发区 ...

  2. jQuery UI resizable使用注意事项、实时等比例拉伸及你不知道的技巧

    这篇文章总结的是我在使用resizable插件的过程中,遇到的问题及变通应用的奇思妙想. 一.resizable使用注意事项 以下是我在jsfiddle上写的测试demo:http://jsfiddl ...

  3. Windows Server 2012 NIC Teaming介绍及注意事项

    Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...

  4. TODO:Golang指针使用注意事项

    TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...

  5. app开发外包注意事项,2017最新资讯

    我们见过很多创业者,栽在这app外包上.很多创业者对于app外包这件事情不是特别重视,以为将事情交给app外包公司就完事了,实际上不是的.无论是从选择app外包公司还是签订合同.售后维护等各方面都有许 ...

  6. favicon.ioc使用以及注意事项

    1.效果 2.使用引入方法 2.1 注意事项:(把图标命名为favicon.ico,并且放在根目录下,同时使用Link标签,多重保险) 浏览器默认使用根目录下的favicon.ico 图标(如果你并没 ...

  7. ORACLE分区表梳理系列(二)- 分区表日常维护及注意事项(红字需要留意)

    版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...

  8. 【原】Masonry+UIScrollView的使用注意事项

    [原]Masonry+UIScrollView的使用注意事项 本文转载请注明出处 —— polobymulberry-博客园 1.问题描述 我想实现的使用在一个UIScrollView依次添加三个UI ...

  9. 《连载 | 物联网框架ServerSuperIO教程》- 5.轮询通讯模式开发及注意事项。附:网友制作的类库说明(CHM)

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

随机推荐

  1. BZOJ3611 HEOI2014大工程

    先建虚树,然后统计答案. 对于这个两点间最大值和最小值的操作我参考了hzwer的代码. 建虚树时注意判自环 By:大奕哥 #include<bits/stdc++.h> using nam ...

  2. android实现gif图播放、暂停、继续播放

    之前做过一个项目,在android上实现gif图的播放以及点击屏幕弹出窗口显示gif图片的暂停,之前一直用gifView的jar包实现gif图片的显示,但是在gif暂停.继续播放这块没有找到好的解决方 ...

  3. poj 2623 Sequence Median 堆的灵活运用

    I - Sequence Median Time Limit:1000MS     Memory Limit:1024KB     64bit IO Format:%I64d & %I64u ...

  4. SGU 403 Game with points

    408. Game with points Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: standa ...

  5. Linux性能监控分析命令(二)—sar命令介绍

    性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof ======= ...

  6. 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”

    报错内容如下: 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM对象强制转换为接口类型“Microsoft.Office.Inte ...

  7. 理解PHP数组的序列化和反序列化

    当我们想要将数组值存储到数据库时,就可以对数组进行序列化操作,然后将序列化后的值存储到数据库中.其实PHP序列化数组就是将复杂的数组数据类型转换为字符串,方便数组存库操作.对PHP数组进行序列化和反序 ...

  8. 该死的Ubuntu 16.04不自动续租DHCP的IP

    BUG,这是一个BUG,参考:https://bugs.launchpad.net/ubuntu/+source/isc-dhcp/+bug/1551351,如果不自动续租IP,导致的问题就是网线灯还 ...

  9. mysql文件目录详解 LINUX

    http://www.cnblogs.com/yjf512/archive/2012/12/11/2813398.html

  10. .Net高级技术——垃圾收集器

    垃圾收集器概述 大排档和学校食堂.一个是别人帮你收拾盘子,一个是自己收拾盘子. 垃圾收集GC(Garbage Collection).内存的分配.回收不需要程序员操心,程序员只要需要的时候new就可以 ...