c/c++中_stdcall与dll动态调用

1._stdcall在动态dll调用中的注意事项

为了用vc写的dll能被其它语言的写的程序使用,即实现跨语言。我们在dll的函数调用约定中使用__stdcall .

但当用GetProcAddress调用是却失败了.

用dumpbin工具查看导出的函数名可以看到:导出的函数名确实为_mygetGrad@20

所以我们要阻止导出的函数名被编译器修改,方法就是使用def文件

; xxx.def : Declares the module parameters for the DLL.
LIBRARY      "xxx"
EXPORTS
    ; Explicit exports can go here
    mygetGrad @1

这样重新调用GetProcAddress就正确了

并且在函数指针定义和GetProcAddress时必须把__stdcall 加上否则运行时会报错

void (_stdcall *mygetGrad)(unsigned char*, unsigned char*, int, int, long);


2. .c文件中函数中间声明变量有时会报错,把声明放到最前面就ok,不知道为啥


3.动态加载要#include


4.LoadLibraryA与LoadLibrary

LoadLibrary动态加载dll失败,把LoadLibrary改为LoadLibraryA就ok

  HMODULE hDllInst;
  void (_stdcall *mygetGrad)(unsigned char*, unsigned char*, int, int, long);
  hDllInst = NULL;
  mygetGrad = NULL;
  hDllInst = LoadLibraryA("FignerPrintAnalysis.dll");

  mygetGrad = (void (_stdcall *)(unsigned char*, unsigned char*, int, int, long))GetProcAddress(hDllInst,"mygetGrad");


参考

http://blog.csdn.net/cglover/article/details/1621685

http://blog.csdn.net/guoyong10721073/article/details/52399027

http://blog.csdn.net/dybinx/article/details/7709822

你可能感兴趣的:(C/C++,函数,应用相关)