我要把3DSMax加到我的MDIForm中来:
pid = Shell(strAppCmd, vbNormalFocus)
If pid = 0 Then
    Exit Sub
End IfmWnd = OpenProcess(SYNCHRONIZE, 0, pid)
Card_OldParent& = SetParent(mWnd, Me.hwnd)开机第一次运行肯定失败,以后有一定的失败几率,如果经常运行,就没有问题了(好像是刚开机3DSMax第一次运行,资源还没有加载,所以失败了)
但我跟踪了SetParent函数的返回值,有两种可能:6553?和3DS的hwnd,但两种返回值又和是否成功执行没有关系,我已经试验了好多方法,比如WaitForSingleObj等,但都不能解决,只好求助大家
(注:strAppCmd就是3ds的路径文件名,如果换成记事本等小程序,就没有任何问题)

解决方案 »

  1.   

    先调用3dmax
    再Findwindow就可以准确的找到程序的句柄了!:p
    ----------------------------------
    夜已深,还有什么人,像我这样,醒着在编程海纳百川,有容乃大;
    壁立千仞,无欲则刚。
      

  2.   

    Function InstanceToWnd(ByVal target_pid As Long) As Long
        Dim test_hwnd As Long
        Dim test_pid As Long
        Dim test_thread_id As Long    ' Get the first window handle.
        test_hwnd = FindWindow(ByVal 0&, ByVal 0&)    ' Loop until we find the target or we run out
        ' of windows.
        Do While test_hwnd <> 0
            ' See if this window has a parent. If not,
            ' it is a top-level window.
            If GetParent(test_hwnd) = 0 Then
                ' This is a top-level window. See if
                ' it has the target instance handle.
                test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)            If test_pid = target_pid Then
                    ' This is the target.
                    InstanceToWnd = test_hwnd
                    Exit Do
                End If
            End If        ' Examine the next window.
            test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
        Loop
    End Function我这样找在刚开机后第一次执行还是不行
      

  3.   

    一直没有解决,但CSDN要强制结贴怎么办?
      

  4.   

    pid = Shell(strAppCmd, vbNormalFocus)
    If pid = 0 Then
        Exit Sub
    End IfmWnd = OpenProcess(SYNCHRONIZE, 0, pid)
    Card_OldParent& = SetParent(mWnd, Me.hwnd)-------------------------------------------什么?OpenProcess 返回的是窗口句柄?
      

  5.   

    我猜测3dmax加载的速度较慢,shell并不是同步执行的,因此mwnd尚未得到,当然setparent出错了
      

  6.   

    那要看你的3DS加载速度又没有程序运行速度快。第一次加载3DS时所有的资源和DLL第一次调入,速度非常慢,窗体更没来得及创建。而Shell()是立即返回的。将FindWindow放入一个循环中就可以解决问题。注意为了防止死机,最好加个超时限制。
      

  7.   

    不好意思,是OpenProcess函数而不是FindWindow。
      

  8.   

    要等待另一个进程加载的话,
    用  WaitForInputIdle  API函数。而且,强烈建议用 CreateProcess 函数!
      

  9.   

    备注,本贴没有解决,但是被CSDN强制结贴!!