CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    32

    [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" );     
    }

  2. #2
    Join Date
    Feb 2011
    Posts
    32

    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,

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  4. #4
    Join Date
    Feb 2011
    Posts
    32

    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?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  6. #6
    Join Date
    Feb 2011
    Posts
    32

    Talking Re: ResumeThread() doesn't work

    Thanks, finally got it working

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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
  •  





Click Here to Expand Forum to Full Width

Featured