In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to export C++ DLL". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There are usually two ways to export DLL interfaces in a project:
1. _ _ declspec (dllexport) Export
2.*.def file export.
_ _ declspec (dllexport) export
Use the _ declspec (dllexport) keyword to export the function interface before the function name of the DLL export.
Test_dll.h// test_dll.h # ifdef _ _ cplusplusextern "C" {# endif#ifdef DLL_EXPORT#else#define DLL_EXPORT _ declspec (dllexport) # endif DLL_EXPORT int add (int a, int b); DLL_EXPORT int sub (int a, int b); # ifdef _ cplusplus} # endiftest_dll.cpp// test_dll.cpp#include "test_dll.h" int add (int a, int b) {return a + b } int sub (int a, int b) {return a-b;} def file export
Because the _ _ declspec (dllexport) export interface needs to define a long list of contents, MS introduces the def file to export the function interface.
It is relatively easy to use def files, you only need to add a file with the suffix .def to the project, and define the interface that DLL needs to export in the def file according to the format.
1. Define .def file
Test_dll.def
LIBRARY test_dllEXPORTS add @ 1 sub @ 2
DllName represents the file name of Dll
@ 1 [optional] specifies the sequence number of the exported function. When exporting multiple functions, the developer needs to customize the sequence number, which cannot be repeated.
two。 Add a def file to the project
3. Set project properties
Enter the name of the def file in the Properties-> Linker-> input configuration module definition file.
4. Compile and generate DLL
Use the tool to view the exported function name interface, which is in the same order as the sequence number defined in the def file.
There are problems
Through the tool, you can view the export function name and expose the function of the interface function.
The exported function name can be accessed and used arbitrarily, even if the business function is encapsulated in the internal code and encrypted, but the external interface is public and can be called normally. Resulting in unknowingly, being abused by unauthorized developers, resulting in indirect losses.
Solution: export anonymous interface export anonymous serial number interface
The anonymous export API only needs to add the NONAME keyword to the export interface name of the def file, as follows:
Test_dll.def
LIBRARY test_dllEXPORTS add @ 1 NONAME sub @ 2 NONAME
NONAME means that the function name is not exported (anonymous).
Use the CFF Explorer tool to view the interface name of the exported function, which has been hidden.
Access anonymous serial number interface static link
In the same way that a project usually references DLL, reference .h, link .lib, and copy the .dll file to the application running directory before running.
Dynamic loading
Call the DLL interface through dynamic loading.
When exporting sequence numbers, defining function pointers becomes complex, and you can only determine the number and type of parameters and define function pointers through static decompilation.
# include / / define function pointer typedef int (* func_ptr) (int, int); int dynamic_load_dll () {/ / dynamically load DLL file HINSTANCE hDll = LoadLibrary ("test_dll.dll"); / / define function pointer variable func_ptr add_ptr, sub_ptr; if (hDll) {add_ptr = (func_ptr) GetProcAddress (hDll, (LPCSTR) 1) / / load the function (add) sub_ptr = (func_ptr) GetProcAddress (hDll, (LPCSTR) 2) according to the export sequence number; / / load the function (sub)} else {printf ("Load DLL failed!\ n"); return 1;} if (add_ptr) {printf ("% d\ n", add_ptr (11,4)) } else {printf ("GetProcAddress add function failed!\ n"); return 1;} if (sub_ptr) {printf ("% d\ n", sub_ptr (21,4));} else {printf ("GetProcAddress sub function failed!\ n"); return 1;} return 0 } Note: the type declaration that defines the function pointer must be consistent with the function definition, otherwise it cannot be used properly. " This is the end of how to Export C++ DLL. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.