

码龄10年
暂无认证
34157访问
1
等级
8获赞
3
评论
热门文章
-
一.某岛之人物对象及其加密函数解析
2148
-
一.《传奇M》装备栏遍历的突破口
1791
-
二.某龙端游中LUA的分析和调用
1540
-
一.《轩辕传奇》周围遍历之二叉树
1386
-
一.《UE4奥丁》人物最大属性
1331
最新评论
-
20240228阿⑤
积分乍获得
-
20231124ziher
666
-
20230909八月老师
什麼時候發佈易語言支持庫呢
十.线程局部变量tls
heart
2024-10-10 10:55:58 发布
274
分类专栏: Win32开发 文章标签: 线程局部变量tls
// The DLL code #include static DWORD dwTlsIndex; // address of shared memory // DllMain() is the entry-point function for this DLL. BOOL WINAPI DllMain(HINSTANCE hinstDLL, // DLL module handle DWORD fdwReason, // reason called LPVOID lpvReserved) // reserved { LPVOID lpvData; BOOL fIgnore; switch (fdwReason) { // The DLL is loading due to process // initialization or a call to LoadLibrary. case DLL_PROCESS_ATTACH: // Allocate a TLS index. if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE; // No break: Initialize the index for first thread. // The attached process creates a new thread. case DLL_THREAD_ATTACH: // Initialize the TLS index for this thread. lpvData = (LPVOID) LocalAlloc(LPTR, 256); if (lpvData != NULL) fIgnore = TlsSetValue(dwTlsIndex, lpvData); break; // The thread of the attached process terminates. case DLL_THREAD_DETACH: // Release the allocated memory for this thread. lpvData = TlsGetValue(dwTlsIndex); if (lpvData != NULL) LocalFree((HLOCAL) lpvData); break; // DLL unload due to process termination or FreeLibrary. case DLL_PROCESS_DETACH: // Release the allocated memory for this thread. lpvData = TlsGetValue(dwTlsIndex); if (lpvData != NULL) LocalFree((HLOCAL) lpvData); // Release the TLS index. TlsFree(dwTlsIndex); break; default: break; } return TRUE; UNREFERENCED_PARAMETER(hinstDLL); UNREFERENCED_PARAMETER(lpvReserved); } // The export mechanism used here is the __declspec(export) // method supported by Microsoft Visual Studio, but any // other export method supported by your development // environment may be substituted. #ifdef __cplusplus // If used by C++ code, extern "C" { // we need to export the C interface #endif __declspec(dllexport) BOOL WINAPI StoreData(DWORD dw) { LPVOID lpvData; DWORD * pData; // The stored memory pointer lpvData = TlsGetValue(dwTlsIndex); if (lpvData == NULL) { lpvData = (LPVOID) LocalAlloc(LPTR, 256); if (lpvData == NULL) return FALSE; if (!TlsSetValue(dwTlsIndex, lpvData)) return FALSE; } pData = (DWORD *) lpvData; // Cast to my data type. // In this example, it is only a pointer to a DWORD // but it can be a structure pointer to contain more complicated data. (*pData) = dw; return TRUE; } __declspec(dllexport) BOOL WINAPI GetData(DWORD *pdw) { LPVOID lpvData; DWORD * pData; // The stored memory pointer lpvData = TlsGetValue(dwTlsIndex); if (lpvData == NULL) return FALSE; pData = (DWORD *) lpvData; (*pdw) = (*pData); return TRUE; } #ifdef __cplusplus } #endif #include #include #define THREADCOUNT 4 #define DLL_NAME TEXT("testdll") VOID ErrorExit(LPSTR); extern "C" BOOL WINAPI StoreData(DWORD dw); extern "C" BOOL WINAPI GetData(DWORD *pdw); DWORD WINAPI ThreadFunc(VOID) { int i; if(!StoreData(GetCurrentThreadId())) ErrorExit("StoreData error"); for(i=0; i<THREADCOUNT; i++) { DWORD dwOut; if(!GetData(&dwOut)) ErrorExit("GetData error"); if( dwOut != GetCurrentThreadId()) printf("thread %d: data is incorrect (%d)\n", GetCurrentThreadId(), dwOut); else printf("thread %d: data is correct\n", GetCurrentThreadId()); Sleep(0); } return 0; } int main(VOID) { DWORD IDThread; HANDLE hThread[THREADCOUNT]; int i; HMODULE hm; // Load the DLL hm = LoadLibrary(DLL_NAME); if(!hm) { ErrorExit("DLL failed to load"); } // Create multiple threads. for (i = 0; i < THREADCOUNT; i++) { hThread[i] = CreateThread(NULL, // default security attributes 0, // use default stack size (LPTHREAD_START_ROUTINE) ThreadFunc, // thread function NULL, // no thread function argument 0, // use default creation flags &IDThread); // returns thread identifier // Check the return value for success. if (hThread[i] == NULL) ErrorExit("CreateThread error\n"); } WaitForMultipleObjects(THREADCOUNT, hThread, TRUE, INFINITE); FreeLibrary(hm); return 0; } VOID ErrorExit (LPSTR lpszMessage) { fprintf(stderr, "%s\n", lpszMessage); ExitProcess(0); }


评论列表