liblog4rvcother.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // liblog4rvcother.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "liblog4rvcother.h"
  4. #include <stdio.h>
  5. #if OS_WIN
  6. #include <Psapi.h>
  7. #pragma comment(lib, "Psapi.lib")
  8. #define CUR_PROCESS_NAME "sphost.exe"
  9. static HMODULE gModule = NULL;
  10. #else
  11. #include <string.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #define CUR_PROCESS_NAME "sphost"
  15. #endif
  16. //该版本默认为TRUE
  17. static int IsSphostExe = TRUE;
  18. BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize);
  19. /*!
  20. * @brief detect current process is created by VTM
  21. * @return : TRUE: created by VTM; FALSE: created by vendor
  22. * TODO: need to enforce it for safety.
  23. */
  24. EXTERN_C BOOL IsVTMProcess()
  25. {
  26. if(IsSphostExe == -1) {
  27. char szPath[4096] = {'\0'};
  28. DWORD dwPathSize = 4096;
  29. if(GetCurProcessPath(szPath, dwPathSize)) {
  30. const int pathLen = strlen(szPath);
  31. int index;
  32. for(index=0; index<pathLen; ++index) {
  33. if(szPath[index] >= 'A' && szPath[index] <= 'Z')
  34. szPath[index] = 'a' + (szPath[index] - 'A');
  35. }
  36. char* pos = strstr(szPath, CUR_PROCESS_NAME);
  37. dwPathSize = strlen(szPath);
  38. if(pos != NULL && ((pos-szPath) + strlen(CUR_PROCESS_NAME)) == dwPathSize) {
  39. IsSphostExe = TRUE;
  40. } else {
  41. IsSphostExe = FALSE;
  42. }
  43. } else {
  44. /*If failed, regard it as VTM environment.*/
  45. IsSphostExe = TRUE;
  46. }
  47. }
  48. return IsSphostExe;
  49. }
  50. EXTERN_C BOOL GetCurFileVersion(char szVerion[], DWORD dwVerionSize)
  51. {
  52. #if 0
  53. HRSRC hsrc=FindResourceA(gModule, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
  54. HGLOBAL hgbl = LoadResource(gModule, hsrc);
  55. BYTE *pBt = (BYTE *)LockResource(hgbl);
  56. VS_FIXEDFILEINFO* pFinfo = (VS_FIXEDFILEINFO*)(pBt + 40);
  57. sprintf_s(szVerion, dwVerionSize, "%d.%d.%d.%d",
  58. (pFinfo->dwFileVersionMS >> 16) & 0xFF,
  59. (pFinfo->dwFileVersionMS) & 0xFF,
  60. (pFinfo->dwFileVersionLS >> 16) & 0xFF,
  61. (pFinfo->dwFileVersionLS) & 0xFF);
  62. #endif
  63. sprintf(szVerion, "1.0.0.0");
  64. return TRUE;
  65. }
  66. #if OS_WIN
  67. BOOL APIENTRY DllMain( HMODULE hModule,
  68. DWORD ul_reason_for_call,
  69. LPVOID lpReserved
  70. )
  71. {
  72. switch (ul_reason_for_call)
  73. {
  74. case DLL_PROCESS_ATTACH:
  75. gModule = hModule;
  76. IsVTMProcess();
  77. break;
  78. case DLL_THREAD_ATTACH:
  79. case DLL_THREAD_DETACH:
  80. case DLL_PROCESS_DETACH:
  81. break;
  82. }
  83. return TRUE;
  84. }
  85. BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize)
  86. {
  87. ZeroMemory(szPath, sizeof(char)*dwPathSize);
  88. const DWORD PID = GetCurrentProcessId();
  89. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  90. FALSE, PID);
  91. if (hProcess == NULL) {
  92. return FALSE;
  93. }
  94. if (GetModuleFileNameExA(hProcess, (HMODULE)0, szPath, dwPathSize) == 0) {
  95. printf("GetModuleFileNameExA failed: %d\n", GetLastError());
  96. DWORD dwSize = dwPathSize;
  97. if(QueryFullProcessImageNameA(hProcess, 0, szPath, &dwSize) == 0)
  98. {
  99. printf("QueryFullProcessImageNameA failed: %d\n", GetLastError());
  100. if (!GetProcessImageFileNameA(hProcess, szPath, dwSize)) {
  101. printf("GetProcessImageFileNameA failed: %d\n", GetLastError());
  102. CloseHandle(hProcess);
  103. return FALSE;
  104. }
  105. }
  106. }
  107. CloseHandle(hProcess);
  108. printf("process image name: %s\n", szPath);
  109. return TRUE;
  110. }
  111. #else
  112. BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize)
  113. {
  114. int status;
  115. size_t length;
  116. char path[64];
  117. char buffer[4096];
  118. snprintf(path, 64, "/proc/%d/exe", getpid());
  119. status = readlink(path, buffer, sizeof(buffer));
  120. if (status < 0) {
  121. return FALSE;
  122. }
  123. buffer[status] = '\0';
  124. length = strnlen(buffer, sizeof(buffer));
  125. memset(szPath, '\0', sizeof(szPath[0]) * dwPathSize);
  126. if (length < dwPathSize) {
  127. memcpy(szPath, buffer, length);
  128. szPath[length] = '\0';
  129. return TRUE;
  130. }
  131. memcpy(szPath, buffer, dwPathSize - 1);
  132. szPath[dwPathSize - 1] = '\0';
  133. return FALSE;
  134. }
  135. #endif