-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Error creating window handle. #4418
Comments
Was this scenario working fine with the old MSAL version before upgrading? Which version were you using before? Or it started happening when using cmd.exe outside of Windows terminal? |
Hi @petrochuk - I don't think we made any changes in that area of the code. How do you configure PublicClientApplicaiton and AcquireTokenInteractive? It is possible that a Windows update introduced some regression. But in general Terminal's way of handling windows is pretty different. Read on..
|
@bgavrilMS, thanks for your reply.
|
Hi @petrochuk, does the issue repro if you use the logic below for finding handle for terminal window? enum GetAncestorFlags
{
GetParent = 1,
GetRoot = 2,
/// <summary>
/// Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent.
/// </summary>
GetRootOwner = 3
}
/// <summary>
/// Retrieves the handle to the ancestor of the specified window.
/// </summary>
/// <param name="hwnd">A handle to the window whose ancestor is to be retrieved.
/// If this parameter is the desktop window, the function returns NULL. </param>
/// <param name="flags">The ancestor to be retrieved.</param>
/// <returns>The return value is the handle to the ancestor window.</returns>
[DllImport("user32.dll", ExactSpelling = true)]
static extern IntPtr GetAncestor(IntPtr hwnd, GetAncestorFlags flags);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
// This is your window handle!
public IntPtr GetConsoleOrTerminalWindow()
{
IntPtr consoleHandle = GetConsoleWindow();
IntPtr handle = GetAncestor(consoleHandle, GetAncestorFlags.GetRootOwner );
return handle;
} |
We used the code below, but your suggestion looks much more efficient. I am closing the issue. /// <summary>
/// Gets the parent process of a specified process.
/// </summary>
/// <param name="handle">The process handle.</param>
/// <returns>An instance of the Process class.</returns>
public static Process GetParentProcess(IntPtr handle)
{
var pbi = default(ProcessInformation);
int status = NtQueryInformationProcess(handle, 0, ref pbi, Marshal.SizeOf(pbi), out var _);
if (status != 0)
throw new Win32Exception(status);
try
{
return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32());
}
catch (ArgumentException)
{
// not found
#pragma warning disable CS8603 // Possible null reference return.
return null;
#pragma warning restore CS8603 // Possible null reference return.
}
}
/// <summary>
/// Searches the process tree for the first process with a window.
/// </summary>
/// <returns></returns>
public static IntPtr GetProcessMainWindowHandle()
{
// Walk up the process tree until we find a process with a window
var parentProcess = GetParentProcess();
while (parentProcess != null && parentProcess.MainWindowHandle == IntPtr.Zero)
{
parentProcess = GetParentProcess(parentProcess.Id);
}
#pragma warning disable CS8602 // Dereference of a possibly null reference.
return parentProcess.MainWindowHandle;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
} |
Library version used
4.57.0
.NET version
.NET Framework 4.8.9181.0
processArchitecture: X64
osArchitecture: X64
Scenario
PublicClient - desktop app
Is this a new or an existing app?
The app is in production, and I have upgraded to a new version of MSAL
Issue description and reproduction steps
PublicClient.AcquireTokenInteractive call throws System.ComponentModel.Win32Exception "Error creating window handle."
We observed this is happening when our console application is launched by cmd.exe process which is hosted by Windows Terminal.
Identity provider
Microsoft Entra ID (Work and School accounts and Personal Microsoft accounts)
Solution and workarounds
use cmd.exe outside of Windows Terminal
sounds like related to this one: AzureAD/MSAL.PS#58
The text was updated successfully, but these errors were encountered: