ForumFree

Aiuto riguardo all'articolo "Windows Handles, etc..."

« Older   Newer »
  Share  
intoinside
icon13  view post Posted on 11/12/2008, 16:23




Ciao a tutti,
ho realizzato un'applicazione "principale" che manda in esecuzione alcune istanze di un'applicazione "secondaria" in modo che siano visibili all'interno di un TPanel della prima. Si verifica però un problema: nel momento in cui creo una nuova istanza tramite l'istruzione

CODICE
if not ExecuteProcess(PChar(name), 0, False, True, SW_HIDE) then
 begin
//Gestione dell'errore
 end;


nella procedura StartApplication(.) si apre subito il form nonostante sia stato dichiarato "Visible := False;" e sia stato impostato il flag "SW_HIDE" nella chiamata ExecuteProcess(.)
Avrei bisogno che l'applicazione appena creata non fosse visibile o che almeno non salga in primo piano e che non rubi il fuoco dell'applicazione. Come posso risolvere?
Di seguito ho inserito il blocco relativo alla funzione ExecuteProcess(.)

Grazie a tutti

CODICE
function ExecuteProcess(FileName: string; BitMask: Integer; Synch: Boolean; WaitForIdle: Boolean; nCmdShow: Integer): Boolean;
var
 zAppName: array[0..512] of Char;
 zCurDir: array[0..255] of Char;
 WorkDir: string;
 StartupInfo: TStartupInfo;
 ProcessInfo: TProcessInformation;
 Closed: Boolean;
 exitCode: Cardinal;
begin
 Result := False;

 StrPCopy(zAppName, FileName);
 GetDir(0, WorkDir);
 StrPCopy(zCurDir, WorkDir);
 FillChar(ProcessInfo, SizeOf(ProcessInfo), #0);
 FillChar(StartupInfo, SizeOf(StartupInfo), #0);
 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
 StartupInfo.wShowWindow := nCmdShow;

 try
   if not CreateProcess(nil,
     zAppName, // pointer to command line string
     nil, // pointer to process security attributes
     nil, // pointer to thread security attributes
     False, // handle inheritance flag
     CREATE_NEW_CONSOLE or // creation flags
     BELOW_NORMAL_PRIORITY_CLASS,
     nil, //pointer to new environment block
     nil, // pointer to current directory name
     StartupInfo, // pointer to STARTUPINFO
     ProcessInfo) then // pointer to PROCESS_INF
   begin
     Exit;
   end
   else
   begin
     //eseguire il processo sull' insieme di CPU specificato
     if BitMask <> 0 then
     begin
       SetProcessAffinityMask(ProcessInfo.hProcess, BitMask);
     end;

     if WaitForIdle then
       if WaitForInputIdle(ProcessInfo.hProcess, INFINITE) <> 0 then
         Exit;

     if (Synch = True) then //se voglio una esecuzione Sincrona
     begin
       Closed:= False;
       repeat
         case WaitForSingleObject(
           ProcessInfo.hProcess, 100) of
             WAIT_OBJECT_0 : Closed := True;
             WAIT_FAILED : Exit; //RaiseLastWin32Error;
         end;
         Application.ProcessMessages;
       until (Closed);
       GetExitCodeProcess(ProcessInfo.hProcess, exitCode);

       if exitCode <> 0 then
         Exit;
     end;

   end;
   Result := True;

 finally
   if ProcessInfo.hThread <> 0 then
     CloseHandle(ProcessInfo.hThread);
   if ProcessInfo.hProcess <> 0 then
   begin
     CloseHandle(ProcessInfo.hProcess);
     if not Result then  //killo il processo se qualcosa è andato storto in modo da non averlo più tra le scatole
       TerminateProcess(ProcessInfo.hProcess, exitCode);
   end;
 end;
end; {ExecuteProcess}
 
Top
DigitX32
view post Posted on 16/12/2008, 19:24




in pratica stai facendo riferimento all'articolo

http://pasotech.altervista.org/delphi/articolo125.htm


non ho capito bene cosa vorresti cambiare comunque dovresti studiare il codice della StartApplication

CODICE
procedure TFormSample.StartApplication(name,classname,title : string);
var
       i: integer;
       ptitle,pclassname: pchar;
       buf: array[0..255] of char;
begin
 if hWndApplication <> 0 then
   PostMessage(hWndApplication,WM_QUIT,0,0);

 //Carlo Pasolini : 21/07/08
 if not ExecuteProcess(PChar(name), 0, False, True, SW_HIDE) then
   begin
           PanelApp.caption := format('failed starting of application %­s ',[name]);
           exit;
   end;
 (*hApplication := WinExec(pchar(name),SW_HIDE);
 if hApplication < 32 then begin
         PanelApp.caption := format('application %­s not found',[name]);
         exit;
 end;*)

 if classname <> '' then
   pclassname := pchar(classname)
 else
   pclassname := pointer(0);
 if title <> '' then
   ptitle := pchar(title)
 else
   ptitle := pointer(0);

 //Carlo Pasolini : 21/07/08
 hWndApplication := FindWindow(pclassname,ptitle);
 if hWndApplication = 0 then
   Exit;

 (*for i := 0 to 20 do begin
         hWndApplication := FindWindow(pclassname,ptitle);
         if hWndApplication <> 0 then break;
         sleep(100);
 end; *)

 GetClassName(hWndApplication,buf,sizeof(buf));
 labelClassName.caption:= buf;

 SetMenu(handle,GetMenu(hWndApplication)); // Apply the applications menu to our form
 DrawMenuBar(handle);                      // Draw it
 if CheckBoxAsChild.checked then
   begin
           windows.SetParent(hWndApplication,PanelApp.handle);
           SetWindowLong(hWndApplication,GWL_STYLE,WS_CHILD);  // Make the application a child
   end
 else
         windows.SetParent(hWndApplication,PanelApp.handle);   // Move it to our panel
 ShowWindow(hWndApplication,sw_minimize);                // finally, draw it
 ShowWindow(hWndApplication,SW_MAXIMIZE);                // finally, draw it
 MoveWIndow(hWndApplication,0,0,PanelApp.width,PanelApp.height,FALSE); // Set it to the panels size
 InvalidateRect(hWndApplication,pointer(0),true);
 PanelAppResize(self);

end;


fai delle prove sulle funzioni che vengono chiamate dopo la ExecuteProcess
 
Top
1 replies since 11/12/2008, 16:23   102 views
  Share