.net - SetWindowLong悬挂

标签 .net winapi vb6 interop pinvoke

为什么SetWindowLong(myForm.hWnd, GWL_HWNDPARENT, parentHwnd)挂起?

通过执行这三个步骤,我可以始终如一地重现此问题。

  • 创建.NET表单
  • 初始化WaitWindow COM对象,并在传递.NET Forms Handle的同时调用COM对象上的ShowWindow
  • 在VB6中调用SetWindowLong方法

  • C#Windows应用程序(挂起)
    private static void Main(string[] args)
    {
          Form form = new Form();
          form.Show();
    
          Interop.WaitWindow waitWindow = new Interop.WaitWindow();
          waitWindow.ShowWindow(form.Handle.ToInt32(), Language.RISEnglish);
    }
    

    C#控制台应用程序(不挂起)
    private static void Main(string[] args)
    {
          IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;     
    
          Interop.WaitWindow waitWindow = new Interop.WaitWindow();
          waitWindow.ShowWindow(handle.ToInt32(), Language.RISEnglish);
    }
    

    VB6代码段
    Public Sub ShowWindow(ByVal parentHwnd As Long, ByVal language As Language)
    
        SetWindowLong(myForm.hWnd, GWL_HWNDPARENT, parentHwnd)  'Hangs Here
        CenterWindow (parentHwnd)
    
        myForm.ShowRetrieving (language)
        myForm.Show (vbModal)
    End Sub
    

    真的会感谢您的帮助:)

    编辑

    我确实知道不应调用SetWIndowLong来更改父级,但是我试图理解为什么只有在使用.NET窗体句柄时它才会挂起。

    EDIT2

    我现在认为问题与SetWindowLong无关,而与实际的句柄本身有关。我仍在调查中,但是当我从.NET调用VB6代码时,它似乎创建了一个RPC线程。我不确定,但是我感觉到它与跨线程问题有关。

    最佳答案

    MSDN文档明确指出

    You must not call SetWindowLong with the GWL_HWNDPARENT index to change the parent of a child window. Instead, use the SetParent function.

    关于.net - SetWindowLong悬挂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10216414/

    相关文章:

    c# - 使用空对象初始值设定项有什么缺点吗?

    c++ - Windows 消息有多健壮?

    vb.net - 如何调用com+组件?

    .net - 文件版本与程序集版本

    .net - 样式 TargetType 属性问题

    c# - 是否可以知道某个方法是否在应用程序中被调用?

    c# - 找不到“Newtonsoft”

    c++ - 将 SNMP OID 转换为 Win32 上的描述性名称

    c++ - 使用用户密码加密

    C#、VB6 和 Decimal 数据类型