Talk again in VB to transfer VC ++ developed DLL

zhaozj2021-02-12  151

Recently developed a program, using dynamic link libraries, encountered some problems in VB. I checked some information, I also read the article on 9CBS, I feel that these articles describe the problem of DLL developed by VC in VB, which is not detailed. Therefore, in my problem, I have written this article. Friends who come into contact with DLL. In this paper, the solution to the convention is also applicable to the compatibility of solving DLL calls between other programming languages. 1 About the creation of the DLL and calls to use the VC wizard. The specific operation is as follows: Open the menu "file / new" → select "Projects / Win32 Dynamic-Link Library" → Select "A Simple DLL Project". At this time, the system will automatically generate 3 files: *. CPP, stdafx.cpp, stdafx.h. Then add the entry function dllmain () complete, add the code you customize the function. If you have a lot of functions, you can uniformly write the declaration of these functions into a header file. Then introduced this header file with the "#include" statement in the .cpp file. Note that "__declspec (dllexport)" is added before the function declaration. (If you choose the third type (add sample code) when you build a DLL (add sample code), add the system-defined macro "* _API" in front of the function declaration and definition.) Declaring the following statement in VB: "Declare Function function name lib "full path / file name .dll" [alias "function alias"] (Byval Variable 1 AS Type 1, BYVAL Variable 2 AS Type 2, ...) AS Type 3 ", similar to calling API functions. Note: If you use it in the "General" section of the form code, "private" is added before "Declare"; if you use it in moudle, "public" is added before "Declare". If the DLL file is placed in the system directory ("/ Windows / System" or "/ WinNT / SYSTEM32") or the program executable, only the DLL main file name will only be written after "LIB". Specific instance code is shown 4 (after correction, it can be run directly).

2 About the entry point as the Cipher.dll, run, error messages, "No DLL entry point (Error 53)" is found. The reason for this error is that the C compiler has modified the function name Encrypt when compiling. Open the Quick View Program (D: /Winnt/System32/Viewers/quikView.exe), drag the cipher.dll into the view window, find the field "? Encrypt @@ yahhh @ z", found that the function name is added a string of characters. Solutions have two. First, directly "? Encrypt @@ y ahhh @ z" is placed in "alias" in the VB declaration; second, in the cipher.dll code in the statement "__declspec (dllexport) int __stdcall encrypt ( INT P, INT K); "Plus" extern "C" ", after compiling, use quikView to view, function names become" _Encrypt ", then adjust it accordingly in the VB declaration. (For DLLs using macro, in the "#define" statement, make changes to the replacement value of Hong "Cipher_API".) After the change is changed, the program cannot find the entry point. Use QuikView to view and find that the function name is "_Encrypt @ 8". There is also a solution. Add a text file to the Cipher.dll project, named "cipher.def", add code such as 4. After compiling, use the quikview to view, the function name changes back "Encrypt", calls in VB, running properly. Note that the .def file does not need to use "EXTERN" C "". 3 About the call agreement adopting the second second solution, operation, an error message "DLL call convention error (ERROR 49)". The reason is that the call is a total of 5 ways: __ fast, __ pascal, __ stdcall, __ cdecl, __thiscall To __stdcall. The solution is that the change code is as follows (specified call mode):

__DECLSPEC (DLLEXPORT) INT __STDCALL ENCRYPT (INT P, INT K); ............ int __stdcall Encrypt (int P, int K) {INT C = P K; returnif;}

4 Source code cipher.dll: copher.cpp: // introduced the pre-compiled head file #include "stdafx.h" #define cipher_api __declspec (dllexport) // Declare my function cipher_api int __stdcall Encrypt (int P, int K); ... // DLL Entrance Function Bool Apientry Dllmain (Handle Hmodule, DWORD UL_REASON_FOR_CALL, LPVOID LPRESERVED)

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

New Post(0)