none
MainWindowHandle - SetParent

    Question

  • Hello Everyone,

    i have problem, i'm using this code on vb

    here

          If ((GetAsyncKeyState(Keys.Home) = -32767) AndAlso (Process.GetProcessesByName("notepad").Length > 0)) Then
                Dim process As Process = process.GetProcessesByName("notepad")(0)
                Do While ((process.MainWindowHandle = IntPtr.Zero) OrElse Not IsWindowVisible(process.MainWindowHandle))
                    Thread.Sleep(5)
                    process.Refresh()
                Loop
                process.WaitForInputIdle()
                SetParent(process.MainWindowHandle, Panel1.Handle)
            End If
      Private Const WM_SYSCOMMAND As Integer = 274
        Private Const SC_MAXIMIZE As Integer = 61488
        Dim proc As Process
        <DllImport("user32.dll")> _
        Private Shared Function GetAsyncKeyState(ByVal vKey As Keys) As Short
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function SetParent(ByVal child As IntPtr, ByVal newParent As IntPtr) As IntPtr
        End Function
        <DllImport("user32.dll")> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        End Function
    
    
        <DllImport("user32.dll")> _
        Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

    i have tried to make the same code in C++ but i still had problem here is my code

    literal int WM_SYSCOMMAND = 274;
    	literal int SC_MAXIMIZE = 61488;
    	Process ^proc;
    	[DllImport("user32.dll")]
    	static short GetAsyncKeyState(Keys ^vKey);
    
    	[DllImport("user32.dll")]
    	static IntPtr SetParent(IntPtr child, IntPtr newParent);
    	[DllImport("user32.dll")]
    	static int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
    
    	[return: MarshalAs(UnmanagedType::Bool)]
    	[DllImport("user32.dll")]
    	static bool IsWindowVisible(IntPtr hWnd);


    if ((GetAsyncKeyState(Keys->Home) == -32767) && (Process::GetProcessesByName("notepad")->Length > 0))
    	{
    		Process ^process = process->GetProcessesByName("notepad")[0];
    		while ((process->MainWindowHandle == IntPtr::Zero) || ! (IsWindowVisible(process->MainWindowHandle)))
    		{
    			Thread::Sleep(5);
    			process->Refresh();
    		}
    		process->WaitForInputIdle();
    		SetParent(process->MainWindowHandle, Panel1->Handle);
    	}

    i still have problem on my code, anyhelp would be good.



    • Edited by Cube125 Wednesday, September 03, 2014 1:07 AM
    • Changed type Cube125 Wednesday, September 03, 2014 1:07 AM
    Tuesday, September 02, 2014 10:36 PM

Answers

  • in the import DLL code

    i got this error

    error C2144: syntax error : 'int' should be preceded by ';'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2059: syntax error : 'constant'
    error C2238: unexpected token(s) preceding ';'
    error C2144: syntax error : 'int' should be preceded by ';'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2059: syntax error : 'constant'
    error C2238: unexpected token(s) preceding ';'
    error C3100: 'return' : unknown attribute qualifier

    in the 2nd code the main one

    error C2664: 'IsWindowVisible' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called fatal error C1903: unable to recover from previous error(s); stopping compilatio

    error C2664: 'SetParent' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'

    i got this error without using the first code, so i guess if we fix this error, everything going be okay.


    I am not an expert on DllImport, but to convert from IntPtr to HWND, try

    HWND hWnd = static_cast<HWND>(intPtr.ToPointer());


    David Wilkinson | Visual C++ MVP

    • Marked as answer by Cube125 Wednesday, September 03, 2014 10:55 AM
    Wednesday, September 03, 2014 8:42 AM

All replies

  • i still have problem on my code, anyhelp would be good.

    What problem?

    Edit: Please change your thread type to Ask a Question.


    David Wilkinson | Visual C++ MVP


    • Edited by davewilkMVP Wednesday, September 03, 2014 12:12 AM
    Wednesday, September 03, 2014 12:09 AM
  • in the import DLL code

    i got this error

    error C2144: syntax error : 'int' should be preceded by ';'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2059: syntax error : 'constant'
    error C2238: unexpected token(s) preceding ';'
    error C2144: syntax error : 'int' should be preceded by ';'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2059: syntax error : 'constant'
    error C2238: unexpected token(s) preceding ';'
    error C3100: 'return' : unknown attribute qualifier

    in the 2nd code the main one

    error C2664: 'IsWindowVisible' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called fatal error C1903: unable to recover from previous error(s); stopping compilatio

    error C2664: 'SetParent' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'

    i got this error without using the first code, so i guess if we fix this error, everything going be okay.


    • Edited by Cube125 Wednesday, September 03, 2014 1:19 AM
    Wednesday, September 03, 2014 1:16 AM
  • in the import DLL code

    i got this error

    error C2144: syntax error : 'int' should be preceded by ';'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2059: syntax error : 'constant'
    error C2238: unexpected token(s) preceding ';'
    error C2144: syntax error : 'int' should be preceded by ';'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2059: syntax error : 'constant'
    error C2238: unexpected token(s) preceding ';'
    error C3100: 'return' : unknown attribute qualifier

    in the 2nd code the main one

    error C2664: 'IsWindowVisible' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called fatal error C1903: unable to recover from previous error(s); stopping compilatio

    error C2664: 'SetParent' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'

    i got this error without using the first code, so i guess if we fix this error, everything going be okay.


    I am not an expert on DllImport, but to convert from IntPtr to HWND, try

    HWND hWnd = static_cast<HWND>(intPtr.ToPointer());


    David Wilkinson | Visual C++ MVP

    • Marked as answer by Cube125 Wednesday, September 03, 2014 10:55 AM
    Wednesday, September 03, 2014 8:42 AM
  • i don't need for import DLL anymore

    here is the code that i needed

    Process ^process = process->GetProcessesByName("notepad")[0];
    		
    		{
    			Thread::Sleep(5);
    			process->Refresh();
    		}
    		process->WaitForInputIdle();
    		SetParent(process->MainWindowHandle, panel1->Handle);

    i got error on

    error C2664: 'SetParent' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'

    where should i add "HWND hWnd = static_cast<HWND>(intPtr.ToPointer());" ?

    Wednesday, September 03, 2014 9:08 AM
  • i don't need for import DLL anymore

    here is the code that i needed

    Process ^process = process->GetProcessesByName("notepad")[0];
    		
    		{
    			Thread::Sleep(5);
    			process->Refresh();
    		}
    		process->WaitForInputIdle();
    		SetParent(process->MainWindowHandle, panel1->Handle);

    i got error on

    error C2664: 'SetParent' : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'

    where should i add "HWND hWnd = static_cast<HWND>(intPtr.ToPointer());" ?

    Are you seriously telling me that you do not know how to apply the conversion I showed to your code?

    Clearly, you cannot pass the IntPtr's in process->MainWindowHandle and panel1->Handle directly to SetParent() -- you have to convert them to HWND's first. I would create two intermediate HWND variables, say hWnd and hWndParent (using the conversion I showed you) and pass them to SetParent().


    David Wilkinson | Visual C++ MVP

    Wednesday, September 03, 2014 9:37 AM
  • its works, Thanks mate.
    Wednesday, September 03, 2014 11:03 AM