123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // liblog4rvcother.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "liblog4rvcother.h"
- #include <stdio.h>
- #if OS_WIN
- #include <Psapi.h>
- #pragma comment(lib, "Psapi.lib")
- #define CUR_PROCESS_NAME "sphost.exe"
- static HMODULE gModule = NULL;
- #else
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define CUR_PROCESS_NAME "sphost"
- #endif
- //该版本默认为TRUE
- static int IsSphostExe = TRUE;
- BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize);
- /*!
- * @brief detect current process is created by VTM
- * @return : TRUE: created by VTM; FALSE: created by vendor
- * TODO: need to enforce it for safety.
- */
- EXTERN_C BOOL IsVTMProcess()
- {
- if(IsSphostExe == -1) {
- char szPath[4096] = {'\0'};
- DWORD dwPathSize = 4096;
- if(GetCurProcessPath(szPath, dwPathSize)) {
- const int pathLen = strlen(szPath);
- int index;
- for(index=0; index<pathLen; ++index) {
- if(szPath[index] >= 'A' && szPath[index] <= 'Z')
- szPath[index] = 'a' + (szPath[index] - 'A');
- }
- char* pos = strstr(szPath, CUR_PROCESS_NAME);
- dwPathSize = strlen(szPath);
- if(pos != NULL && ((pos-szPath) + strlen(CUR_PROCESS_NAME)) == dwPathSize) {
- IsSphostExe = TRUE;
- } else {
- IsSphostExe = FALSE;
- }
- } else {
- /*If failed, regard it as VTM environment.*/
- IsSphostExe = TRUE;
- }
- }
- return IsSphostExe;
- }
- EXTERN_C BOOL GetCurFileVersion(char szVerion[], DWORD dwVerionSize)
- {
- #if 0
- HRSRC hsrc=FindResourceA(gModule, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
- HGLOBAL hgbl = LoadResource(gModule, hsrc);
- BYTE *pBt = (BYTE *)LockResource(hgbl);
- VS_FIXEDFILEINFO* pFinfo = (VS_FIXEDFILEINFO*)(pBt + 40);
- sprintf_s(szVerion, dwVerionSize, "%d.%d.%d.%d",
- (pFinfo->dwFileVersionMS >> 16) & 0xFF,
- (pFinfo->dwFileVersionMS) & 0xFF,
- (pFinfo->dwFileVersionLS >> 16) & 0xFF,
- (pFinfo->dwFileVersionLS) & 0xFF);
- #endif
- sprintf(szVerion, "1.0.0.0");
- return TRUE;
- }
- #if OS_WIN
- BOOL APIENTRY DllMain( HMODULE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- gModule = hModule;
- IsVTMProcess();
- break;
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
- BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize)
- {
- ZeroMemory(szPath, sizeof(char)*dwPathSize);
- const DWORD PID = GetCurrentProcessId();
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
- FALSE, PID);
- if (hProcess == NULL) {
- return FALSE;
- }
-
- if (GetModuleFileNameExA(hProcess, (HMODULE)0, szPath, dwPathSize) == 0) {
- printf("GetModuleFileNameExA failed: %d\n", GetLastError());
- DWORD dwSize = dwPathSize;
- if(QueryFullProcessImageNameA(hProcess, 0, szPath, &dwSize) == 0)
- {
- printf("QueryFullProcessImageNameA failed: %d\n", GetLastError());
- if (!GetProcessImageFileNameA(hProcess, szPath, dwSize)) {
- printf("GetProcessImageFileNameA failed: %d\n", GetLastError());
- CloseHandle(hProcess);
- return FALSE;
- }
- }
- }
-
- CloseHandle(hProcess);
- printf("process image name: %s\n", szPath);
- return TRUE;
- }
- #else
- BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize)
- {
- int status;
- size_t length;
- char path[64];
- char buffer[4096];
- snprintf(path, 64, "/proc/%d/exe", getpid());
- status = readlink(path, buffer, sizeof(buffer));
- if (status < 0) {
- return FALSE;
- }
- buffer[status] = '\0';
- length = strnlen(buffer, sizeof(buffer));
- memset(szPath, '\0', sizeof(szPath[0]) * dwPathSize);
- if (length < dwPathSize) {
- memcpy(szPath, buffer, length);
- szPath[length] = '\0';
- return TRUE;
- }
- memcpy(szPath, buffer, dwPathSize - 1);
- szPath[dwPathSize - 1] = '\0';
- return FALSE;
- }
- #endif
|