Launch Delphi 7 application process in .net

Hi!
I am trying to host a delphi 7 vcl application in a .Net wpf application. Everything works fine, except that modal dialogs do not behave like modal dialogs, the parent window does not turn off.
This is my code:

   class MySimpleDelphiHost : HwndHost
   {
      private Process _appProc;
      public IntPtr hwndHost;

      protected override HandleRef BuildWindowCore(HandleRef hwndParent)
      {
         _appProc = new Process();
         _appProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
         _appProc.StartInfo.FileName = @"MySimpleDelphiApplication.exe";
         _appProc.Start();
         Thread.Sleep(1000);
         hwndHost = Win32API.FindWindow("TMainForm", null);
         int oldStyle = Win32API.GetWindowLong(hwndHost, Win32API.GWL_STYLE);
         Win32API.SetWindowLong(hwndHost, Win32API.GWL_STYLE, (oldStyle | Win32API.WS_CHILD) & ~Win32API.WS_BORDER);

         Win32API.SetParent(hwndHost, hwndParent.Handle);
         Win32API.ShowWindowAsync(hwndHost, Win32API.SW_SHOWMAXIMIZED);

         return new HandleRef(this, hwndHost);
      }

      protected override void DestroyWindowCore(HandleRef hwnd)
      {
         _appProc.Kill();         
      }
   }

If I host any delphi application, this works fine. Any ideas?

I created a demo http://www.easy-share.com/1913154119/SimpleDelphiAppHosting.zip . Sorry for the hosting site.

+3
source share
5 answers

.
VCL TApplication , EnumThreadWindows. Child WPF, EnumThreadWindows .
, Forms.pas TApplication.WndProc. WM_ENABLE

   if TWMEnable(Message).Enabled then
   begin
      if Application.MainForm <> nil then
      begin
         ParentWindow := GetParent(Application.MainForm.Handle);
         if ParentWindow <> 0 then
            EnableWindow(ParentWindow, true);
      end;
      ....
   end else
   begin
     Default;
     if Application.MainForm <> nil then
     begin
       ParentWindow := GetParent(Application.MainForm.Handle);
       if ParentWindow <> 0 then
          EnableWindow(ParentWindow, false);
     end;
     ....

, , , , .

+1

FWIW :

Form.ModalPopupMode = pmExplicit;
Form.ModalParent = ParentForm;
Form.ShowModal;

tApplication Forms,

var 
  OldWndProc: Pointer; 

function NewWndProc(Handle: hWnd; Msg: UINT; PW: WPARAM; PL: LPARAM): LRESULT stdcall; 
begin
  if Msg = WM_ENABLED then 
  begin
     doWhatever();
     result := 1; // handled
  end else 
  result := CallWindowProc(OldWndProc, Handle, Msg, PW, PL); 
end; 

initialization 
  OldWndProc := Pointer(SetWindowLong(Application.Handle, GWL_WNDPROC,    
  LongInt(@NewWndProc))); 
+2

hwndHost = Win32API.FindWindow("TMainForm", null);

, TMainForm ( , ), , , , ,

, TMainForm

TMainForm

MainForm

MainForm//(Application.mainform)

0

delphi #, delphi dll delphi #, delphi exe ( # delphi app) dll fron Delphi app

Delphi , . - GunnarIF

,

Delphi ( )

, project.dpr( )

Application.CreateForm(TForm2, Form2); // form2 is your modal dialogs

now

unit2 //unit 2 modelform

procedure TForm1.Button1Click(Sender: TObject);
var
f: TForm  ;
begin
 F  := TForm2.Create(nil);
 f.ShowModal ;

end;

form2 false ( form2) ( , showmodel)

, , , .

0

. # , , # ( ) , , , vcl ,

just now I checked your demo, I think this is a VCL problem (as you said, it worked in other development tools) even the showmessage function does not disable the main form;

The best answer I can predict

use a DLL, place the vcl form inside the dll and call it from C #, it will work

0
source

Source: https://habr.com/ru/post/1777802/


All Articles