Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


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

GetParent (user32)
 
.
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

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 PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions