troubleshooting Question

Start process on new desktop!

Avatar of spasa
spasa asked on
Delphi
5 Comments1 Solution1246 ViewsLast Modified:
I make 2 applications. Proggram A create new desktop-ND after that create new process B, switch to new desktop and waitforsingleobject (B) to terminate. Proggram B is user interface for desktop ND. Both applications works perfect.
PROBLEM: I copy code from application A to my new service application and start service. After create desktop and process - app. raise error: THE APPLICATION (B) FAILED TO INITIALIZE PROPERLY (0xc0000142). CLICK ON OK TO TERMINATE THE APPLICATION.
Part of code in service app.:
procedure StartProcessInNewDeskTopAndWait;
var StartInfo: TStartupInfo;
    ProcInfo: TProcessInformation;
    PR: boolean;
    hMainD, hDeskTop: Thandle;
begin
  hMainD:=0;
  GetStartUpInfo(StartInfo);
  hGlavniD:=GetThreadDesktop(GetCurrentThreadID);
  hDeskTop:=CreateDesktop(PChar('ND'), nil, nil, DF_ALLOWOTHERACCOUNTHOOK, MAXIMUM_ALLOWED, nil);
   try
      FillChar(StartInfo, SizeOf(StartInfo), 0);
      with StartInfo do
      begin
        cb := SizeOf(StartUpInfo);
        dwFlags := STARTF_USESHOWWINDOW;
        wShowWindow := SW_SHOW;
        lpDesktop := PChar('ND');
      end;
      PR:=false;
      PR:=CreateProcess(PChar('c:\B.exe'),nil,nil,nil,false,CREATE_NEW_CONSOLE or
NORMAL_PRIORITY_CLASS, nil,PChar(ExtractFilePath('c:\B.exe')), StartInfo, ProcInfo);
      if RR then begin
        SwitchDesktop(hDeskTop);
        WaitForSingleObject(ProcInfo.hProcess, INFINITE);
        CloseHandle(ProcInfo.hProcess);
        CloseHandle(ProcInfo.hThread);
        SwitchDesktop(GetThreadDesktop(GetCurrentThreadID));
      end;
    except
      SwitchDesktop(GetThreadDesktop(GetCurrentThreadID));
      CloseDesktop(hDeskTop);
      ShowMessage('Error');
    end;
  end;
end;
ASKER CERTIFIED SOLUTION
raidos

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros