getparent (user32)
Last changed: -78.110.196.168

.
Summary

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

Notes:

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;
}

Note:

For the sample code above, FxCop reports:

DoNotIndirectlyExposeMethodsWithLinkDemands

'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:

->'NativeMethods.GetParentSafe(IntPtr)'

->'NativeMethods.GetParentSafe(IntPtr)'

->'MyClass.MyMethod.get()'

Alternative Managed API:

The ManagedWindowsApi project (http://mwinapi.sourceforge.net) provides a

class ManagedWinapi.SystemWindow that has a Parent property.

Documentation
GetParent on MSDN