[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);
<DllImport("user32.dll", ExactSpelling:=True, CharSet:=CharSet.Auto)> _
Public Shared Function GetParent(ByVal hWnd As IntPtr) As IntPtr
End Function
[DllImport("user32.dll", ExactSpelling := true, CharSet := CharSet.Auto)]
class method GetParent(hWnd: IntPtr): IntPtr; external;
Public Declare Function GetParent Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd as LongPtr) as LongPtr
Please add some!
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:
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()'
The ManagedWindowsApi project (http://mwinapi.sourceforge.net) provides a
class ManagedWinapi.SystemWindow that has a Parent property.