VB (EXE)VC (DLL) Mixed Programming Example

xiaoxiao2021-03-06  19

VB (EXE) / VC (DLL) Mixed Programming Example

(1) First, open the VC, then create a Win32 Dynamic-Link Library project, then select the default empty project "A Empty DLL Project" in the project type, which does not contain any files, and then end the wizard.

(2) Manually add two files, a "C Source File", a "text file", please pay attention to the suffix of the two files, you can write the suffix when the source file is named, then the default is a CPP file (ie C file) You can also write ".c" to indicate that the C language source file (personal preference is a C language, in fact, the system DLL is written in C language, especially to point out, here is the suffix, source file The content should also be changed accordingly, and the specific sections will be pointed out); when the text file is named, please change the suffix name to DEF because this file is the output function list definition file.

(3) Ok, we only need these two files! Source files are used to define functions and specific function content, and the functions to be output are listed in the DEF file. The following is the code, first of all, the DLL project and the traditional EXE project have their own main function (it is equivalent to the main () and winmain () functions), the name is DLLMAIN.

⑷ Idea as follows, write a DLL master function (at this time, you can build a DLL file, but there is no output function), then in the main function, "Extern" function and the implementation of the function, the code is as follows: MyDLL The contents of the .C file are as follows: #include / * This header file can not be less * / extern int __stdcall getnum (int Num); / * EXTERN and __STDCALL These two keywords can not be less! There are 4 ways to call the convention: __ fastcall, __ pASCAL, __ stdcall, __ cdecl, VC default call mode is __cdecl, and VB default call mode is __stdcall. * /

/ * DLL engineering main function, here call does not call the function we have to output is irrelevant. * / BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {switch (ul_reason_for_call) {case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break;} return TRUE;}

INT __STDCALL GETNUM (INT NUM) / * Specific Realization * / {RETURN NUM;

MyDLL.DEF content:

Exports getnum / * This means that the output function of this DLL file * / / * If there are multiple output functions, the wrap continues to enter the function name can be * / ⑸ encoding is completed, after the compilation is complete, it can be in the working directory There is a MyDLL.dll file in the debug directory, copy this file to the system directory, and then open the VB to call the function of this DLL file output. This is to pay special attention: extern int __stdcall getnum (int Num); extern "c" int __stdcall getnum (int Num); / * More "c" * / this is because their source file is the default C Document (.cpp), so in order to comply with the C language call rule, add a "c" character, but here I use the source file is a pure C language file (.c), so I can't add "C", otherwise To report an error, please pay special attention!

It's okay, the next thing is VB this, I don't have much to say, it is a function declaration, but I have to repeat the statement: Please note that the function name and parameters must be completely in DLL. Explea matching in the definition! ! Especially cases, you need to match! Private declare function getnum lib "mydll.dll" (Byval Num as integer) AS INTEGER

For parameters in the DLL output function containing data pointer parameters, then in VB is declared to change the array pointer to the DLL ~! Such as: int __stdcall array (int * a, int number); Declare Function (byref samplea () As long, ByVal Numsam as integer) AS Integer

转载请注明原文地址:https://www.9cbs.com/read-41101.html

New Post(0)