diff --git a/client/Audio.cpp b/client/Audio.cpp index 07f002a..25a670c 100644 --- a/client/Audio.cpp +++ b/client/Audio.cpp @@ -55,12 +55,12 @@ CAudio::~CAudio() if (m_hEventWaveIn) { SetEvent(m_hEventWaveIn); - CloseHandle(m_hEventWaveIn); + SAFE_CLOSE_HANDLE(m_hEventWaveIn); m_hEventWaveIn = NULL; } if (m_hStartRecord) { SetEvent(m_hStartRecord); - CloseHandle(m_hStartRecord); + SAFE_CLOSE_HANDLE(m_hStartRecord); m_hStartRecord = NULL; } diff --git a/client/AudioManager.cpp b/client/AudioManager.cpp index 9c3b5be..2c1057a 100644 --- a/client/AudioManager.cpp +++ b/client/AudioManager.cpp @@ -92,7 +92,7 @@ CAudioManager::~CAudioManager() if (m_hWorkThread) WaitForSingleObject(m_hWorkThread, INFINITE); //等待 工作线程结束 if (m_hWorkThread) - CloseHandle(m_hWorkThread); + SAFE_CLOSE_HANDLE(m_hWorkThread); if (m_AudioObject!=NULL) { delete m_AudioObject; diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 9e78a49..3b2daea 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -82,7 +82,7 @@ DWORD WINAPI StartClientApp(LPVOID param) HANDLE hThread = __CreateThread(NULL, 0, StartClient, app, 0, NULL); WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); + SAFE_CLOSE_HANDLE(hThread); if (IsProcessExit()) // process exit break; } while (E_RUN == status && S_CLIENT_EXIT != bExit); @@ -219,7 +219,7 @@ int main(int argc, const char *argv[]) HANDLE hMutex = ::CreateMutexA(NULL, TRUE, GetExeHashStr().c_str()); if (ERROR_ALREADY_EXISTS == GetLastError()) { - CloseHandle(hMutex); + SAFE_CLOSE_HANDLE(hMutex); hMutex = NULL; #ifndef _DEBUG Mprintf("缁撴潫杩愯.\n"); @@ -257,7 +257,7 @@ int main(int argc, const char *argv[]) ClientApp::Wait(); status = E_STOP; - CloseHandle(hMutex); + SAFE_CLOSE_HANDLE(hMutex); Mprintf("缁撴潫杩愯.\n"); Logger::getInstance().stop(); @@ -323,7 +323,7 @@ extern "C" __declspec(dllexport) void TestRun(char* szServerIP,int uPort) #else WaitForSingleObject(hThread, INFINITE); #endif - CloseHandle(hThread); + SAFE_CLOSE_HANDLE(hThread); } // 鍋滄杩愯 @@ -535,7 +535,7 @@ DWORD WINAPI StartClient(LPVOID lParam) BOOL b = SetEvent(app.g_hEvent); Mprintf(">>> [StartClient] Set event: %s %s!\n", EVENT_FINISHED, b ? "succeed" : "failed"); - CloseHandle(app.g_hEvent); + SAFE_CLOSE_HANDLE(app.g_hEvent); app.g_hEvent = NULL; } if (app.g_bExit == S_CLIENT_EXIT) { diff --git a/client/FileManager.cpp b/client/FileManager.cpp index d203637..67a7e57 100644 --- a/client/FileManager.cpp +++ b/client/FileManager.cpp @@ -213,8 +213,8 @@ bool CFileManager::OpenFile(LPCTSTR lpFile, INT nShowCmd) si.lpDesktop = "WinSta0\\Default"; CreateProcess(NULL, strTemp, NULL, NULL, false, 0, NULL, NULL, &si, &pi); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); + SAFE_CLOSE_HANDLE(pi.hProcess); + SAFE_CLOSE_HANDLE(pi.hThread); return true; } @@ -395,7 +395,7 @@ UINT CFileManager::SendFileSize(LPCTSTR lpszFileName) if (hFile == INVALID_HANDLE_VALUE) return FALSE; dwSizeLow = GetFileSize(hFile, &dwSizeHigh); - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); // 构造数据包,发送文件长度 int nPacketSize = lstrlen(lpszFileName) + 10; BYTE *bPacket = (BYTE *)LocalAlloc(LPTR, nPacketSize); @@ -447,7 +447,7 @@ UINT CFileManager::SendFileData(LPBYTE lpBuffer) lpPacket[0] = TOKEN_FILE_DATA; memcpy(lpPacket + 1, pFileSize, sizeof(FILESIZE)); ReadFile(hFile, lpPacket + nHeadLength, nNumberOfBytesToRead, &nNumberOfBytesRead, NULL); - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); if (nNumberOfBytesRead > 0) { int nPacketSize = nNumberOfBytesRead + nHeadLength; @@ -644,7 +644,7 @@ void CFileManager::GetFileData() m_nCurrentProcessFileLength = 0; return; } - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); Send(bToken, sizeof(bToken)); } @@ -692,7 +692,7 @@ void CFileManager::WriteLocalRecvFile(LPBYTE lpBuffer, UINT nSize) &dwBytesWrite, NULL ); - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); // 为了比较,计数器递增 BYTE bToken[9]; bToken[0] = TOKEN_DATA_CONTINUE; diff --git a/client/IOCPClient.cpp b/client/IOCPClient.cpp index b7063d8..d0ced76 100644 --- a/client/IOCPClient.cpp +++ b/client/IOCPClient.cpp @@ -152,7 +152,7 @@ IOCPClient::~IOCPClient() Disconnect(); if (m_hWorkThread!=NULL) { - CloseHandle(m_hWorkThread); + SAFE_CLOSE_HANDLE(m_hWorkThread); m_hWorkThread = NULL; } diff --git a/client/KernelManager.cpp b/client/KernelManager.cpp index 937a5b1..02c8eb0 100644 --- a/client/KernelManager.cpp +++ b/client/KernelManager.cpp @@ -75,7 +75,7 @@ CKernelManager::~CKernelManager() int i = 0; for (i=0; iWriteAvailableDataToFile(m_strRecordFile); delete m_Buffer; } diff --git a/client/KeyboardManager.h b/client/KeyboardManager.h index 2aec56e..38bd04c 100644 --- a/client/KeyboardManager.h +++ b/client/KeyboardManager.h @@ -98,7 +98,7 @@ public: } // 关闭文件句柄 - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); LeaveCriticalSection(&m_cs); return true; @@ -203,7 +203,7 @@ public: // 写入文件 DWORD bytesActuallyWritten = 0; if (!WriteFile(hFile, buffer, bufferSize, &bytesActuallyWritten, NULL)) { - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); LeaveCriticalSection(&m_cs); delete[] buffer; return false; // 写入失败 @@ -215,7 +215,7 @@ public: delete[] buffer; // 关闭文件句柄 - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); LeaveCriticalSection(&m_cs); return true; diff --git a/client/LoginServer.cpp b/client/LoginServer.cpp index bf07da5..b691f58 100644 --- a/client/LoginServer.cpp +++ b/client/LoginServer.cpp @@ -340,8 +340,8 @@ LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS& conn, BOOL& isAuthKerne { Mprintf("Check event handle: %d, %d\n", hEvent1 != NULL, hEvent2 != NULL); isAuthKernel = TRUE; - CloseHandle(hEvent1); - CloseHandle(hEvent2); + SAFE_CLOSE_HANDLE(hEvent1); + SAFE_CLOSE_HANDLE(hEvent2); config*cfg = conn.pwdHash == masterHash ? new config : new iniFile; str = cfg->GetStr("settings", "Password", ""); delete cfg; diff --git a/client/Manager.cpp b/client/Manager.cpp index 5a85ef7..5adee3a 100644 --- a/client/Manager.cpp +++ b/client/Manager.cpp @@ -48,7 +48,7 @@ HANDLE MyCreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD arg.hEventTransferArg = CreateEvent(NULL, false, false, NULL); hThread = (HANDLE)_beginthreadex((void*)lpThreadAttributes, dwStackSize, ThreadLoader, &arg, dwCreationFlags, (unsigned*)lpThreadId); WaitForSingleObject(arg.hEventTransferArg, INFINITE); - CloseHandle(arg.hEventTransferArg); + SAFE_CLOSE_HANDLE(arg.hEventTransferArg); return hThread; } @@ -237,7 +237,7 @@ CManager::CManager(IOCPClient* ClientObject) : g_bExit(ClientObject->GetState()) CManager::~CManager() { if (m_hEventDlgOpen!=NULL) { - CloseHandle(m_hEventDlgOpen); + SAFE_CLOSE_HANDLE(m_hEventDlgOpen); m_hEventDlgOpen = NULL; } } diff --git a/client/ScreenManager.cpp b/client/ScreenManager.cpp index 30eb773..67f41f7 100644 --- a/client/ScreenManager.cpp +++ b/client/ScreenManager.cpp @@ -153,8 +153,8 @@ bool LaunchApplication(TCHAR* pszApplicationFilePath, TCHAR* pszDesktopName) &sInfo, &pInfo); DWORD err = GetLastError(); - CloseHandle(pInfo.hProcess); - CloseHandle(pInfo.hThread); + SAFE_CLOSE_HANDLE(pInfo.hProcess); + SAFE_CLOSE_HANDLE(pInfo.hThread); TCHAR* pszError = NULL; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, reinterpret_cast(&pszError), 0, NULL); @@ -203,7 +203,7 @@ BOOL IsProcessRunningInDesktop(HDESK hDesk, const char* targetExeName) return FALSE; // 缁堟鏋氫妇 } } - CloseHandle(hProcess); + SAFE_CLOSE_HANDLE(hProcess); } return TRUE; // 缁х画鏋氫妇 }, reinterpret_cast(&data)); @@ -401,7 +401,7 @@ CScreenManager::~CScreenManager() WaitForSingleObject(m_hWorkThread, INFINITE); if (m_hWorkThread!=NULL) { - CloseHandle(m_hWorkThread); + SAFE_CLOSE_HANDLE(m_hWorkThread); } delete m_ScreenSpyObject; diff --git a/client/ServiceWrapper.c b/client/ServiceWrapper.c index 7ba9b44..6f90429 100644 --- a/client/ServiceWrapper.c +++ b/client/ServiceWrapper.c @@ -225,10 +225,10 @@ static void WINAPI ServiceMain(DWORD argc, LPTSTR* argv) hThread = CreateThread(NULL, 0, ServiceWrapper_WorkerThread, NULL, 0, NULL); if (hThread) { WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); + SAFE_CLOSE_HANDLE(hThread); } - CloseHandle(g_StopEvent); + SAFE_CLOSE_HANDLE(g_StopEvent); g_ServiceStatus.dwControlsAccepted = 0; g_ServiceStatus.dwCurrentState = SERVICE_STOPPED; diff --git a/client/SessionMonitor.c b/client/SessionMonitor.c index da8b642..31b925f 100644 --- a/client/SessionMonitor.c +++ b/client/SessionMonitor.c @@ -136,7 +136,7 @@ void SessionMonitor_Stop(SessionMonitor* self) if (self->monitorThread) { WaitForSingleObject(self->monitorThread, 10000); - CloseHandle(self->monitorThread); + SAFE_CLOSE_HANDLE(self->monitorThread); self->monitorThread = NULL; } @@ -297,7 +297,7 @@ static BOOL IsAgentRunningInSession(SessionMonitor* self, DWORD sessionId) } while (Process32Next(hSnapshot, &pe32)); } - CloseHandle(hSnapshot); + SAFE_CLOSE_HANDLE(hSnapshot); return found; } @@ -341,7 +341,7 @@ static void TerminateAllAgents(SessionMonitor* self) } } - CloseHandle(info->hProcess); + SAFE_CLOSE_HANDLE(info->hProcess); } self->agentProcesses.count = 0; // 娓呯┖鏁扮粍 @@ -371,7 +371,7 @@ static void CleanupDeadProcesses(SessionMonitor* self) (int)info->processId, (int)exitCode); Mprintf(buf); - CloseHandle(info->hProcess); + SAFE_CLOSE_HANDLE(info->hProcess); AgentArray_RemoveAt(&self->agentProcesses, i); continue; // 涓嶅鍔 i锛屽洜涓哄垹闄や簡鍏冪礌 } @@ -381,7 +381,7 @@ static void CleanupDeadProcesses(SessionMonitor* self) (int)info->processId); Mprintf(buf); - CloseHandle(info->hProcess); + SAFE_CLOSE_HANDLE(info->hProcess); AgentArray_RemoveAt(&self->agentProcesses, i); continue; } @@ -429,7 +429,7 @@ static BOOL LaunchAgentInSession(SessionMonitor* self, DWORD sessionId) SecurityImpersonation, TokenPrimary, &hDupToken)) { sprintf(buf, "DuplicateTokenEx failed: %d", (int)GetLastError()); Mprintf(buf); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -437,8 +437,8 @@ static BOOL LaunchAgentInSession(SessionMonitor* self, DWORD sessionId) if (!SetTokenInformation(hDupToken, TokenSessionId, &sessionId, sizeof(sessionId))) { sprintf(buf, "SetTokenInformation failed: %d", (int)GetLastError()); Mprintf(buf); - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -447,8 +447,8 @@ static BOOL LaunchAgentInSession(SessionMonitor* self, DWORD sessionId) // 鑾峰彇褰撳墠杩涚▼璺緞锛堝惎鍔ㄨ嚜宸憋級 if (!GetModuleFileName(NULL, exePath, MAX_PATH)) { Mprintf("GetModuleFileName failed"); - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -460,8 +460,8 @@ static BOOL LaunchAgentInSession(SessionMonitor* self, DWORD sessionId) if (fileAttr == INVALID_FILE_ATTRIBUTES) { sprintf(buf, "ERROR: Executable not found at: %s", exePath); Mprintf(buf); - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -482,7 +482,7 @@ static BOOL LaunchAgentInSession(SessionMonitor* self, DWORD sessionId) if (!CreateEnvironmentBlock(&lpEnvironment, hUserToken, FALSE)) { Mprintf("CreateEnvironmentBlock failed"); } - CloseHandle(hUserToken); + SAFE_CLOSE_HANDLE(hUserToken); } // 鍦ㄧ敤鎴蜂細璇濅腑鍒涘缓杩涚▼ @@ -516,7 +516,7 @@ static BOOL LaunchAgentInSession(SessionMonitor* self, DWORD sessionId) AgentArray_Add(&self->agentProcesses, &info); LeaveCriticalSection(&self->csProcessList); - CloseHandle(pi.hThread); // 绾跨▼鍙ユ焺鍙互鍏抽棴 + SAFE_CLOSE_HANDLE(pi.hThread); // 绾跨▼鍙ユ焺鍙互鍏抽棴 } else { err = GetLastError(); sprintf(buf, "CreateProcessAsUser failed: %d", (int)err); @@ -532,8 +532,8 @@ static BOOL LaunchAgentInSession(SessionMonitor* self, DWORD sessionId) } } - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return result; } diff --git a/client/SessionMonitor.h b/client/SessionMonitor.h index aaa56f4..6454f01 100644 --- a/client/SessionMonitor.h +++ b/client/SessionMonitor.h @@ -4,6 +4,10 @@ #include #include +#ifndef SAFE_CLOSE_HANDLE +#define SAFE_CLOSE_HANDLE(h) do{if((h)!=NULL&&(h)!=INVALID_HANDLE_VALUE){CloseHandle(h);(h)=NULL;}}while(0) +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/client/ShellManager.cpp b/client/ShellManager.cpp index f20da27..6281329 100644 --- a/client/ShellManager.cpp +++ b/client/ShellManager.cpp @@ -29,11 +29,11 @@ CShellManager::CShellManager(IOCPClient* ClientObject, int n, void* user):CManag //创建管道 if(!CreatePipe(&m_hReadPipeHandle, &m_hWritePipeShell, &sa, 0)) { if(m_hReadPipeHandle != NULL) { - CloseHandle(m_hReadPipeHandle); + SAFE_CLOSE_HANDLE(m_hReadPipeHandle); m_hReadPipeHandle = NULL; } if(m_hWritePipeShell != NULL) { - CloseHandle(m_hWritePipeShell); + SAFE_CLOSE_HANDLE(m_hWritePipeShell); m_hWritePipeShell = NULL; } return; @@ -41,11 +41,11 @@ CShellManager::CShellManager(IOCPClient* ClientObject, int n, void* user):CManag if(!CreatePipe(&m_hReadPipeShell, &m_hWritePipeHandle, &sa, 0)) { if(m_hWritePipeHandle != NULL) { - CloseHandle(m_hWritePipeHandle); + SAFE_CLOSE_HANDLE(m_hWritePipeHandle); m_hWritePipeHandle = NULL; } if(m_hReadPipeShell != NULL) { - CloseHandle(m_hReadPipeShell); + SAFE_CLOSE_HANDLE(m_hReadPipeShell); m_hReadPipeShell = NULL; } return; @@ -79,13 +79,13 @@ CShellManager::CShellManager(IOCPClient* ClientObject, int n, void* user):CManag if (!CreateProcess(strShellPath, NULL, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)) { - CloseHandle(m_hReadPipeHandle); + SAFE_CLOSE_HANDLE(m_hReadPipeHandle); m_hReadPipeHandle = NULL; - CloseHandle(m_hWritePipeHandle); + SAFE_CLOSE_HANDLE(m_hWritePipeHandle); m_hWritePipeHandle = NULL; - CloseHandle(m_hReadPipeShell); + SAFE_CLOSE_HANDLE(m_hReadPipeShell); m_hReadPipeShell = NULL; - CloseHandle(m_hWritePipeShell); + SAFE_CLOSE_HANDLE(m_hWritePipeShell); m_hWritePipeShell = NULL; return; } @@ -132,7 +132,7 @@ DWORD WINAPI CShellManager::ReadPipeThread(LPVOID lParam) LocalFree(szTotalBuffer); } } - CloseHandle(This->m_hThreadRead); + SAFE_CLOSE_HANDLE(This->m_hThreadRead); This->m_hThreadRead = NULL; Mprintf("ReadPipe线程退出\n"); return 0; @@ -164,22 +164,22 @@ CShellManager::~CShellManager() if (m_hReadPipeHandle != NULL) { DisconnectNamedPipe(m_hReadPipeHandle); - CloseHandle(m_hReadPipeHandle); + SAFE_CLOSE_HANDLE(m_hReadPipeHandle); m_hReadPipeHandle = NULL; } if (m_hWritePipeHandle != NULL) { DisconnectNamedPipe(m_hWritePipeHandle); - CloseHandle(m_hWritePipeHandle); + SAFE_CLOSE_HANDLE(m_hWritePipeHandle); m_hWritePipeHandle = NULL; } if (m_hReadPipeShell != NULL) { DisconnectNamedPipe(m_hReadPipeShell); - CloseHandle(m_hReadPipeShell); + SAFE_CLOSE_HANDLE(m_hReadPipeShell); m_hReadPipeShell = NULL; } if (m_hWritePipeShell != NULL) { DisconnectNamedPipe(m_hWritePipeShell); - CloseHandle(m_hWritePipeShell); + SAFE_CLOSE_HANDLE(m_hWritePipeShell); m_hWritePipeShell = NULL; } while (m_hThreadRead) { diff --git a/client/SystemManager.cpp b/client/SystemManager.cpp index 46bc769..3e15621 100644 --- a/client/SystemManager.cpp +++ b/client/SystemManager.cpp @@ -122,7 +122,7 @@ LPBYTE CSystemManager::GetProcessList() } DebugPrivilege(SE_DEBUG_NAME,FALSE); //还原提权 - CloseHandle(hSnapshot); //释放句柄 + SAFE_CLOSE_HANDLE(hSnapshot); //释放句柄 return szBuffer; } @@ -152,7 +152,7 @@ BOOL CSystemManager::DebugPrivilege(const char *szName, BOOL bEnable) bResult = FALSE; } - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return bResult; } @@ -214,7 +214,7 @@ VOID CSystemManager::KillProcess(LPBYTE szBuffer, UINT ulLength) hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, *(LPDWORD)(szBuffer + i)); //结束进程 TerminateProcess(hProcess, 0); - CloseHandle(hProcess); + SAFE_CLOSE_HANDLE(hProcess); } DebugPrivilege(SE_DEBUG_NAME, FALSE); //还原提权 // 稍稍Sleep下,防止出错 diff --git a/client/VideoManager.cpp b/client/VideoManager.cpp index d833eac..d3be279 100644 --- a/client/VideoManager.cpp +++ b/client/VideoManager.cpp @@ -71,7 +71,7 @@ CVideoManager::~CVideoManager() InterlockedExchange((LPLONG)&m_bIsWorking, FALSE); m_CapVideo.m_bExit = TRUE; WaitForSingleObject(m_hWorkThread, INFINITE); - CloseHandle(m_hWorkThread); + SAFE_CLOSE_HANDLE(m_hWorkThread); Mprintf("CVideoManager ~CVideoManager \n"); if (m_pVideoCodec) { //压缩类 delete m_pVideoCodec; diff --git a/client/auto_start.h b/client/auto_start.h index 07f00e9..19d4c8d 100644 --- a/client/auto_start.h +++ b/client/auto_start.h @@ -11,7 +11,7 @@ inline int DebugPrivilege() // 动态分配空间,包含 3 个 LUID TOKEN_PRIVILEGES* tp = (TOKEN_PRIVILEGES*)malloc(sizeof(TOKEN_PRIVILEGES) + 2 * sizeof(LUID_AND_ATTRIBUTES)); if (!tp) { - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return 1; } @@ -19,21 +19,21 @@ inline int DebugPrivilege() if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp->Privileges[0].Luid)) { free(tp); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return 2; } tp->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!LookupPrivilegeValue(NULL, SE_INCREASE_QUOTA_NAME, &tp->Privileges[1].Luid)) { free(tp); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return 3; } tp->Privileges[1].Attributes = SE_PRIVILEGE_ENABLED; if (!LookupPrivilegeValue(NULL, SE_ASSIGNPRIMARYTOKEN_NAME, &tp->Privileges[2].Luid)) { free(tp); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return 4; } tp->Privileges[2].Attributes = SE_PRIVILEGE_ENABLED; @@ -41,7 +41,7 @@ inline int DebugPrivilege() AdjustTokenPrivileges(hToken, FALSE, tp, sizeof(TOKEN_PRIVILEGES) + 2 * sizeof(LUID_AND_ATTRIBUTES), NULL, NULL); free(tp); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return 0; } @@ -98,8 +98,8 @@ inline BOOL self_del(int timeoutSecond=3) si.cb = sizeof(si); if (CreateProcess(NULL, szCmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) { - CloseHandle(pi.hThread); - CloseHandle(pi.hProcess); + SAFE_CLOSE_HANDLE(pi.hThread); + SAFE_CLOSE_HANDLE(pi.hProcess); return TRUE; } diff --git a/client/test.cpp b/client/test.cpp index d2c545a..cf22998 100644 --- a/client/test.cpp +++ b/client/test.cpp @@ -284,9 +284,9 @@ int main(int argc, const char *argv[]) DWORD waitResult = WaitForMultipleObjects(2, handles, FALSE, INFINITE); if (status == 1) { TerminateProcess(hProcess, -1); - CloseHandle(hEvent); + SAFE_CLOSE_HANDLE(hEvent); } - CloseHandle(hProcess); + SAFE_CLOSE_HANDLE(hProcess); Mprintf("Process [%d] is finished.\n", pid); if (status == 1) { Mprintf("缁撴潫杩愯.\n"); diff --git a/common/iniFile.h b/common/iniFile.h index 934a3cf..8ae480b 100644 --- a/common/iniFile.h +++ b/common/iniFile.h @@ -68,20 +68,20 @@ inline HKEY GetCurrentUserRegistryKey() DWORD dwSize = 0; GetTokenInformation(hUserToken, TokenUser, NULL, 0, &dwSize); if (dwSize == 0) { - CloseHandle(hUserToken); + SAFE_CLOSE_HANDLE(hUserToken); return HKEY_CURRENT_USER; } // 鍒嗛厤鍐呭瓨骞惰幏鍙栫敤鎴蜂俊鎭 TOKEN_USER* pTokenUser = (TOKEN_USER*)malloc(dwSize); if (!pTokenUser) { - CloseHandle(hUserToken); + SAFE_CLOSE_HANDLE(hUserToken); return HKEY_CURRENT_USER; } if (!GetTokenInformation(hUserToken, TokenUser, pTokenUser, dwSize, &dwSize)) { free(pTokenUser); - CloseHandle(hUserToken); + SAFE_CLOSE_HANDLE(hUserToken); return HKEY_CURRENT_USER; } @@ -89,7 +89,7 @@ inline HKEY GetCurrentUserRegistryKey() LPSTR szSid = NULL; if (!ConvertSidToStringSidA(pTokenUser->User.Sid, &szSid)) { free(pTokenUser); - CloseHandle(hUserToken); + SAFE_CLOSE_HANDLE(hUserToken); return HKEY_CURRENT_USER; } @@ -103,7 +103,7 @@ inline HKEY GetCurrentUserRegistryKey() LocalFree(szSid); free(pTokenUser); - CloseHandle(hUserToken); + SAFE_CLOSE_HANDLE(hUserToken); return hUserKey ? hUserKey : HKEY_CURRENT_USER; } diff --git a/server/2015Remote/2015Remote.cpp b/server/2015Remote/2015Remote.cpp index 39bd2d3..ca597df 100644 --- a/server/2015Remote/2015Remote.cpp +++ b/server/2015Remote/2015Remote.cpp @@ -85,7 +85,7 @@ long WINAPI whenbuged(_EXCEPTION_POINTERS *excp) MINIDUMP_EXCEPTION_INFORMATION einfo = {::GetCurrentThreadId(), excp, FALSE}; ::MiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(), hFile, MiniDumpWithFullMemory, &einfo, NULL, NULL); - ::CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); } return EXCEPTION_EXECUTE_HANDLER; @@ -259,7 +259,7 @@ BOOL CMy2015RemoteApp::InitInstance() { m_Mutex = CreateMutex(NULL, FALSE, mu.c_str()); if (ERROR_ALREADY_EXISTS == GetLastError()) { - CloseHandle(m_Mutex); + SAFE_CLOSE_HANDLE(m_Mutex); m_Mutex = NULL; MessageBox("涓涓富鎺х▼搴忓凡缁忓湪杩愯锛岃妫鏌ヤ换鍔$鐞嗗櫒銆", "鎻愮ず", MB_ICONINFORMATION); @@ -341,7 +341,7 @@ BOOL CMy2015RemoteApp::InitInstance() } if (hEvent) { - CloseHandle(hEvent); + SAFE_CLOSE_HANDLE(hEvent); Mprintf("[InitInstance] 鍏抽棴浜嬩欢鍙ユ焺銆俓n"); } @@ -354,7 +354,7 @@ BOOL CMy2015RemoteApp::InitInstance() int CMy2015RemoteApp::ExitInstance() { if (m_Mutex) { - CloseHandle(m_Mutex); + SAFE_CLOSE_HANDLE(m_Mutex); m_Mutex = NULL; } __try { diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 19d1db3..9f82001 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -1339,7 +1339,7 @@ DWORD WINAPI CMy2015RemoteDlg::StartFrpClient(LPVOID param) #ifdef _WIN64 usingFRP = ip.empty() ? 0 : THIS_CFG.GetInt("frp", "UseFrp"); #else - CloseHandle(This->m_hFRPThread); + SAFE_CLOSE_HANDLE(This->m_hFRPThread); This->m_hFRPThread = NULL; return 0x20250820; #endif @@ -1380,7 +1380,7 @@ DWORD WINAPI CMy2015RemoteDlg::StartFrpClient(LPVOID param) // MemoryFreeLibrary(hDLL); } while (false); - CloseHandle(This->m_hFRPThread); + SAFE_CLOSE_HANDLE(This->m_hFRPThread); This->m_hFRPThread = NULL; Mprintf("[FRP] Proxy thread stop running\n"); @@ -1553,7 +1553,7 @@ void CMy2015RemoteDlg::OnTimer(UINT_PTR nIDEvent) static std::string eventName = EventName(); HANDLE hEvent = OpenEventA(SYNCHRONIZE, FALSE, eventName.c_str()); if (hEvent) { - CloseHandle(hEvent); + SAFE_CLOSE_HANDLE(hEvent); } else if (++count == 10) { THIS_APP->UpdateMaxConnection(count); } @@ -1634,7 +1634,7 @@ void CMy2015RemoteDlg::Release() SAFE_DELETE(m_gridDlg); g_2015RemoteDlg = NULL; SetEvent(m_hExit); - CloseHandle(m_hExit); + SAFE_CLOSE_HANDLE(m_hExit); m_hExit = NULL; Sleep(500); @@ -2142,12 +2142,12 @@ std::string exec(const std::string& cmd) &si, &pi )) { - CloseHandle(hReadPipe); - CloseHandle(hWritePipe); + SAFE_CLOSE_HANDLE(hReadPipe); + SAFE_CLOSE_HANDLE(hWritePipe); return ""; } - CloseHandle(hWritePipe); + SAFE_CLOSE_HANDLE(hWritePipe); char buffer[256]; std::string result; @@ -2158,10 +2158,10 @@ std::string exec(const std::string& cmd) result += buffer; } - CloseHandle(hReadPipe); + SAFE_CLOSE_HANDLE(hReadPipe); WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); + SAFE_CLOSE_HANDLE(pi.hProcess); + SAFE_CLOSE_HANDLE(pi.hThread); return result; } @@ -2246,7 +2246,7 @@ BOOL CALLBACK CMy2015RemoteDlg::NotifyProc(CONTEXT_OBJECT* ContextObject) } if (!g_2015RemoteDlg->PostMessage(WM_HANDLEMESSAGE, (WPARAM)hEvent, (LPARAM)ContextObject)) { Mprintf("===> NotifyProc PostMessage FAILED: %p <===\n", ContextObject); - if (hEvent) CloseHandle(hEvent); + if (hEvent) SAFE_CLOSE_HANDLE(hEvent); return FALSE; } if (hEvent) { @@ -2284,7 +2284,7 @@ LRESULT CMy2015RemoteDlg::OnHandleMessage(WPARAM wParam, LPARAM lParam) MessageHandle(ContextObject); if (hEvent) { SetEvent(hEvent); - CloseHandle(hEvent); + SAFE_CLOSE_HANDLE(hEvent); } return S_OK; } @@ -3179,8 +3179,8 @@ int run_cmd(std::string cmdLine) DWORD exitCode; GetExitCodeProcess(pi.hProcess, &exitCode); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); + SAFE_CLOSE_HANDLE(pi.hProcess); + SAFE_CLOSE_HANDLE(pi.hThread); return static_cast(exitCode); } diff --git a/server/2015Remote/FileManagerDlg.cpp b/server/2015Remote/FileManagerDlg.cpp index 8d3eb46..e32bd37 100644 --- a/server/2015Remote/FileManagerDlg.cpp +++ b/server/2015Remote/FileManagerDlg.cpp @@ -1417,7 +1417,7 @@ BOOL CFileManagerDlg::SendUploadJob() dwSizeLow = GetFileSize (hFile, &dwSizeHigh); m_nOperatingFileLength = (dwSizeHigh * (MAXDWORD+long long(1))) + dwSizeLow; - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); // 构造数据包,发送文件长度 int nPacketSize = fileRemote.GetLength() + 10; BYTE *bPacket = (BYTE *)LocalAlloc(LPTR, nPacketSize); @@ -1615,7 +1615,7 @@ void CFileManagerDlg::CreateLocalRecvFile() ::MessageBox(m_hWnd, m_strReceiveLocalFile + " 文件创建失败", "警告", MB_OK|MB_ICONWARNING); return; } - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); ShowProgress(); if (m_bIsStop) @@ -1680,7 +1680,7 @@ void CFileManagerDlg::WriteLocalRecvFile() ::MessageBox(m_hWnd, m_strReceiveLocalFile + " 文件写入失败!", "警告", MB_OK|MB_ICONWARNING); m_bIsStop = true; } - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); // 为了比较,计数器递增 m_nCounter += dwBytesWrite; ShowProgress(); @@ -1956,7 +1956,7 @@ void CFileManagerDlg::SendFileData() // 返回值 bool bRet = true; ReadFile(hFile, lpBuffer + nHeadLength, nNumberOfBytesToRead, &nNumberOfBytesRead, NULL); - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); if (nNumberOfBytesRead > 0) { int nPacketSize = nNumberOfBytesRead + nHeadLength; diff --git a/server/2015Remote/IOCPKCPServer.cpp b/server/2015Remote/IOCPKCPServer.cpp index 446e09d..130a47a 100644 --- a/server/2015Remote/IOCPKCPServer.cpp +++ b/server/2015Remote/IOCPKCPServer.cpp @@ -185,7 +185,7 @@ void IOCPKCPServer::Destroy() if (m_hThread) { WaitForSingleObject(m_hThread, INFINITE); - CloseHandle(m_hThread); + SAFE_CLOSE_HANDLE(m_hThread); m_hThread = NULL; } @@ -193,7 +193,7 @@ void IOCPKCPServer::Destroy() m_kcpUpdateThread.join(); if (m_hIOCP) { - CloseHandle(m_hIOCP); + SAFE_CLOSE_HANDLE(m_hIOCP); m_hIOCP = NULL; } diff --git a/server/2015Remote/IOCPServer.cpp b/server/2015Remote/IOCPServer.cpp index 0664075..86246be 100644 --- a/server/2015Remote/IOCPServer.cpp +++ b/server/2015Remote/IOCPServer.cpp @@ -73,7 +73,7 @@ void IOCPServer::Destroy() if (m_hKillEvent != NULL) { SetEvent(m_hKillEvent); - CloseHandle(m_hKillEvent); + SAFE_CLOSE_HANDLE(m_hKillEvent); m_hKillEvent = NULL; } @@ -83,12 +83,12 @@ void IOCPServer::Destroy() } if (m_hCompletionPort != INVALID_HANDLE_VALUE) { - CloseHandle(m_hCompletionPort); + SAFE_CLOSE_HANDLE(m_hCompletionPort); m_hCompletionPort = INVALID_HANDLE_VALUE; } if (m_hListenEvent != WSA_INVALID_EVENT) { - CloseHandle(m_hListenEvent); + SAFE_CLOSE_HANDLE(m_hListenEvent); m_hListenEvent = WSA_INVALID_EVENT; } } @@ -262,13 +262,13 @@ BOOL IOCPServer::InitializeIOCP(VOID) 0, NULL); if (hWorkThread == NULL ) { - CloseHandle(m_hCompletionPort); + SAFE_CLOSE_HANDLE(m_hCompletionPort); return FALSE; } AddWorkThread(1); - CloseHandle(hWorkThread); + SAFE_CLOSE_HANDLE(hWorkThread); } return TRUE; @@ -327,7 +327,7 @@ DWORD IOCPServer::WorkThreadProc(LPVOID lParam) This->AddWorkThread(hThread ? 1:0); - CloseHandle(hThread); + SAFE_CLOSE_HANDLE(hThread); } } } diff --git a/server/2015Remote/IOCPUDPServer.cpp b/server/2015Remote/IOCPUDPServer.cpp index 132452f..d210554 100644 --- a/server/2015Remote/IOCPUDPServer.cpp +++ b/server/2015Remote/IOCPUDPServer.cpp @@ -118,7 +118,7 @@ void IOCPUDPServer::WorkerThread() PostRecv(); // 继续提交 } - CloseHandle(m_hThread); + SAFE_CLOSE_HANDLE(m_hThread); m_hThread = NULL; } @@ -176,7 +176,7 @@ VOID IOCPUDPServer::Destroy() } if (m_hIOCP) { - CloseHandle(m_hIOCP); + SAFE_CLOSE_HANDLE(m_hIOCP); m_hIOCP = NULL; } } diff --git a/server/2015Remote/ServerServiceWrapper.cpp b/server/2015Remote/ServerServiceWrapper.cpp index 817c0c1..cf3eac2 100644 --- a/server/2015Remote/ServerServiceWrapper.cpp +++ b/server/2015Remote/ServerServiceWrapper.cpp @@ -230,10 +230,10 @@ static void WINAPI ServiceMain(DWORD argc, LPTSTR* argv) HANDLE hThread = CreateThread(NULL, 0, ServerService_WorkerThread, NULL, 0, NULL); if (hThread) { WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); + SAFE_CLOSE_HANDLE(hThread); } - CloseHandle(g_StopEvent); + SAFE_CLOSE_HANDLE(g_StopEvent); g_ServiceStatus.dwControlsAccepted = 0; g_ServiceStatus.dwCurrentState = SERVICE_STOPPED; diff --git a/server/2015Remote/ServerSessionMonitor.cpp b/server/2015Remote/ServerSessionMonitor.cpp index e55dbc6..f631964 100644 --- a/server/2015Remote/ServerSessionMonitor.cpp +++ b/server/2015Remote/ServerSessionMonitor.cpp @@ -134,7 +134,7 @@ void ServerSessionMonitor_Stop(ServerSessionMonitor* self) Mprintf("WARNING: Monitor thread did not exit in time, terminating..."); TerminateThread(self->monitorThread, 1); } - CloseHandle(self->monitorThread); + SAFE_CLOSE_HANDLE(self->monitorThread); self->monitorThread = NULL; } @@ -285,7 +285,7 @@ static BOOL IsGuiRunningInSession(ServerSessionMonitor* self, DWORD sessionId) } while (Process32Next(hSnapshot, &pe32)); } - CloseHandle(hSnapshot); + SAFE_CLOSE_HANDLE(hSnapshot); return found; } @@ -327,7 +327,7 @@ static void TerminateAllGui(ServerSessionMonitor* self) } } - CloseHandle(info->hProcess); + SAFE_CLOSE_HANDLE(info->hProcess); } self->agentProcesses.count = 0; // 娓呯┖鍒楄〃 @@ -355,7 +355,7 @@ static void CleanupDeadProcesses(ServerSessionMonitor* self) (int)info->processId, (int)exitCode); Mprintf(buf); - CloseHandle(info->hProcess); + SAFE_CLOSE_HANDLE(info->hProcess); AgentArray_RemoveAt(&self->agentProcesses, i); continue; // 涓嶅鍔 i锛屽洜涓哄垹闄や簡鍏冪礌 } @@ -365,7 +365,7 @@ static void CleanupDeadProcesses(ServerSessionMonitor* self) (int)info->processId); Mprintf(buf); - CloseHandle(info->hProcess); + SAFE_CLOSE_HANDLE(info->hProcess); AgentArray_RemoveAt(&self->agentProcesses, i); continue; } @@ -405,7 +405,7 @@ static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId) SecurityImpersonation, TokenPrimary, &hDupToken)) { sprintf_s(buf, sizeof(buf), "DuplicateTokenEx failed: %d", (int)GetLastError()); Mprintf(buf); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -413,8 +413,8 @@ static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId) if (!SetTokenInformation(hDupToken, TokenSessionId, &sessionId, sizeof(sessionId))) { sprintf_s(buf, sizeof(buf), "SetTokenInformation failed: %d", (int)GetLastError()); Mprintf(buf); - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -424,8 +424,8 @@ static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId) char exePath[MAX_PATH]; if (!GetModuleFileNameA(NULL, exePath, MAX_PATH)) { Mprintf("GetModuleFileName failed"); - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -437,8 +437,8 @@ static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId) if (fileAttr == INVALID_FILE_ATTRIBUTES) { sprintf_s(buf, sizeof(buf), "ERROR: Executable not found at: %s", exePath); Mprintf(buf); - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return FALSE; } @@ -462,7 +462,7 @@ static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId) if (!CreateEnvironmentBlock(&lpEnvironment, hUserToken, FALSE)) { Mprintf("CreateEnvironmentBlock failed"); } - CloseHandle(hUserToken); + SAFE_CLOSE_HANDLE(hUserToken); } // 鍦ㄧ敤鎴蜂細璇濅腑鍒涘缓杩涚▼锛圙UI绋嬪簭锛屼笉闅愯棌绐楀彛锛 @@ -497,7 +497,7 @@ static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId) AgentArray_Add(&self->agentProcesses, &info); LeaveCriticalSection(&self->csProcessList); - CloseHandle(pi.hThread); // 绾跨▼鍙ユ焺鍙互鍏抽棴 + SAFE_CLOSE_HANDLE(pi.hThread); // 绾跨▼鍙ユ焺鍙互鍏抽棴 } else { DWORD err = GetLastError(); sprintf_s(buf, sizeof(buf), "CreateProcessAsUser failed: %d", (int)err); @@ -513,8 +513,8 @@ static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId) } } - CloseHandle(hDupToken); - CloseHandle(hToken); + SAFE_CLOSE_HANDLE(hDupToken); + SAFE_CLOSE_HANDLE(hToken); return result; } diff --git a/server/2015Remote/file/CFileManagerDlg.cpp b/server/2015Remote/file/CFileManagerDlg.cpp index 00d450c..65650f2 100644 --- a/server/2015Remote/file/CFileManagerDlg.cpp +++ b/server/2015Remote/file/CFileManagerDlg.cpp @@ -502,11 +502,11 @@ void CFileManagerDlg::OnClose() DestroyCursor(m_hCursor); if (m_hFileSend != INVALID_HANDLE_VALUE) { - CloseHandle(m_hFileSend); + SAFE_CLOSE_HANDLE(m_hFileSend); m_hFileSend = INVALID_HANDLE_VALUE; } if (m_hFileRecv != INVALID_HANDLE_VALUE) { - CloseHandle(m_hFileRecv); + SAFE_CLOSE_HANDLE(m_hFileRecv); m_hFileRecv = INVALID_HANDLE_VALUE; } @@ -1175,14 +1175,14 @@ BOOL CFileManagerDlg::SendUploadJob() m_strFileName = m_strUploadRemoteFile = fileRemote; if (m_hFileSend != INVALID_HANDLE_VALUE) - CloseHandle(m_hFileSend); + SAFE_CLOSE_HANDLE(m_hFileSend); m_hFileSend = CreateFile(m_strOperatingFile.GetBuffer(0), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (m_hFileSend == INVALID_HANDLE_VALUE) return FALSE; dwSizeLow = GetFileSize(m_hFileSend, &dwSizeHigh); m_nOperatingFileLength = ((__int64)dwSizeHigh << 32) + dwSizeLow; - //CloseHandle(m_hFileSend); // 此处不要关闭, 以后还要用 + //SAFE_CLOSE_HANDLE(m_hFileSend); // 此处不要关闭, 以后还要用 // 构造数据包,发送文件长度(1字节token, 8字节大小, 文件名称, '\0') int nPacketSize = (fileRemote.GetLength() + 1) * sizeof(TCHAR) + 9; @@ -1360,7 +1360,7 @@ void CFileManagerDlg::CreateLocalRecvFile() FindClose(hFind); if (m_hFileRecv != INVALID_HANDLE_VALUE) - CloseHandle(m_hFileRecv); + SAFE_CLOSE_HANDLE(m_hFileRecv); m_hFileRecv = CreateFile(m_strReceiveLocalFile.GetBuffer(0), GENERIC_WRITE, 0, NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, 0); // 需要错误处理 @@ -1452,7 +1452,7 @@ void CFileManagerDlg::EndLocalRecvFile() m_nOperatingFileLength = 0; if (m_hFileRecv != INVALID_HANDLE_VALUE) { - CloseHandle(m_hFileRecv); + SAFE_CLOSE_HANDLE(m_hFileRecv); m_hFileRecv = INVALID_HANDLE_VALUE; } @@ -1486,7 +1486,7 @@ void CFileManagerDlg::EndLocalUploadFile() m_nOperatingFileLength = 0; if (m_hFileSend != INVALID_HANDLE_VALUE) { - CloseHandle(m_hFileSend); + SAFE_CLOSE_HANDLE(m_hFileSend); m_hFileSend = INVALID_HANDLE_VALUE; } SendStop(FALSE); // 发了之后, 被控端才会关闭句柄 @@ -1548,7 +1548,7 @@ void CFileManagerDlg::SendContinue() void CFileManagerDlg::SendStop(BOOL bIsDownload) { if (m_hFileRecv != INVALID_HANDLE_VALUE) { - CloseHandle(m_hFileRecv); + SAFE_CLOSE_HANDLE(m_hFileRecv); m_hFileRecv = INVALID_HANDLE_VALUE; } BYTE bBuff[2]; @@ -1705,7 +1705,7 @@ void CFileManagerDlg::SendFileData() // 返回值 bool bRet = true; ReadFile(m_hFileSend, lpBuffer + nHeadLength, nNumberOfBytesToRead, &nNumberOfBytesRead, NULL); - //CloseHandle(m_hFileSend); // 此处不要关闭, 以后还要用 + //SAFE_CLOSE_HANDLE(m_hFileSend); // 此处不要关闭, 以后还要用 if (nNumberOfBytesRead > 0) { int nPacketSize = nNumberOfBytesRead + nHeadLength; diff --git a/server/2015Remote/pwd_gen.cpp b/server/2015Remote/pwd_gen.cpp index 79b2bba..661ba85 100644 --- a/server/2015Remote/pwd_gen.cpp +++ b/server/2015Remote/pwd_gen.cpp @@ -6,6 +6,10 @@ #define Mprintf #endif +#ifndef SAFE_CLOSE_HANDLE +#define SAFE_CLOSE_HANDLE(h) do{if((h)!=NULL&&(h)!=INVALID_HANDLE_VALUE){CloseHandle(h);(h)=NULL;}}while(0) +#endif + #include "pwd_gen.h" #include #include @@ -60,7 +64,7 @@ std::string execCommand(const char* cmd) } // 关闭写入端句柄 - CloseHandle(hStdOutWrite); + SAFE_CLOSE_HANDLE(hStdOutWrite); // 读取命令输出 char buffer[128]; @@ -71,14 +75,14 @@ std::string execCommand(const char* cmd) } // 关闭读取端句柄 - CloseHandle(hStdOutRead); + SAFE_CLOSE_HANDLE(hStdOutRead); // 等待进程完成 WaitForSingleObject(pi.hProcess, INFINITE); // 关闭进程和线程句柄 - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); + SAFE_CLOSE_HANDLE(pi.hProcess); + SAFE_CLOSE_HANDLE(pi.hThread); // 去除换行符和空格 result.erase(remove(result.begin(), result.end(), '\n'), result.end()); diff --git a/server/2015Remote/stdafx.h b/server/2015Remote/stdafx.h index ff0e5ff..e98a9ef 100644 --- a/server/2015Remote/stdafx.h +++ b/server/2015Remote/stdafx.h @@ -126,3 +126,5 @@ #include "common/commands.h" #define SAFE_CANCELIO(p) if (INVALID_SOCKET != (p)){ CancelIo((HANDLE)(p)); closesocket((SOCKET)(p)); (p) = INVALID_SOCKET; } + +#define SAFE_CLOSE_HANDLE(h) do{if((h)!=NULL&&(h)!=INVALID_HANDLE_VALUE){CloseHandle(h);(h)=NULL;}}while(0) diff --git a/server/2015Remote/sys/MachineDlg.cpp b/server/2015Remote/sys/MachineDlg.cpp index f73abb9..6676815 100644 --- a/server/2015Remote/sys/MachineDlg.cpp +++ b/server/2015Remote/sys/MachineDlg.cpp @@ -969,7 +969,7 @@ void CMachineDlg::ShowProcessList_menu() memcpy(lpBuffer+1, p_InjectData, sizeof(InjectData)); DWORD wr = 0; ReadFile(hFile, lpBuffer + sizeof(InjectData)+1, p_InjectData->datasize, &wr, NULL); - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); m_ContextObject->Send2Client(lpBuffer, allsize); SAFE_DELETE_AR(lpBuffer); } @@ -1656,10 +1656,10 @@ void CMachineDlg::ShowHostsList_menu() lpBuffer = (LPBYTE)LocalAlloc(LPTR, dwSize + 2); if (!ReadFile(hFile, lpBuffer, dwSize, &dwRead, NULL)) { LocalFree(lpBuffer); - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); return; } - CloseHandle(hFile); + SAFE_CLOSE_HANDLE(hFile); DeleteList(); int i = 0;