main函数(主函数)是c程序下的入口点,WinMain是windows平台下应用程序的入口点(入口函数)

c程序

  1. #include<Windows.h>
  2. #pragma comment(linker,"/INCLUDE:__tls_used")
  3. void print_console(const char* szMsg)
  4. {
  5. HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  6. WriteConsoleA(hStdout, szMsg, strlen(szMsg), NULL, NULL);
  7. }
  8. void NTAPI TLS_CALLBACK1(PVOID DllHandle, DWORD Reason, PVOID Reserved)
  9. {
  10. char szMsg[80] = { 0, };
  11. wsprintfA(szMsg, "TLS_CALLBACK1():DllHandle=%X,Reason=%d\n", DllHandle, Reason);
  12. print_console(szMsg);
  13. }
  14. void NTAPI TLS_CALLBACK2(PVOID DllHandle, DWORD Reason, PVOID Reserved)
  15. {
  16. char szMsg[80] = { 0, };
  17. wsprintfA(szMsg, "TLS_CALLBACK2():DllHandle=%X,Reason=%d\n", DllHandle, Reason);
  18. print_console(szMsg);
  19. }
  20. #pragma data_seg(".CRT$XLX")
  21. PIMAGE_TLS_CALLBACK pTLS_CALLBACKs[] = { TLS_CALLBACK1,TLS_CALLBACK2,0 };
  22. #pragma data_seg()
  23. DWORD WINAPI ThreadProc(LPVOID lPram) {
  24. print_console("ThreadProc() start\n");
  25. print_console("ThreadProc() end\n");
  26. return 0;
  27. }
  28. int main(void)
  29. {
  30. HANDLE hThread = NULL;
  31. print_console("main()start\n");
  32. hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
  33. WaitForSingleObject(hThread, 60 * 1000);
  34. CloseHandle(hThread);
  35. print_console("main() end\n");
  36. system("pause");
  37. return 0;
  38. }

image.png

windows平台程序

TLS.rar
双击打开.dsw文件升级,报错则右键修改下属性
image.png
TLS.zip
image.png