Thread: CreateRemoteThread in a child process

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    18

    CreateRemoteThread in a child process

    hi,
    i am trying to create a new thread in a child process:
    Code:
    long WINAPI eThread(){
        AllocConsole();
        freopen("CONOUT$", "wb", stdout);
        printf("ok\n");                                               
    }
    Code:
    if( !CreateProcess(
        "C:\\WINDOWS\\System32\\calc.exe",
        NULL,
        NULL,                   // Process handle not inheritable. 
        NULL,                   // Thread handle not inheritable. 
        FALSE,                  // Set handle inheritance to FALSE. 
        0,                      // No creation flags. 
        NULL,                   // Use parent's environment block. 
        NULL,                   // Use parent's starting directory. 
        &si,                    // Pointer to STARTUPINFO structure.
        &pi )                   // Pointer to PROCESS_INFORMATION structure.
    ){
    	printf( "CreateProcess failed (%d).\n", GetLastError() );
    }
    else if(!CreateRemoteThread(pi.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)eThread, 0, 0, 0)){
    	printf( "CreateRemoteThread failed (%d).\n", GetLastError() );
    }
    the calc.exe starts, but exit immediately with a fatal error. do i need other flags?
    the error comes not from the AllocConsole etc - it doesn't matter what eThread executes, it exits all the time.

    thx for help, stev

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    http://msdn.microsoft.com/en-us/libr...37(VS.85).aspx
    Read lpStartAddress parameter description carefully.

    "The function must exist in the remote process."

    gg

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    hm ok. i am at the end of my latin than.
    i have an application that creates a window with a minimum size. it think it does it about the WM_GETMINMAXINFO event. my idea was to overwrite the WndProc but that doesent work if i cant open an thread in the child process.
    i know how to set the size of a window but i dont want to set a static size my self i just want no minimum size.
    [edit]
    is is possible to receive the resize event from the window so that i can set the size?
    Last edited by electrohippi; 11-12-2008 at 04:58 PM. Reason: new idea

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You can receive the message through a windows hook.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    what hook do i have to use ? i just found that:

    WH_CBT

    To write a CBT application, the developer must coordinate the CBT application with the application for which it is written. Windows supplies the WH_CBT hook to make this possible. Windows passes a hook code to the filter function, indicating which event has occurred and the appropriate data for the event.

    HCBT_MOVESIZE

    Windows calls the WH_CBT hook with this hook code when Windows is about to move or size a window, and the user has just finished selecting the new window position or size. In the case of thread-specific hooks, the thread must own the window. If the filter function returns TRUE, the action does not occur.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    the problem is, that the HCBT_MOVESIZE hook will be called if the user has just finished selecting the new window position or size. but that dosent work because there is this damn minimum size. anymore ideas?

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need to get your code running in that process. Then you can sub-class that window and enforce the minimum size. Search "dll injection" for different ways to get your code running within a process.

    gg

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    hi and thx. the subclassing works now with a WH_CBT hook, but now i have problems to catch the WM_GETMINMAXINFO event (i get no output 'in').
    maybe searching for the topwindow by the classname "CicMarshalWndClass" doesn't work. i tried it with GetParent() and GetAncestor(), but that dosent work too.

    Code:
    long oldwinproc;
    LRESULT CALLBACK WndProcOverwrite(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){ // the new window procedure
    	
        HANDLE hPipe = connectToPipe();	
        WritePipe(hPipe, "*"); /// subclassing works
        
        if(msg == WM_GETMINMAXINFO){			
            WritePipe(hPipe, "in"); // !!!
            ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 300;
            ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 300;
            
            CloseHandle(hPipe);
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        CloseHandle(hPipe);
        return CallWindowProc((WNDPROC)oldwinproc, hwnd, msg, wParam, lParam );
    }
    
    LRESULT CALLBACK filterFunc(int nCode, WPARAM wParam, LPARAM lParam){ // WH_CBT hook
        if (nCode == HCBT_CREATEWND){
        	HWND hwnd = (HWND)wParam;
           	
           	if(getModule(hwnd, "notepad.exe")){
                char wndClassname[STRLEN]; RealGetWindowClass(hwnd, wndClassname, STRLEN);
                if(strstr(wndClassname, "CicMarshalWndClass")){                            	 
                    oldwinproc = SetWindowLongPtr(hwnd, GWLP_WNDPROC, (long)WndProcOverwrite);                
                }
          	}
        }
        return CallNextHookEx(g_hHook, nCode, wParam, lParam);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. inter process communcation, parent - child
    By tallan in forum C Programming
    Replies: 5
    Last Post: 02-28-2009, 04:04 AM
  3. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  4. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM