minimal C# not working for Windows API DwmGetWindowAttribute / DWMWA_CLOAKED

  • Thread starter Thread starter Fearless96
  • Start date Start date
F

Fearless96

Guest
I'm trying to get working my minimal C# code for Windows API DwmGetWindowAttribute / DWMWA_CLOAKED. I'm running it in PowerShell but I suspect the issue is far more likely to be the C# code than the tiny bit of PowerShell code. The code runs but DwmGetWindowAttribute always returns hresult -2147024809, "One or more arguments are invalid." Using uint instead of int for all the DWORD types made it more complicated but it didn't help. Does anybody know why this doesn't work?

API references:

https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmgetwindowattribute

https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute

Add-Type API -Namespace Win32 -MemberDefinition @'
[DllImport("dwmapi.dll")]
private static extern int DwmGetWindowAttribute(
IntPtr hwnd,
int dwAttribute,
IntPtr pvAttribute,
int cbAttribute
);
public static int[] GetWndCloak(IntPtr hwnd)
{
const int DWMWA_CLOAKED = 13;
// Get mem for return value (4 bytes)
IntPtr pcloak = Marshal.AllocHGlobal(4);
int hresult = DwmGetWindowAttribute(
hwnd,
DWMWA_CLOAKED,
pcloak,
4
);
int cloak = Marshal.ReadInt32(pcloak);
Marshal.FreeHGlobal(pcloak);
return new[] {hresult, cloak};
}
'@
$proc = Get-Process -Id $pid
$proc.MainWindowHandle.GetType()
$hresult,$cloak = [Win32.API]::GetWndCloak($proc.MainWindowHandle)
$hresult,$cloak



https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmgetwindowattribute

Continue reading...
 
Back
Top