J
John paul coder
Guest
Hello,
In my application, I am enumerating the processes and checking if the .exe is running or not by continuously polling for the exes. If it finds the running exe then it connects and sends the login event.
This is working fine but what I observed is unnecessarily we are using for loop as it is checking all the running processes.
And also when it finds the running exe, it connects and logs into the application but while loop is still running.
Can anyone suggest better design.
Below is the code snippet:
bool CEMRImpl::m_boAlive = true;
void CEMRImpl:rocessApplication()
{
m_pIEMREventHandler = new IEMREventHandler(*this, m_spIEMRObject,
&CEMRImpl::OnEMREvent);
while(m_boAlive)
{
m_boEMRUiRunning = false;
m_boEMRPowerRunning = false;
m_boEMRConsoleRunning = false;
DWORD l_dwaProcesses[1024], l_dwProcessNeeded, l_dwProcess;
unsigned int i;
if (!EnumProcesses(l_dwaProcesses, sizeof(l_dwaProcesses), &l_dwProcessNeeded))
{
return;
}
l_dwProcess = l_dwProcessNeeded / sizeof(DWORD);
for (i = 0; i < l_dwProcess; i++)
{
if(l_dwaProcesses != 0)
{
TCHAR l_szProcessName[MAX_PATH] = TEXT("<unknown>");
HANDLE l_hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, FALSE, l_dwaProcesses);
if (NULL != l_hProcess )
{
HMODULE l_hMod;
DWORD l_dwProcessNeeded;
if (EnumProcessModules(l_hProcess, &l_hMod, sizeof(l_hMod),
&l_dwProcessNeeded))
{
GetModuleBaseName(l_hProcess, l_hMod, l_szProcessName,
sizeof(l_szProcessName)/sizeof(TCHAR));
}
}
basic_string<TCHAR> l_strTxt( l_szProcessName );
string l_str( l_strTxt.begin(), l_strTxt.end() );
if (_tcscmp(l_str.c_str(), "EMRUi.exe") == 0)
{
m_boEMRUiRunning = true;
m_strSoftPhoneName = EMR_UI_STRING;
}
if (_tcscmp(l_str.c_str(), "EMRPower.exe") == 0)
{
m_boEMRPowerRunning = true;
m_strSoftPhoneName = EMR_POWER_STRING;
}
if (_tcscmp(l_str.c_str(), "EMRConsole.exe") == 0)
{
m_boEMRConsoleRunning = true;
m_strSoftPhoneName = EMR_CONSOLE_STRING;
}
CloseHandle(l_hProcess);
}
}
if(!m_boEMRProcAttached && (m_boEMRUiRunning || m_boEMRPowerRunning || m_boEMRConsoleRunning))
// Using connect we are establishing the connection and logging in to the application
connect();
else if(m_boEMRProcAttached && !m_boEMRUiRunning && !m_boEMRPowerRunning && !m_boEMRConsoleRunning)
disconnect();
CCommonUtilities::addDelay(1500);
}
}
void CEMRImpl::destroy()
{
m_boAlive = false;
CoUninitialize();
}
Thank you.
Continue reading...
In my application, I am enumerating the processes and checking if the .exe is running or not by continuously polling for the exes. If it finds the running exe then it connects and sends the login event.
This is working fine but what I observed is unnecessarily we are using for loop as it is checking all the running processes.
And also when it finds the running exe, it connects and logs into the application but while loop is still running.
Can anyone suggest better design.
Below is the code snippet:
bool CEMRImpl::m_boAlive = true;
void CEMRImpl:rocessApplication()
{
m_pIEMREventHandler = new IEMREventHandler(*this, m_spIEMRObject,
&CEMRImpl::OnEMREvent);
while(m_boAlive)
{
m_boEMRUiRunning = false;
m_boEMRPowerRunning = false;
m_boEMRConsoleRunning = false;
DWORD l_dwaProcesses[1024], l_dwProcessNeeded, l_dwProcess;
unsigned int i;
if (!EnumProcesses(l_dwaProcesses, sizeof(l_dwaProcesses), &l_dwProcessNeeded))
{
return;
}
l_dwProcess = l_dwProcessNeeded / sizeof(DWORD);
for (i = 0; i < l_dwProcess; i++)
{
if(l_dwaProcesses != 0)
{
TCHAR l_szProcessName[MAX_PATH] = TEXT("<unknown>");
HANDLE l_hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, FALSE, l_dwaProcesses);
if (NULL != l_hProcess )
{
HMODULE l_hMod;
DWORD l_dwProcessNeeded;
if (EnumProcessModules(l_hProcess, &l_hMod, sizeof(l_hMod),
&l_dwProcessNeeded))
{
GetModuleBaseName(l_hProcess, l_hMod, l_szProcessName,
sizeof(l_szProcessName)/sizeof(TCHAR));
}
}
basic_string<TCHAR> l_strTxt( l_szProcessName );
string l_str( l_strTxt.begin(), l_strTxt.end() );
if (_tcscmp(l_str.c_str(), "EMRUi.exe") == 0)
{
m_boEMRUiRunning = true;
m_strSoftPhoneName = EMR_UI_STRING;
}
if (_tcscmp(l_str.c_str(), "EMRPower.exe") == 0)
{
m_boEMRPowerRunning = true;
m_strSoftPhoneName = EMR_POWER_STRING;
}
if (_tcscmp(l_str.c_str(), "EMRConsole.exe") == 0)
{
m_boEMRConsoleRunning = true;
m_strSoftPhoneName = EMR_CONSOLE_STRING;
}
CloseHandle(l_hProcess);
}
}
if(!m_boEMRProcAttached && (m_boEMRUiRunning || m_boEMRPowerRunning || m_boEMRConsoleRunning))
// Using connect we are establishing the connection and logging in to the application
connect();
else if(m_boEMRProcAttached && !m_boEMRUiRunning && !m_boEMRPowerRunning && !m_boEMRConsoleRunning)
disconnect();
CCommonUtilities::addDelay(1500);
}
}
void CEMRImpl::destroy()
{
m_boAlive = false;
CoUninitialize();
}
Thank you.
Continue reading...