Receives a question mark when trying to see active window open in my program
-
hi I built Software that grabbing weight and throw into open window where the cursor is. Everything worked out well - I only have one problem annoying Receives a question mark **(?)** when I open **Word**. and Then the software hangs not recognize the window properly. when i open Word i see Word? - 123.docx for example. And even if I remove the question mark, the software is still stuck in this case. my code: [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); private string GetActiveWindowTitle() { const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); IntPtr handle = GetForegroundWindow(); if (GetWindowText(handle, Buff, nChars) > 0) { return Buff.ToString(); } return null; } [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); public void Start(string NAME) { MSG = lblMSG.Text.Trim(); IntPtr zero = IntPtr.Zero; for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++) { Thread.Sleep(500); zero = FindWindow(null, NAME); } if (zero != IntPtr.Zero) { . . . } } what is the problem ? how to fix it ? thanks
-
hi I built Software that grabbing weight and throw into open window where the cursor is. Everything worked out well - I only have one problem annoying Receives a question mark **(?)** when I open **Word**. and Then the software hangs not recognize the window properly. when i open Word i see Word? - 123.docx for example. And even if I remove the question mark, the software is still stuck in this case. my code: [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); private string GetActiveWindowTitle() { const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); IntPtr handle = GetForegroundWindow(); if (GetWindowText(handle, Buff, nChars) > 0) { return Buff.ToString(); } return null; } [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); public void Start(string NAME) { MSG = lblMSG.Text.Trim(); IntPtr zero = IntPtr.Zero; for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++) { Thread.Sleep(500); zero = FindWindow(null, NAME); } if (zero != IntPtr.Zero) { . . . } } what is the problem ? how to fix it ? thanks
Are you sure that it is a question-mark, and not another character that it cannot/will not display?
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
Try to add this attribute to your P/invokes and see if it helps.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)