-
February 16th, 2011, 02:02 PM
#1
[RESOLVED] ResumeThread() doesn't work
Hi, what this application does (or atleast should do) is to run notepad.exe in a suspended state, and when I input '1' it resumes notepad. I'm not sure ResumeThread works for applications too though... When I run the program it says "ResumeThread() error: 6". The process ID and handle are found, so I don't know where does this error come from... Here's the code:
Code:
#include <stdio.h>
#include <windows.h>
#include <Tlhelp32.h>
DWORD FindProcessId( char *processName )
{
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ( processesSnapshot == INVALID_HANDLE_VALUE )
return 0;
Process32First(processesSnapshot, &processInfo);
if ( strcmp( processName, processInfo.szExeFile ) == 0 )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
while ( Process32Next(processesSnapshot, &processInfo) )
{
if ( strcmp( processName, processInfo.szExeFile ) == 0 )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
}
CloseHandle(processesSnapshot);
return 0;
}
int main( void )
{
char lpApplicationName[ ] = "notepad.exe";
PROCESS_INFORMATION pi;
STARTUPINFO info = { sizeof(info) };
HANDLE hwnd;
int flag;
if ( CreateProcess( lpApplicationName, NULL, NULL, NULL, FALSE,
CREATE_SUSPENDED, NULL, NULL, &info, &pi) == 0)
printf( "Erro ao criar processo - erro %d.\n", GetLastError() );
Sleep(5000);
hwnd = OpenProcess( 0x0800, FALSE, FindProcessId( "notepad.exe" ) );
if ( hwnd == NULL )
printf( "Erro ao abrir processo - erro %d\n", GetLastError() );
scanf( "%d", &flag );
if ( flag == 1 )
{
if ( ResumeThread( hwnd ) == -1 )
printf( "ResumeThread() error: %d\n", GetLastError() );
}
system( "PAUSE" );
}
-
February 16th, 2011, 02:06 PM
#2
Re: ResumeThread() doesn't work
I forgot to say that notepad.exe is in the same directory as this app, so that's not the problem,
-
February 16th, 2011, 04:27 PM
#3
Re: ResumeThread() doesn't work
There are serious mistakes here:
1. OpenProcess() returns process handle, not a thread handle, so you cannot use it for thread functions like ResumeThread and others.
2. Why do you search for the process that you jast have started with CreateProcess? Have a look at the PROCESS_INFORMATION pi / it contains all what you need: process handle, thread handles, ...
3. You should not use Sleep() after CreateProcess call. if you wnat to be sure the spawned process is ready fo |user inpurt" - use WaitForInputIdle
Victor Nijegorodov
-
February 17th, 2011, 10:52 AM
#4
Re: ResumeThread() doesn't work
OpenProcess() returns process handle, not a thread handle, so you cannot use it for thread functions like ResumeThread and others.
Ok, that's what I suspected :P. Then what function should I use to "pause" applications, so I can resume them when I want, if that exists?
-
February 17th, 2011, 11:04 AM
#5
Re: ResumeThread() doesn't work
You cannot "pause" the whole application, only threads can be suspended/resumed.
As for your particular problem: you create the process with the primary thread being in a suspended state, and this thread does not run (and the whole application does not either) until the ResumeThread function is called.
So call ResumeThread and pass in the handle of the primary thread of this process.
As I already wrote you'll find process/thread handles in the PROCESS_INFORMATION structure.
Victor Nijegorodov
-
February 17th, 2011, 06:49 PM
#6
Re: ResumeThread() doesn't work
Thanks, finally got it working
-
February 18th, 2011, 01:50 AM
#7
Re: [RESOLVED] ResumeThread() doesn't work
You are welcome!
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|