Jump to content
Tuts 4 You

CreateProcess in Thread problem


LCF-AT

Recommended Posts

Hi guys,

have a new problem and need some advice.

Problem: I created a thread and in this thread I also use a CreateProcess API to start a exe file and right after this API in my thread comes a loop to write bytes into hStdOutput handlle into new created process.All working so far but the problem is this...if the new created process does run and I do exit this process manually with mouse button then the remaining codes / following codes will not executed anymore.So thats the problem and I dont know why.I checked this also in Olly and did set soft BPs right after the loop and on thread exit but it will never reached.Does anyone have a idea why my thread will no more executed?Below a short code of my thread.

    mov SECURITY.nLength,sizeof SECURITY
    mov SECURITY.bInheritHandle,TRUE
    mov SECURITY.lpSecurityDescriptor,NULL
    invoke CreatePipe,addr HREAD,addr HWRITE,addr SECURITY,0
    .if eax == 0h
    invoke MessageBox,NULL,chr$("Can't create a pipe!"),chr$("Problem!"),MB_ICONWARNING
    mov eax, -1h
    ret
    .endif
    mov eax,HWRITE
    mov PIPEHANDLE,eax
    invoke SetHandleInformation,HWRITE,HANDLE_FLAG_INHERIT,0
    .if eax == 0h
    invoke MessageBox,NULL,chr$("Can't set handle information!"),chr$("Problem!"),MB_ICONWARNING
CLOSE:    
    invoke CloseHandle,HWRITE
    invoke CloseHandle,HREAD
    mov eax, -1h    
    ret     
   .endif    
    invoke RtlZeroMemory, addr startupinfo1, sizeof startupinfo1
    invoke RtlZeroMemory, addr pinfo, sizeof pinfo
    mov startupinfo1.cb,sizeof STARTUPINFO
    mov eax, HREAD
    mov startupinfo1.hStdInput,eax
    mov eax, PIPEHANDLE
    mov startupinfo1.hStdOutput,eax
    mov startupinfo1.hStdError,eax
    mov startupinfo1.dwFlags, STARTF_USESTDHANDLES    	
    invoke CreateProcess,NULL ,offset MPC_PLAYPATH ,NULL,NULL,TRUE,NULL,NULL,NULL,addr startupinfo1,addr pinfo
    .if eax == 0h
    invoke MessageBox,NULL,chr$("Can't create process!"),chr$("Problem!"),MB_ICONWARNING
    jmp CLOSE      
    .endif	


LOOP:
....
.repeat

WriteFile (nsize)
FlushFileBuffers

.until  nsize==0

...Rest of my code what gets no more accessed

You see what the problem could be in that case and why I not get reached the code after loop?Also in the loop I cant stop anyymore so it seems the thread was just closed or anything like this etc.Maybe you can see the problem and help.

Thank you

Link to comment

CreateProcess returns without waiting for the new process to finish its initialization.

I suggest using WaitForInputIdle function to wait until the new process has finished its initialization.

This is just a possible cause and may be wrong.

Link to comment

Hi again,

so the problem seems to be at the write API.It hangs inside and does not come back anymore if I close or stop the created file.I also tried to add GetExitCodeProcess / GetExitCodeThread before write API but this also dont work and gives still_active back.So somehow I need to check whether I can still write on the handle or not.All these problems I dont get using ShellExecuteEx API but in this case I cant use it.

Has anyone any another hints?

greetz

Link to comment

Hi again,

ok I found the problem.So I forgot to close the hStdInput handle right after CreateProcess API. :) Now its working and if I exit the player manually then it will also come back from WriteFile API and the code after in my thread gets executed / continue as it should be. :)

greetz

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...