当前位置:网站首页>DLL bit by bit
DLL bit by bit
2022-06-13 06:03:00 【You must know this and do it yourself】
It is often used in the system DLL Program , Most of the DLL The program works at the bottom , Provide support for upper layer applications , Development DLL The following points shall be paid attention to during the procedure :
1、 Data storage and exchange
quite a lot DLL Programs are dynamically loaded ( use Loadlibrary function ),DLL The data in follows Freelibrary And died , Sometimes DLL The functions in are related , Such as DLL Middle function 2 need DLL Middle function 1 Generated data , At this point, the function is finished 1 when DLL already Freelibrary 了 , At another moment, the customer reloads DLL And call DLL The function in 2, That function 2 How to get the function 1 The generated data ? Whether to call the function again 1 Well ? left-off , You can use windows File mapping mechanism (IPC) To save the function 1 Generated data , Supply function 2 Use , The code is as follows :
Memory mapped files ( Shared memory ) Example :
// Write memory mapped file
extern "C" __declspec(dllexport) void __cdecl WriteMap(unsigned char* buff, int len=1024)
{
char MapName[16]={0};
HANDLE hMap;
LPVOID pBuffer;
sprintf(MapName,"%s","ShareMemory");
hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, MapName);
if (hMap==NULL){
hMap = CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
len+1,
MapName);
}
pBuffer = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
memcpy(pBuffer,buff,len);
}
// Read memory mapped file
extern "C" __declspec(dllexport) int __cdecl ReadMap(unsigned char* buff,int len=1024)
{
char MapName[16]={0};
HANDLE hMap;
LPVOID pBuffer;
sprintf(MapName,"%s","ShareMemory");
hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, MapName);
if (hMap==NULL){
return -1;
}
pBuffer = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
memcpy(buff,pBuffer,len);
UnmapViewOfFile(pBuffer);
CloseHandle(hMap);
return 0;
}
WriteMap The function is responsible for DLL Middle function 1 Write the data to the memory mapped file ,ReadMap The function is responsible for reading the written data , Once the data is written , As long as the client's program is running , No matter DLL Released or loaded , Data always exists ( The memory mapped file in this example is equivalent to shared memory )
2、 Reentry prevention and queuing
quite a lot DLL It is time-consuming communication or equipment IO operation , To be on the safe side DLL Functions in should prevent threads from re entering , Or queue up , Ensure correct operation , You can use techniques such as semaphores or critical areas to protect your code .
3、 routing problem
Sometimes it needs to be in DLL In order to get DLL Path to file , At this time, we can obtain (win32 DLL nothing MFC Support )
First get and save DLL Handle
HMODULE m_hModule; // Definition m_hModule, preservation DLL Handle
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
m_hModule=hModule; // preservation DLL Handle
return TRUE;
}
And then use getDLLPath Function is used to obtain DLL route :
void getDLLPath(HANDLE hModule, char* DLLPath) {
char DLLPathTemp[1024] = { 0 };
GetModuleFileName((HMODULE)hModule, DLLPathTemp, 1024);
strncpy(DLLPath, DLLPathTemp, strrchr(DLLPathTemp, '\\') - DLLPathTemp + 1); // Take out the path of the file
}
The advantage of this is that no matter DLL What's the name , Can dynamically obtain DLL The path of .
If you generate DLL When used MFC, You can use the following methods to obtain DLL The path of , Here's a snippet of the program
// The only one CMemoryMapDllApp object
CMemoryMapDllApp theApp;
// CMemoryMapDllApp initialization
BOOL CMemoryMapDllApp::InitInstance()
{
CWinApp::InitInstance();
char disp[128]={0};
char DLLPathTemp[1024] = { 0 };
sprintf(disp,"%s.dll",theApp.m_pszAppName);
GetModuleFileName(GetModuleHandle(disp), DLLPathTemp, 1024); //DLLPathTemp It's preserved in DLL The path of
//MessageBox(NULL,DLLPathTemp,"dll path",0);
return TRUE;
}
4、 File permissions
Sometimes our DLL The program shall be installed in the embedded system wince or xp And other embedded terminal devices , These systems set read and write permissions on certain directories , That's when our DLL You should pay attention to the file operation , In order to prevent the operation error from causing the program to crash .
5、 Export functions and _cdecl _stdcall
_stdcall yes Pascal The default calling mode of the program , The parameters are stack pressed from right to left , The called function itself clears the stack before returning .WIN32 Api All use _stdcall Call mode ._cdecl yes C/C++ Default calling method , The parameters are stack pressed from right to left , The memory stack that passes parameters is maintained by the caller ._cedcl The agreed function can only be C/C++ call , Every function that calls it contains the code to empty the stack , So the size of the resulting executable is larger than that of the call _stdcall The size of the function . If our function uses _cdecl, Then the stack is cleared by the caller , This brings about a problem , Different compilers produce stacks in different ways , Can the caller complete the cleanup work normally ? The answer is no . If you use __stdcall, The above problems are solved , The function solves the cleanup by itself . therefore , across ( Development ) The platform is being called , We all use __stdcall( Although sometimes with WINAPI The appearance of ). So why do we need _cdecl Well ? When we encounter such a function as fprintf() Its parameters are variable , Indefinitely long , The callee cannot know the length of the parameter in advance , The cleaning work after the event can not be carried out normally , therefore , In this case we can only use _cdecl. Here we have a conclusion , If your program does not involve variable parameters , Best use __stdcall.6、 Link issues
DLL When compiling the program, try to chain all dependent libraries into the program at one time , In especial MFC Of DLL Pay attention to the compile time selection “ Use... In a static library MFC”, lest DLL Possible errors on different devices after release .
7、Dll pack
Sometimes we are DLL Another... Is dynamically loaded in DLL, At this time, you can consider adding another DLL Packaged as a resource into our DLL in , Dynamically release when using DLL, The code is as follows :
HMODULE hThis = hModuleDll; //DLL Handle
HRSRC hRes = FindResource(hThis, MAKEINTRESOURCE(IDR_DLL1), "DLL");
HGLOBAL hGres = LoadResource(hThis, hRes);
HANDLE hFile = CreateFile("TesoLive.dll", GENERIC_WRITE, NULL, NULL, CREATE_NEW,FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, NULL);
PVOID pRes = LockResource(hGres);
DWORD dwSize = SizeofResource(hThis, hRes);
DWORD dwSizeWritten = 0;
WriteFile(hFile, pRes, dwSize, &dwSizeWritten, NULL);
CloseHandle(hFile);
Now you can load “TesoLive.dll” 了 .
8、DLL Development tool selection
If our DLL Need to be used in java or python Wait for the environment , Do not choose a version that is too high vs, Such as vs2105 In the environment DLL stay jdk1.6 or python2 There is a compatibility problem on , Now many enterprise applications are still in jdk1.6 or python2 On such a platform , So now we are developing DLL Use vs2005 It's more appropriate , do DLL Not too far ahead .
边栏推荐
- The problem of distinguishing and sharing sessions for multiple applications in tongweb
- OpenGL馬賽克(八)
- Audio stereo to mono (Audio Dual Channel to mono channel)
- 20 flowable container (event sub process, things, sub process, pool and pool)
- 零拷贝技术
- Parallelgateway and exclusivegateway of 14 gateways
- 为什么那么多人讨厌A-Spice
- Test logiciel - résumé des FAQ d'interface
- 2021.9.29 learning log restful architecture
- OpenGL mosaic (VIII)
猜你喜欢
中断处理过程
[compilation and assembly link] coff file and structure description
How MySQL optimizes the use of joint index ABC
Sentinel series introduction to service flow restriction
12 error end event and terminateendevent of end event
零拷贝技术
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
Pod libwebp error reporting solution
OpenGL馬賽克(八)
Config server configuration center of Nacos series
随机推荐
Leetcode judge subsequence simple
Audio stereo to mono (Audio Dual Channel to mono channel)
Leetcode- keyboard line - simple
Lamda expression
Leetcode Timo attack - simple
Pod libwebp error reporting solution
Annotation only integration SSM framework
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Leetcode- number of maximum consecutive ones - simple
Etcd understanding of microservice architecture
Leetcode- intersection of two arrays - simple
Leetcode guessing numbers game - simple
15 inclusivegateway and eventgateway of flowable gateway
Power simple of leetcode-3
Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
2021.9.29 learning log MIME type
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
Leetcode- key formatting - simple
How MySQL optimizes the use of joint index ABC
安全基线检查脚本—— 筑梦之路