Results 1 to 2 of 2

Thread: [2.0] Win32 Handles From VB6 to C#

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    2

    [2.0] Win32 Handles From VB6 to C#

    Hi everyone. I'm encountering a roadblock with a VB6/C# integration project which I'm hoping someone will have a solution or workaround for.

    Basic Synopsis: I have an existing program written entirely in VB6, and I'm (unsuccessfully) trying to use the VB6 program to open a modal wizard dialog programmed in C#, but I'm unable to pass to the C# application the appropriate Win32 handle of the VB6 application (to make the VB6 program the "owner") and as such the C# program does not run in modal mode.


    In my VB6 application, I call the C# application using a Shell command:

    Code:
    Private Sub mnuImport_Click()
       Shell App.Path & "\ImportWizard.exe " & Me.hWnd, vbNormalFocus
    End Sub
    The Main() of the C# project takes in the hWnd argument and passes it on to the main form:

    Code:
    [ STAThread ]
    static void Main(string[] args)
    {
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
    
       if (args.Length > 0)
       {
          long hWnd = Convert.ToInt64(args[0]);
          Application.Run(new frmWizard(hWnd));
       }
       else
       {
          Application.Run(new frmWizard());
       }
    }
    Finally, in the constructor of the main form that accepts a long value, I attempt to open the form as a modal dialog, which is where the roadblock is:

    Code:
    public frmWizard(long handle)
    {
       InitializeComponent();
       this.ShowDialog(handle); //doesn't work
    }
    Basically, my problem is: the ShowDialog() method only accepts an IWin32Window object, and the only reference to the VB6 window I can give is the value passed from the hWnd property. It seems as though ShowDialog() will only accept a Win32 handle from a form which implements the IWin32Window interface, in which case I may be in trouble.

    Any ideas/feedback would be greatly appreciated.

    Thanks.

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    2

    Re: [2.0] Win32 Handles From VB6 to C#

    I found a solution that seems to work.

    I created a wrapper class which inherits from IWin32Window with a single constructor which takes in an IntPtr value and a read-only property that returns the properly wrapped handle of the window.

    This makes the C# window a modal child of the VB6 parent form.

    Code:
        
    class WindowWrapper : System.Windows.Forms.IWin32Window 
        {
            private IntPtr _hWnd;
            
            public WindowWrapper(IntPtr handle)
            {
                _hWnd = handle;
            }
    
            public IntPtr Handle
            {
                get
                {
                    return _hWnd;
                }
            }
        }
    Then the wizard form constructor I modified thusly:

    Code:
    public frmWizard(long handle)
    {
       InitializeComponent();
       IntPtr parent = new IntPtr(handle);
       this.ShowDialog(new WindowWrapper(parent)); //works
    }
    This solution I got from CodeProject.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width