1,105,363 Community Members

Convert Object to IntPtr

Member Avatar
AndrewP7
Newbie Poster
4 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

I am trying to convert an object to IntPtr so that I can pass it to the Win32 function GetWindowRect. I have used the code below, but my program just hangs

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
       static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
     public int Left;
     public int Top;
     public int Right;
     public int Bottom;
}

RECT rct;
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(IE));
Marshal.StructureToPtr(IE, pt, true);
GetWindowRect(pt, out rct);

IE is of type object

Hope someone can help
Thanks

Member Avatar
sknake
Senior Poster
3,957 posts since Feb 2009
Reputation Points: 1,620 [?]
Q&As Helped to Solve: 747 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

This works fine:

[DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
      public int Left;
      public int Top;
      public int Right;
      public int Bottom;
    }


    private void button1_Click(object sender, EventArgs e)
    {
      RECT rct;
      bool b = GetWindowRect(this.Handle, out rct);
      System.Diagnostics.Debugger.Break();
    }

I changed ref RECT lpRect to out RECT lpRect . You need to declare it with "out" and not "ref". Also what is IE? You said it is an "object" but the first parameter of the win32 should be a window handle.

Member Avatar
AndrewP7
Newbie Poster
4 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This works fine:

[DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
      public int Left;
      public int Top;
      public int Right;
      public int Bottom;
    }


    private void button1_Click(object sender, EventArgs e)
    {
      RECT rct;
      bool b = GetWindowRect(this.Handle, out rct);
      System.Diagnostics.Debugger.Break();
    }

I changed ref RECT lpRect to out RECT lpRect . You need to declare it with "out" and not "ref". Also what is IE? You said it is an "object" but the first parameter of the win32 should be a window handle.

IE is SHDocVw.InternetExplorer.
I am using the DocumentComplete event which only allows you to access an instance of Internet Explorer as an object (and the URL of the loaded document).

I want to convert the object to an IntPtr without having to use FindWindow to get the handle of the Internet Explorer window.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article