I don't remember having any problem finding a window in older Windows OS's, but, I'm not succeeding in Windows 8.1 Update 2 OS, using PowerShell v4.0.
This is the PowerShell v4.0 code I'm using (pretty much trivial):
$sig=@'
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(String sClassName, String sAppName);
'@
$fw = Add-Type -Namespace Win32 -Name Funcs -MemberDefinition $sig -PassThru
$wname='Form1' # any existing window name
$fw::FindWindow($null -as [String], $wname) # returns 0, always!
The last command returns 0, always.
Changing the DllImport attribute to
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
does not change anything; 0 is returned the same way.
Interesting to notice that the equivalent code in C#, returns the correct HWND value.
Does anyone know what's wrong (and how to fix) the PowerShell v4.0 code above?