R call_in_ipc(const std::string& func_name, T* val, int size) { assert(hProcess()); int remote_proc = (int)GetProcAddress(hModule(), func_name.c_str()); if(!remote_proc) throw std::invalid_argument(std::string("No function ") + func_name + " found"); return std::move(run<typename R, typename T, time_out>(hProcess(), remote_proc - (int)hModule(), func_name, val, size)); }
void CCameraIO::FilterSet( short Slot ) { // Determine how far we have to move int Pos = Slot - m_FilterPosition; if (Pos < 0) Pos += NUM_POSITIONS; HANDLE hProcess(0); DWORD Class(0); if ( m_HighPriority ) { // Store current process class and priority hProcess = GetCurrentProcess(); Class = GetPriorityClass ( hProcess ); SetPriorityClass ( hProcess, REALTIME_PRIORITY_CLASS ); } for (int I = 0; I < Pos; I++) { // Advance one position for (int J = 0; J < NUM_STEPS_PER_FILTER; J++) { m_FilterStepPos += 1; if (m_FilterStepPos >= NUM_STEPS) m_FilterStepPos = 0; unsigned char Step = Steps[ m_FilterStepPos ]; AuxOutput( Step ); Sleep ( STEP_DELAY ); } } if ( m_HighPriority ) SetPriorityClass ( hProcess, Class ); m_FilterPosition = Slot; }
Process Process::Create(const TChar* commandLine, CreationFlags flags) { STARTUPINFO startupInfo = {0}; startupInfo.cb = sizeof(startupInfo); PROCESS_INFORMATION processInfo = {0}; String buffer(commandLine); BOOL success = CreateProcess( NULL, // The name of the module to be executed buffer.GetBuffer(), // The command line to be executed NULL, // Process handle is not inheritable NULL, // Thread handle is not inheritable FALSE, // Set handle inheritance to FALSE (DWORD)flags, // Creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &startupInfo, // Pointer to STARTUPINFO structure &processInfo); // Pointer to PROCESS_INFORMATION structure if (!success) { THROW_LAST_ERROR_EXCEPTION(); } Handle hThread(processInfo.hThread); Handle hProcess(processInfo.hProcess); return Process(processInfo.dwProcessId, std::move(hProcess)); }
std::wstring ProcessInfo::GetProcessNameByPid(DWORD processId) { Handle hProcess(::OpenProcess(PROCESS_QUERY_INFORMATION, false, processId)); if (hProcess) return GetProcessName(hProcess.get()); return L""; }
explicit RPC_caller(const TString& module_name) : hModule(LoadLibraryEx(module_name.c_str(), NULL, 0)) { PROCESSENTRY32 entry; entry.dwSize = sizeof(PROCESSENTRY32); HandleHolder snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL)); if (Process32First(snapshot(), &entry) == TRUE) { while (Process32Next(snapshot(), &entry) == TRUE) { if (module_name.compare(entry.szExeFile) == 0) { hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); if(!hProcess()) throw rpc_call_error(TString(_TEXT("Unable to open process ")) + module_name); base = this->GetModuleBase(hProcess(), module_name); } } } }
NS_IMETHODIMP InjectCrashRunnable::Run() { if (mInjectorPath.IsEmpty()) return NS_OK; nsAutoHandle hProcess( OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, mPID)); if (!hProcess) { NS_WARNING("Unable to open remote process handle for crashreporter injection."); return NS_OK; } void* proc = LoadRemoteLibraryAndGetAddress(hProcess, mInjectorPath.get(), "Start"); if (!proc) { NS_WARNING("Unable to inject crashreporter DLL."); return NS_OK; } HANDLE hRemotePipe = CrashGenerationClient::DuplicatePipeToClientProcess( NS_ConvertASCIItoUTF16(GetChildNotificationPipe()).get(), hProcess); if (INVALID_HANDLE_VALUE == hRemotePipe) { NS_WARNING("Unable to duplicate crash reporter pipe to process."); return NS_OK; } nsAutoHandle hThread(CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) proc, (void*) hRemotePipe, 0, NULL)); if (!hThread) { NS_WARNING("Unable to CreateRemoteThread"); // We have to close the remote pipe or else our crash generation client // will be stuck unable to accept other remote requests. HANDLE toClose = INVALID_HANDLE_VALUE; if (DuplicateHandle(hProcess, hRemotePipe, ::GetCurrentProcess(), &toClose, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { CloseHandle(toClose); return NS_OK; } } return NS_OK; }
CHandle CHFServer::GetProcessId(std::string& sProcessName, DWORD& dwProcessID ) { DWORD aProcesses[2048], cbNeeded, cProcesses; if(!sProcessName.length()) return CHandle(); dwProcessID = 0; unsigned int i; std::string sUpperCaseProcessName = utils::upcase(sProcessName); if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return CHandle(); // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the name and process identifier for each process. for ( i = 0; i < cProcesses; i++ ) { char szProcessName[MAX_PATH] ={0}; CHandle hProcess(OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i] )); if (NULL != (HANDLE)hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) ) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)); if(utils::upcase(std::string(szProcessName)) == sProcessName && aProcesses[i] != ::GetCurrentProcessId()) { dwProcessID = aProcesses[i]; return hProcess; } } } } return CHandle(); }
Process Process::Run(const TChar* path, const TChar* params, const TChar* verb) { SHELLEXECUTEINFO info = {0}; info.cbSize = sizeof(info); info.lpFile = path; info.lpParameters = params; info.nShow = SW_SHOWNORMAL; info.lpVerb = verb ? verb : Text("open"); info.fMask = SEE_MASK_NOCLOSEPROCESS; BOOL success = ::ShellExecuteEx(&info); if (!success) { THROW_LAST_ERROR_EXCEPTION(); } Handle hProcess(info.hProcess); return Process(std::move(hProcess)); }
bool CCameraIO::GetLine( unsigned short* pLineData, short& xSize ) { int i; if ( m_WaitingforLine ) { // In case application did not poll read_Status m_WaitingforLine = false; ///////////////////////////////////// // Wait until camera is done clocking clock_t StopTime = clock() + CLOCKS_PER_SEC; // wait at most one second while ( true ) { unsigned short val = 0; Read( Reg_Status, val ); if ( ( val & RegBit_LineDone ) != 0 ) break; // Line done if ( clock() > StopTime ) { Flush(); return false; // Timed out, no line available } } } bool ret = false; // assume failure // MaskIrqs(); // NB Application must have allocated enough memory or else !!! if ( pLineData != NULL ) { HANDLE hProcess(0); DWORD Class(0); if ( m_HighPriority ) { // Store current process class and priority hProcess = GetCurrentProcess(); Class = GetPriorityClass ( hProcess ); SetPriorityClass ( hProcess, REALTIME_PRIORITY_CLASS ); } long XPixels = long( m_ExposureNumX ); long SkipPixels = long( m_ExposureSkipC ); if ( ReadLine( SkipPixels, XPixels, pLineData ) ) { // Something went wrong xSize = 0; ret = false; } else { xSize = m_ExposureNumX; if ( m_DataBits == 16 ) { // Take care of two's complement converters unsigned short *Ptr = pLineData; short *Ptr2 = (short *) pLineData; long Size = m_ExposureNumX; for (i = 0; i < Size; i++) { *Ptr++ = (unsigned short) *Ptr2++ + 32768 ; } } ret = true; } //Restore priority if ( m_HighPriority ) SetPriorityClass ( hProcess, Class ); } // UnmaskIrqs(); return ret; }
bool CCameraIO::GetImage( unsigned short* pImageData, short& xSize, short& ySize ) { int i; unsigned short BIC = m_ExposureBIC + m_ExposureStartX; // Update internal variables in case application did not poll read_Status m_WaitingforTrigger = false; m_WaitingforLine = false; if ( m_WaitingforImage ) { // In case application did not poll read_Status m_WaitingforImage = false; ///////////////////////////////////// // Wait until camera is done flushing clock_t StopTime = clock() + long( m_Timeout * CLOCKS_PER_SEC ); // wait at most m_Timeout seconds while ( true ) { unsigned short val = 0; Read( Reg_Status, val ); if ( ( val & RegBit_FrameDone ) != 0 ) break; if ( clock() > StopTime ) return false; // Timed out } } // MaskIrqs(); ///////////////////////////////////// // Update our internal status unsigned short val = 0; Read( Reg_CommandReadback, val ); if ( !( val & RegBit_ShutterOverride ) ) m_Shutter = false; StopFlushing(); LoadColumnLayout( m_ExposureAIC, BIC, (unsigned short) m_ExposureNumX + m_ExposureSkipC ); if ( m_ExposureRemainingLines > 0 ) { LoadTimerAndBinning( 0.0, m_ExposureHFlush, m_ExposureRemainingLines ); ///////////////////////////////////// // Clock out the remaining lines m_RegShadow[ Reg_Command ] |= RegBit_StartNextLine; // set bit to 1 Write( Reg_Command, m_RegShadow[ Reg_Command ] ); m_RegShadow[ Reg_Command ] &= ~RegBit_StartNextLine; // set bit to 0 Write( Reg_Command, m_RegShadow[ Reg_Command ] ); ///////////////////////////////////// ///////////////////////////////////// // Wait until camera is done clocking clock_t StopTime = clock() + CLOCKS_PER_SEC; // wait at most one second while ( true ) { unsigned short val = 0; Read( Reg_Status, val ); if ( ( val & RegBit_LineDone ) != 0 ) break; // Line done if ( clock() > StopTime ) { Flush(); return false; // Timed out, no image available } } } LoadTimerAndBinning( 0.0, m_ExposureBinX, m_ExposureBinY ); bool ret = false; // assume failure // NB Application must have allocated enough memory or else !!! if ( pImageData != NULL ) { HANDLE hProcess(0); DWORD Class(0); if ( m_HighPriority ) { // Store current process class and priority hProcess = GetCurrentProcess(); Class = GetPriorityClass ( hProcess ); SetPriorityClass ( hProcess, REALTIME_PRIORITY_CLASS ); } m_RegShadow[ Reg_Command ] |= RegBit_FIFOCache; // set bit to 1 Write( Reg_Command, m_RegShadow[ Reg_Command ] ); long XPixels = long( m_ExposureNumX ); long SkipPixels = long( m_ExposureSkipC ); for (i = 0; i < m_ExposureSkipR; i++) { if ( ReadLine( SkipPixels, XPixels, pImageData ) ) break; } if ( i == m_ExposureSkipR ) { // We have skipped all the lines long YPixels = long( m_ExposureNumY ); unsigned short* pLineBuffer = pImageData; for (i = 0; i < YPixels; i++) { if ( ReadLine( SkipPixels, XPixels, pLineBuffer ) ) break; pLineBuffer += XPixels; } if ( i == YPixels ) ret = true; // We have read all the lines } m_RegShadow[ Reg_Command ] &= ~RegBit_FIFOCache; // set bit to 0 Write( Reg_Command, m_RegShadow[ Reg_Command ] ); //Restore priority if ( m_HighPriority ) SetPriorityClass ( hProcess, Class ); } // UnmaskIrqs(); if ( ret ) { // We were successfull Flush( m_ExposureAIR ); // flush after imaging rows xSize = m_ExposureNumX; ySize = m_ExposureNumY; if ( m_DataBits == 16 ) { // Take care of two's complement converters unsigned short *Ptr = pImageData; short *Ptr2 = (short *) pImageData; long Size = m_ExposureNumX * m_ExposureNumY; for (i = 0; i < Size; i++) { *Ptr++ = (unsigned short) *Ptr2++ + 32768 ; } } } else { // Something went wrong xSize = 0; ySize = 0; } Flush(); // start normal flushing return ret; }
bool CCameraIO::FilterHome() { HANDLE hProcess(0); DWORD Class(0); if ( m_HighPriority ) { // Store current process class and priority hProcess = GetCurrentProcess(); Class = GetPriorityClass ( hProcess ); SetPriorityClass ( hProcess, REALTIME_PRIORITY_CLASS ); } // Find the home position m_FilterPosition = 0; int Safety = 0; for (int I = 0; I < NUM_POSITIONS * NUM_STEPS_PER_FILTER * 2; I++) { // Advance the filter one step m_FilterStepPos += 1; if (m_FilterStepPos >= NUM_STEPS) m_FilterStepPos = 0; unsigned char Step = Steps[ m_FilterStepPos ]; AuxOutput( Step ); Sleep ( STEP_DELAY ); // Check for strobe unsigned short val = 0; Read( Reg_Status, val ); if ( val & RegBit_GotTrigger ) { // Cycle all the way around if it's on the first time if (I < NUM_STEPS_PER_FILTER) { if (++Safety > NUM_STEPS_PER_FILTER * 2) { // Restore normal priority if ( m_HighPriority ) SetPriorityClass ( hProcess, Class ); return false; } I = 0; continue; } // Continue cycling until we get clear of the opto mirror for (int J = 0; J < NUM_STEPS_PER_FILTER; J++) { // Advance the filter one step m_FilterStepPos += 1; if (m_FilterStepPos >= NUM_STEPS) m_FilterStepPos = 0; unsigned char Step = Steps[ m_FilterStepPos ]; AuxOutput( Step ); Sleep ( STEP_DELAY ); val = 0; Read( Reg_Status, val ); if ( val & RegBit_GotTrigger ) { Sleep ( 10 ); val = 0; Read( Reg_Status, val ); if ( val & RegBit_GotTrigger ) { // Restore normal priority if ( m_HighPriority ) SetPriorityClass ( hProcess, Class ); return true; } } } // Restore normal priority if ( m_HighPriority ) SetPriorityClass ( hProcess, Class ); return true; } } // Restore normal priority if ( m_HighPriority ) SetPriorityClass ( hProcess, Class ); return false; }
void CProcNonExistTask::worker_func() { exception_catcher::set_thread_exception_handlers(); InfoLog("proc non exist task worker thread func begin"); while (true) { std::vector<DWORD> pids; //todo: is exactly_match=false correct ????? find_pids_by_path(m_proc_path, pids, true, false); if (pids.empty())//non exist { InfoLog(TSTR("can not find process[%s], try to execute function if has"), m_proc_path.c_str()); if (m_f) { try { m_f(); } catch (...) { ErrorLog("execute proc non exist task function exception"); } } //sleep some while if function has done something which will effect later on const DWORD wait_result = WaitForSingleObject(m_exit_event.get_ref(), 1000); if (WAIT_OBJECT_0 == wait_result) { InfoLog("got exit notify"); break; } } else//exist { const DWORD pid = pids.at(0); scoped_handle<> hProcess(OpenProcess(SYNCHRONIZE, FALSE, pid)); if (!hProcess.valid()) { ErrorLogLastErr("OpenProcess[%lu] fail", pid); const DWORD wait_result = WaitForSingleObject(m_exit_event.get_ref(), m_interval_seconds * 1000); if (WAIT_OBJECT_0 == wait_result) { InfoLog("got exit notify"); break; } } else { bool should_break = false; HANDLE pHandles[2] = {m_exit_event.get_ref(), hProcess.get_ref()}; const DWORD wait_result = WaitForMultipleObjects(sizeof(pHandles) / sizeof(pHandles[0]), pHandles, FALSE, INFINITE); switch (wait_result) { case WAIT_OBJECT_0: InfoLog("got exit notify"); should_break = true; break; case WAIT_OBJECT_0 + 1: InfoLog(TSTR("process[%s] exited"), m_proc_path.c_str()); break; default: ErrorLogLastErr("WaitForMultipleObjects fail, return code: %lu", wait_result); //sleep some while for recover from error state if (WAIT_OBJECT_0 == WaitForSingleObject(m_exit_event.get_ref(), 1000)) { InfoLog("got exit notify"); should_break = true; } break; } if (should_break) { break; } } } } InfoLog("proc non exist task worker thread func end"); }