Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
GetParent (user32)

C# Signature:

[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);

VB.NET Signature:

<DllImport("user32.dll", ExactSpelling:=True, CharSet:=CharSet.Auto)> _
Public Shared Function GetParent(ByVal hWnd As IntPtr) As IntPtr
End Function

Oxygene.NET Signature:

[DllImport("user32.dll", ExactSpelling := true, CharSet := CharSet.Auto)]
class method GetParent(hWnd: IntPtr): IntPtr; external;

VB Signature:

Public Declare Function GetParent Lib "user32" _
          (ByVal hWnd As Long) As Long

VBA7 Signature:

Public Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd as LongPtr) as LongPtr


GetParent sets LastError

Tips & Tricks:

Please add some!

Sample Code:

IntPtr GetParentSafe(IntPtr handle) {
   IntPtr result = GetParent(handle);
   if(result == IntPtr.Zero) {
       // An error occured
       throw new Win32Exception(Marshal.GetLastWin32Error());
   return result;


For the sample code above, FxCop reports:


'NativeMethods.GetParentSafe(IntPtr)' calls into 'Marshal.GetLastWin32Error()' which has a LinkDemand. By making this call, 'Marshal.GetLastWin32Error()' is indirectly exposed to user code. Review the following protection:




Alternative Managed API:

The ManagedWindowsApi project ( provides a

class ManagedWinapi.SystemWindow that has a Parent property.

GetParent on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version