Host a Delphi 7 application process in .net

HI! I'm trying to host a delphi 7 vcl application in a .Net wpf application. Everything works great except modal dialogs do not behave like modal dialogs, the parent window isn't disabled. This is my code so far:

   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 a none delphi application this works just fine. Any ideas?

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

Answers


FWIW have you tried setting:

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

Also you don't have to change tApplication in the Forms unit, you could do

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))); 

Need Your Help

Android - Adding tabs to a Fragment

android android-fragments tabs android-tabhost fragment-tab-host

I am making a Fragment with tabs but I am having a problem that I cannot resolve. When adding the tabs I must set a content with setContent() but I just can set an Activity or a layout and I want t...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.