|
That depends on the kind of data you already have.
Is it the current process? Or do you have the name of the executable? Or a window handle?
Jens
|
|
|
|
|
Yeah the name is armyops.exe
|
|
|
|
|
|
If you have the name of the process, and that process is not you, you must either search for a window you know that process created, and get the process ID from that, or, if you only have the name of the process, you need to use the PSAPI/ToolHelper API to enumerate all running processes, look at each one's name, check if it matched, and if so, grab it's process ID.
See here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/about_psapi.asp
Bare in mind PSAPI only works with NT/2k/XP so if you want a cross platform soulition, use ToolTip - see the file Tlhelp32.h in the platform SDK for a statring point- but you want something like this which terminates a process given a name
<br />
BOOL CTestEnvKillerApp::InitInstance()<br />
{<br />
<br />
OSVERSIONINFO osvi;<br />
memset(&osvi, NULL, sizeof(OSVERSIONINFO));<br />
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);<br />
GetVersionEx(&osvi);<br />
<br />
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{<br />
HMODULE hLib = LoadLibrary( _T("psapi.dll") );<br />
if (NULL == hLib)<br />
{<br />
AfxMessageBox(_T("Could not load PSAPI.DLL.\nPlease check it's in your path"), MB_OK | MB_ICONEXCLAMATION);<br />
return FALSE;<br />
}<br />
lpfnEnumProcesses pFnEnumProc = (lpfnEnumProcesses)GetProcAddress( hLib, "EnumProcesses" );<br />
lpfnGetModuleBaseName pFnGetModBaseName = (lpfnGetModuleBaseName)GetProcAddress( hLib, "GetModuleBaseNameA" );<br />
lpfnEnumProcessModules pFnEnumProcMod = (lpfnEnumProcessModules)GetProcAddress( hLib, "EnumProcessModules" );<br />
<br />
if ( (NULL == pFnEnumProc) || (NULL == pFnGetModBaseName) || (NULL == pFnEnumProcMod) )<br />
{<br />
AfxMessageBox(_T("Could not find procedure in PSAPI.DLL"), MB_OK | MB_ICONEXCLAMATION);<br />
FreeLibrary(hLib);<br />
return FALSE;<br />
}<br />
<br />
DWORD dwNeeded = 0;<br />
DWORD dwProcessIDs[1024];<br />
memset(dwProcessIDs, NULL, sizeof(DWORD)*1024);<br />
<br />
if ( pFnEnumProc(dwProcessIDs, 1024, &dwNeeded) )<br />
{<br />
char szProcessName[MAX_PATH];<br />
<br />
short sNumProcs = (short)( dwNeeded/sizeof(DWORD) );<br />
for (short s = 0; s < sNumProcs; s++)<br />
{<br />
memset(szProcessName, NULL, sizeof(char)*MAX_PATH);<br />
<br />
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE,<br />
FALSE, dwProcessIDs[s] );<br />
if (hProcess) <br />
{ <br />
HMODULE hMod = NULL;<br />
dwNeeded = 0;<br />
<br />
if ( pFnEnumProcMod( hProcess, &hMod, sizeof(hMod), &dwNeeded) ) <br />
{<br />
if (!pFnGetModBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName) ) )<br />
{<br />
CloseHandle(hProcess);<br />
continue;<br />
}<br />
<br />
if ( ItsOurProcess() )<br />
{<br />
TerminateProcess(hProcess, 0);<br />
}<br />
}<br />
<br />
CloseHandle(hProcess);<br />
}<br />
}<br />
}<br />
<br />
FreeLibrary(hLib);<br />
}<br />
else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{<br />
HMODULE hLib = LoadLibrary( _T("kernel32.dll") );<br />
if (NULL == hLib)<br />
{<br />
AfxMessageBox(_T("Could not load kernel32.DLL.\nPlease check it's in your path"), MB_OK | MB_ICONEXCLAMATION);<br />
return FALSE;<br />
}<br />
<br />
lpfnProcess32First pFnProc32First = (lpfnProcess32First)GetProcAddress( hLib, "Process32First" );<br />
lpfnProcess32Next pFnProc32Next = (lpfnProcess32Next)GetProcAddress( hLib, "Process32Next" );<br />
lpfnTH32Snap pFnTH32Snap = (lpfnTH32Snap)GetProcAddress( hLib, "CreateToolhelp32Snapshot" );<br />
<br />
if ( (NULL == pFnProc32First) || (NULL == pFnProc32Next) || (NULL == pFnTH32Snap) )<br />
{<br />
AfxMessageBox(_T("Could not find procedure in Kernel32.DLL"), MB_OK | MB_ICONEXCLAMATION);<br />
FreeLibrary(hLib);<br />
return FALSE;<br />
}<br />
<br />
PROCESSENTRY32 pe;<br />
memset(&pe, NULL, sizeof(PROCESSENTRY32));<br />
<br />
HANDLE hSnapshot = pFnTH32Snap(TH32CS_SNAPPROCESS, 0);<br />
<br />
if (!hSnapshot)<br />
{<br />
AfxMessageBox(_T("Could not retrieve process snapshot"), MB_OK | MB_ICONEXCLAMATION);<br />
}<br />
<br />
pe.dwSize = sizeof(PROCESSENTRY32);<br />
<br />
if ( pFnProc32First(hSnapshot, &pe) )<br />
{<br />
MODULEENTRY32 me32 = {0}; <br />
BOOL bGotModule = FALSE; <br />
do<br />
{<br />
if ( GetProcessModule(pe.th32ProcessID, pe.th32ModuleID, &me32, sizeof(MODULEENTRY32), hLib) )<br />
{<br />
char szProcessName[MAX_PATH];<br />
memset(szProcessName, NULL, sizeof(char)*MAX_PATH);<br />
<br />
char drive[8];<br />
char dir[MAX_PATH]; <br />
char ext[8];<br />
_splitpath(me32.szExePath, drive, dir, szProcessName, ext);<br />
strcat(szProcessName, ext);<br />
<br />
if ( ItsOurProcess())<br />
{<br />
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE,<br />
FALSE, pe.th32ProcessID);<br />
if (hProcess)<br />
{<br />
<br />
TerminateProcess(hProcess, 0);<br />
<br />
CloseHandle(hProcess);<br />
}<br />
}<br />
}<br />
}<br />
while(pFnProc32Next(hSnapshot, &pe));<br />
}<br />
<br />
CloseHandle(hSnapshot);<br />
<br />
FreeLibrary(hLib);<br />
}<br />
else<br />
{<br />
AfxMessageBox(_T("Operating system does not support this application"), MB_OK | MB_ICONEXCLAMATION);<br />
}<br />
return FALSE;<br />
}
FYI ToolHelp works on both sets of platform so you could just use that
|
|
|
|
|
Hi!
I'm searching for code or just the name of the windows api function that can turn _only_ the screen (not the whole computer) into standby mode, in other words, turn it off, until the mouse gets moved or a key gets pressed.
I found out that there's a (user configurable) display idle timer, however, I haven't found a way to manipulate this timer programmatically.
There are actually quite a lot of functions in the windows api related to power managment, but seemingly not one to turn only the screen off...
Maybe someone can help me,
Martin Leonhartsberger
mleonhartsberger@a1.net
|
|
|
|
|
Create a power scheme where USER_POWER_POLICY.VideoTimeoutAc is set to 1 (second). Save the policy by calling WritePwrScheme() . Then call SetActivePwrScheme() to set the current power scheme to the one that you just created. This is just a high-level overview, but hopefully you get the gist enough to fill in the blanks.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Yeah thanks I successfully used your description to send my screen into darkness after one second.
But (there's always one ) theres the problem of waking up again. If I don't move the mouse around continously the screen blankens immediately again (no surprise, timer is still set to 1 second).
So I thought, well, why don't wait the second until standby mode kicks in, and then write the old timer back? I use Sleep() for this, but the problem is, after Sleep() returns, the system thinks that it should awaken itself again and the screen turns on.
So, I need a way to wait this second and write the old scheme back WITHOUT letting the system think it should wakeup.
|
|
|
|
|
You might need to use GetLastInputInfo() to know when the mouse/keyboard was last used.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
I'd like to introduce into my app a control like Dundas Chart (it's so pretty..., but so expensive too..., no cracks wanted and no burning either) but I don't want to use Microsoft's Chart Control...
OH !!, I'm a MFC programmer.
Please, if you know about a class like Dundas Chart make me know it...
Thankx !!
|
|
|
|
|
|
If a computer is suspended/hibernating, does that affect then length of time a process will sleep for?
If a process is told to Sleep () for 1 hour and after half an hour the computer is put into hibernate for 2 hours, when it starts back up again and Windows resumes, does the process still have half an hour to Sleep (), or is its Sleep () time up (having effectively slept for 2 1/2 hours already)?
|
|
|
|
|
i don't know for your effective question, but don't use sleep() function for so long waits...
i uses active waits, and so need full CPU time.
TOXCCT >>> GEII power
|
|
|
|
|
Yeah, it's not good - I got a program dropped on my desk and while I'm fixing it by re-writing it, I still have to try and make the old version work
|
|
|
|
|
I am not quite sure if Sleep really waits actively...
See MSDN:
<quote>The Sleep function suspends the execution of the current thread for at least the specified interval.
Jens
|
|
|
|
|
yes, what does it say with that ??! give us the entire paragraph please...
i already read the MSDN ; it is not advised
TOXCCT >>> GEII power
|
|
|
|
|
My comment didn't answer your original question.
I just wanted to state that I don't think that
calling "Sleep" will cause the thread to wait actively.
It would be rather dumb to implement a task scheduler which lets a thread that called Sleep wait actively, wouldn't it?
To clarify:
By "waiting actively" I mean a continous loop until a condition is reached, like this one:
<br />
while ( true )<br />
{<br />
if ( bConditionIsReached )<br />
{<br />
break;<br />
}<br />
}<br />
Jens
|
|
|
|
|
yes, i understood what your previous post said about this, and i know what is an active wait.
just paste here what MSDN say about sleep() (but give the entire text on it).
TOXCCT >>> GEII power
|
|
|
|
|
|
well, you didn't want to copy the text, i give you :
If you have a thread that uses Sleep with infinite delay, the system will deadlock.
How do you understand this ????
TOXCCT >>> GEII power
|
|
|
|
|
Well the context of this sentence deals with messages, and if you read a little bit more it reads
Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock
And this one makes sense. Think about a SendMessage call to all windows with no timeout and one of the threads sleeps endlessly. Then the calling process will also hang.
But I don't see this as an evidence that Sleep waits actively.
Jens
|
|
|
|
|
no messages are advised ! do you read entierely the particle or you only see some word ?!
MSDN says that if you have to create a nex thread, you mustn't use sleep() because of this problem.
TOXCCT >>> GEII power
|
|
|
|
|
The system will only deadlock if a message is sent to a window that is on a thread using "Sleep" in a non-alertable state. If you use "SleepEx", the second parameter allows the thread to wait in an "alertable" state.
A "SendMessage" queues an APC on the window's thread and will be processed if the thread is in an "alertable" state. So, if you use SleepEx() and maintain an alertable thread state you will be able to become signaled.
Sleep() is also a system call. It's not implemented in user-mode, so it doesn't do a while(x < 10). It's not a spinlock either. The thread should be delayed in the scheduler until the sleep has expired.
I'm not sure what the original question was, but it was something about going into a hibernate state. Perhaps this should answer the question (Link Below)
If this is true, I would think that the thread sleep would continue to sleep once the machine resumed, since it technically hasn't slept the full period. The only way it wouldn't is unless the scheduler bases the sleep off of time (Which would have changed) and it processes this information upon restart. Of course, even if it is based off of time, generally the OS is probably notified of a hibernation and it could make special arrangements to honor the full sleep independent of the hibernation, ie, resume after the system restarted.
I can't be sure of that without a test. You could write a thread to sleep and after the sleep write a debug message or something. Perform the test and verify the results. Though, generally speaking, the results could vary by implementation (hardware, OS, etc.) but most likely the implementations should match what you find.
http://www.veritest.com/benchmarks/battmark/bb3power_man/b3sleep.asp
Hibernation
Hibernation is one of several terms describing a state of reduced power consumption that a power-managed computer may support.
According to the conventional definition of hibernation, a notebook copies all memory contents and hardware state information to non-volatile storage (typically the hard disk, which requires no power to retain information), and the notebook is completely powered down.
Leaving hibernation and returning the system to full operation requires booting the computer as usual and then waiting while the system restores the memory contents and hardware state. The process is finished when the computer is at exactly the point it was when the hibernate process started.
Hibernation trades time for power: maintaining the system state requires no power (because the state has been saved to disk), so the computer can remain in hibernation indefinitely. However the user must wait when entering and returning from hibernation—sometimes several minutes or more if the system has a lot of memory.
|
|
|
|
|
BTW, Sleep() calls SleepEx with FALSE.
|
|
|
|
|
Well.. TEST IT !!
You can send a MessageBox after the Sleep() function, so if you start back up again and the message is on your screen it means that it worked.
I think is the best option...
|
|
|
|
|
I'm inclined to agree - just I got an attack of lazyitis and didn't want to hibernate a pc
|
|
|
|
|