These two days with CB (Code::Blocks) to write a small program, to compile the DLL for VB (6) to use. CB uses MINGW-GCC as the compiler, the output of the library file with the VC, VS and the like the IDE slightly different.
Because the basic knowledge of C language is not very good, especially for the compilation process is very little known. As a result, the functions in the exported DLL are always unable to be called in a few attempts.
VB loaded with Always prompt "DLL calling convention Error", Baidu learned that VB can only invoke the adaptation __stdcall Convention (which is also the default way other languages can call C) function.
So in the source file in front of the function plus __stdcall, export and then prompt "DLL entry point foo in MyDLL.DLL", search to know that may be the name of the exported function is problematic.
Open the DLL Export Viewer, load MyDLL.DLL, and discover that the function has become "[email protected]".
The online statement is the use of __stdcall side effects, you can use the extern "C" to avoid, and then add the extern "C", the result is still.
Others say that you can use the DEF file to control the name of the exported function, but I also did not find out how to add to the compilation process.
After continuous google&baidu, it is found that GCC can eliminate this situation by specifying the--kill-at parameter during the link phase. Then, followed by the next GCC to understand the use of the method, after several attempts to finally succeed.
In this have to spit groove Some people write technical blog, a lot of problems is a word with, let people see foggy. I think people can say the wrong thing, but at least to make their own meaning to express clearly, or randomly gather out an article astray mistake oneself.
Here is a simple example to illustrate how to do this:
Build the DLL project with the following structure:
test/----mydll.h----MYDLL.C
Header file: mydll.h
#ifndef __mydll_h__#define__mydll_h__#ifdef Build_dll#defineDll_export __declspec (dllexport)#else #defineDll_export __declspec (dllimport)#endif#ifdef __cplusplusextern "C" {#endifDll_exportint__stdcall Foo (intx); #ifdef __cplusplus}#endif#endif //__mydll_h__
C File: mydll.c
" mydll.h " int __stdcall foo (int x) { return x;}
If you have Code::Blocks (and MinGW) installed, create an environment variable:
Mingw_home=C:\Program Files\codeblocks\mingwpath=%mingw_home%\bin;%path%
Open the command line and go to our project path:
d:\test># executes the compile command d:\test>mingw32-gcc-c-dbuild_dll mydll. C#执行链接命令, generate Mydll. dll and static library files Libmydll.ad:\test>mingw32-gcc-shared-o mydll.dll mydll.o-wl,--kill-at,--out-implib, Libmydll. aCreating library File:libmydll. A
Above, is the whole process of generating DLL.
Next, open VB to verify the following:
Private Declare Function Lib " D:\test\mydll.dll " (ByValAs integer asintegerPrivateSub Form_Load () Debug.Print foo (ten)End Sub
Run OK, Immediate window output 10.
Test with Python:
Import ctypes>>> mydll = ctypes.windll.LoadLibrary ("d:\\test\\mydll.dll" )print Mydll.foo (ten)
Well, no problem, then the result is correct.
Of course, because of my low level, the article must have described the wrong place, you God if you see also please feel free.
Reference Documentation:
Http://baike.baidu.com/view/2814224.htm
Http://baike.baidu.com/view/1276580.htm?fr=aladdin
Http://blog.sina.com.cn/s/blog_4b02b8d001000avi.html
MinGW (GCC) Compiling DLL files