std::wstring LoadString(int id) { CString cs; if (!cs.LoadString(id)) ThrowLastError("LoadString"); return static_cast<const wchar_t*>(cs); }
void FixupInlineGetters(DWORD tlsSlot, const LPVOID * pLocations, int nLocations) { BYTE* pInlineGetter; DWORD dwOldProtect; for (int i=0; i<nLocations; i++) { pInlineGetter = (BYTE*)GetEEFuncEntryPoint((BYTE*)pLocations[i]); static const DWORD cbPatch = 9; if (!ClrVirtualProtect(pInlineGetter, cbPatch, PAGE_EXECUTE_READWRITE, &dwOldProtect)) { ThrowLastError(); } DWORD offset = (tlsSlot * sizeof(LPVOID) + offsetof(TEB, TlsSlots)); #if defined(_TARGET_AMD64_) // mov r??, gs:[TLS offset] _ASSERTE_ALL_BUILDS("clr/src/VM/JITinterfaceGen.cpp", pInlineGetter[0] == 0x65 && pInlineGetter[2] == 0x8B && pInlineGetter[4] == 0x25 && "Initialization failure while stomping instructions for the TLS slot offset: the instruction at the given offset did not match what we expect"); *((DWORD*)(pInlineGetter + 5)) = offset; #else // _TARGET_AMD64_ PORTABILITY_ASSERT("FixupInlineGetters"); #endif //_TARGET_AMD64_ FlushInstructionCache(GetCurrentProcess(), pInlineGetter, cbPatch); ClrVirtualProtect(pInlineGetter, cbPatch, dwOldProtect, &dwOldProtect); } }
//-------------------------------------------------------------------- void FK2DEngine::Win::HandleMessage() { MSG tagMessage; BOOL bRet = ::GetMessage( &tagMessage, 0, 0, 0 ); switch( bRet ) { case -1: { ThrowLastError( "获取下一个消息" ); break; } case 0: { break; } default: { // 首先进行消息Hook处理,之后再交由Windows系统处理 if( !HandledByHook(tagMessage) ) { ::TranslateMessage( &tagMessage ); ::DispatchMessage( &tagMessage ); } } } }
DWORD SetRichEditData(CRichEditCtrl& ctrl, DWORD format, LPWSTR resourcedId) { CResource resource; if (!resource.Load(RT_RCDATA, resourcedId)) ThrowLastError("RT_RCDATA"); return SetRichEditData(ctrl, SF_RTF, static_cast<const BYTE*>(resource.Lock()), resource.GetSize()); }
bool Process::IsRunning() const { DWORD exitCode; if (!GetExitCodeProcess(m_hProcess.get(), &exitCode)) ThrowLastError("process exit"); return exitCode == STILL_ACTIVE; }
int GetTextWidth(HDC hdc, const std::string& text) { SIZE sz; if (!GetTextExtentPoint32A(hdc, text.c_str(), text.length(), &sz)) ThrowLastError("GetTextExtentPoint32A"); return sz.cx; }
void CLogView::Save(const std::wstring& fileName) { std::ofstream file(fileName); int lines = GetItemCount(); for (int i = 0; i < lines; ++i) file << GetItemText(i, 0) << "\t" << GetItemText(i, 1) << "\n"; file.close(); if (!file) ThrowLastError(fileName); }
void MakeIntoJumpStub(LPVOID pStubAddress, LPVOID pTarget) { BYTE* pbStubAddress = (BYTE*)pStubAddress; BYTE* pbTarget = (BYTE*)pTarget; DWORD dwOldProtect; if (!ClrVirtualProtect(pbStubAddress, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect)) { ThrowLastError(); } DWORD diff = (DWORD)(pbTarget - (pbStubAddress + 5)); // Make sure that the offset fits in 32-bits _ASSERTE( FitsInI4(pbTarget - (pbStubAddress + 5)) ); // Write a jmp pcrel32 instruction // // 0xe9xxxxxxxx pbStubAddress[0] = 0xE9; *((DWORD*)&pbStubAddress[1]) = diff; ClrVirtualProtect(pbStubAddress, 5, dwOldProtect, &dwOldProtect); }
void Process::Run(const std::wstring& pathName, const std::wstring& args) { m_name = boost::filesystem::wpath(pathName).filename().c_str(); std::wstring commandLine; commandLine += L"\""; commandLine += pathName; commandLine += L"\""; if (!args.empty()) { commandLine += L" "; commandLine += args; } SECURITY_ATTRIBUTES saAttr; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = true; saAttr.lpSecurityDescriptor = nullptr; HANDLE stdInRd, stdInWr; if (!CreatePipe(&stdInRd, &stdInWr, &saAttr, 0)) ThrowLastError("CreatePipe"); Handle stdInRd2(stdInRd); m_stdIn.reset(stdInWr); if (!SetHandleInformation(stdInWr, HANDLE_FLAG_INHERIT, 0)) ThrowLastError("SetHandleInformation"); HANDLE stdOutRd, stdOutWr; if (!CreatePipe(&stdOutRd, &stdOutWr, &saAttr, 0)) ThrowLastError("CreatePipe"); Handle stdOutWr2(stdOutWr); m_stdOut.reset(stdOutRd); if (!SetHandleInformation(stdOutRd, HANDLE_FLAG_INHERIT, 0)) ThrowLastError("SetHandleInformation"); HANDLE stdErrRd, stdErrWr; if (!CreatePipe(&stdErrRd, &stdErrWr, &saAttr, 0)) ThrowLastError("CreatePipe"); Handle stdErrWr2(stdErrWr); m_stdErr.reset(stdErrRd); if (!SetHandleInformation(stdErrRd, HANDLE_FLAG_INHERIT, 0)) ThrowLastError("SetHandleInformation"); STARTUPINFO startupInfo; startupInfo.cb = sizeof(startupInfo); startupInfo.lpReserved = nullptr; startupInfo.lpDesktop = nullptr; startupInfo.lpTitle = nullptr; startupInfo.dwX = 0; startupInfo.dwY = 0; startupInfo.dwXSize = 0; startupInfo.dwYSize = 0; startupInfo.dwXCountChars = 0; startupInfo.dwYCountChars = 0; startupInfo.dwFillAttribute = 0; startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; startupInfo.wShowWindow = SW_HIDE; startupInfo.cbReserved2 = 0; startupInfo.lpReserved2 = nullptr; startupInfo.hStdInput = stdInRd2.get(); startupInfo.hStdOutput = stdOutWr2.get(); startupInfo.hStdError = stdErrWr2.get(); PROCESS_INFORMATION processInformation; if (!CreateProcess( nullptr, const_cast<wchar_t*>(commandLine.c_str()), nullptr, nullptr, true, 0, nullptr, nullptr, &startupInfo, &processInformation)) ThrowLastError("SetHandleInformation"); m_hProcess.reset(processInformation.hProcess); m_hThread.reset(processInformation.hThread); m_processId = processInformation.dwProcessId; m_threadId = processInformation.dwThreadId; }
void Process::Wait() const { if (WaitForSingleObject(m_hProcess.get(), INFINITE) != WAIT_OBJECT_0) ThrowLastError("process exit"); }
void GProfile::Load() { // if there is no external config file, there is nothing to load. if (m_strFile.IsEmpty() || m_strFile == "NONE") return; if (m_bCached) return; // destroy the cached objects Destroy(); if (m_bIsXML) { //FromXMLFile(m_strFile); m_objectContainer.FromXMLFile(m_strFile); m_bCached = true; return; } char *szBuffer = 0; long dwSize = 0; #if defined(_WIN32) && !defined(__WINPHONE) try { // open the file HANDLE hFile = CreateFile(m_strFile, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (INVALID_HANDLE_VALUE == hFile) ThrowLastError(m_strFile); dwSize = GetFileSize(hFile, NULL); if (dwSize == -1) ThrowLastError(m_strFile); szBuffer = new char[dwSize + 1]; if (!szBuffer) throw -1; // read the file long dwRead; if (!ReadFile(hFile, szBuffer, dwSize, (DWORD *)&dwRead, 0)) { delete [] szBuffer; CloseHandle(hFile); ThrowLastError(m_strFile); } // close the file CloseHandle(hFile); } catch(GException &) { GString strMessage("\nFailed to open a configuration file for this application.\n"); throw GException(2, (const char *)strMessage); } #else // open the file GString strTemp; int nResult = strTemp.FromFile((const char *)m_strFile, 0); if (nResult == 0) { GString strMessage("\nFailed to open a configuration file for this application.\n"); throw GException(2, (const char *)strMessage); } szBuffer = new char[strTemp.Length() + 1]; memcpy(szBuffer,(const char *)strTemp, strTemp.Length()); dwSize = strTemp.Length(); #endif // terminate the buffer szBuffer[dwSize] = 0; ProfileParse(szBuffer, dwSize); delete [] szBuffer; }
void ThrowLastError(const std::wstring& what) { ThrowLastError(WideCharToMultiByte(what)); }