Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get the window text using the code shown below. When I try to log why I am not getting the value from GetWindowText sometimes I get a 183 error. But the strange thing is that when I use VS debug it always gets the value. But after creating a built setup and when I will install that, it's not able to get the value.

I put logs to find out where is the issue is, but when I restart the app it starts working again and sometimes when I restart my laptop the issue does not occur. But why is it not working? Is there any alternative way to get the text of Windows? What could be a possible solution for this?

Restarting the app is not a possible solution because I have to authenticate the user. Though I have checked if it is able to get value after restarting of app, but sometimes I need to restart my laptop as well. It's a strange case. If you have any fix for it, please let me know.

What I have tried:

C#
[DllImport("user32.dll")]
     static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

C#
if (GetWindowText(handle, Buff, nChars) > 0)
            {
                int err = Marshal.GetLastWin32Error();
                LogWriter.Log("Window_Error :" + err);
                LogWriter.Log("GetWindowText_Buff:" + "|| "+Buff+" ||"+" handle:"+"|| "+handle+" ||"+" nChars"+"|| "+nChars+" ||");
                return Buff.ToString();
                
            }

C#
if (GetWindowText(handle, Buff, nChars) <= 0)
          {
              int err = Marshal.GetLastWin32Error();
              LogWriter.Log("Window_Error :" + err);
          }
          return null;

Mostly in log text file I get error 183 GetWindowText_Buff is Null but if I restart the application I do not get this issue. However, this is not a solution because in my app I have to authenticate user.

[01-21-2022 19:07:33]	GetWindowText_Buff:||  || handle:|| 66734 || nChars|| 256 ||
[01-21-2022 19:07:33]	Window_Error :183
Posted
Updated 24-Jan-22 5:45am
v2

If you Google "Window_Error :183" you get this description: "Cannot create a file when that file already exists. (Code 183)"
Which means it's not the call to GetWindowText but the logging of the problem that is causing the error.

Check your LogWriter class and see what that is doing.
   
VERSION 2

(1)
I don't trust your error code.

There are two functions that provide error information: GetLastError and GetLastWin32Error.

The former returns the latest Win32 error, but that might be associated to something other than your explicit Win32 p/invoke operation (e.g. when another thread intervenes and somehow calls Win32).

The latter returns the error code from the most recent Win32 call that had
the DllImport attribute SetLastError=true.

Since you didn't use the attribute, the error code returned does not belong to your Win32 call.

Using GetLastError (which often gives the right error code) is better than using GetLastWin32Error on a Win32 that was declared without the attribute (as that always returns wrong error codes). However GetLastWin32Error with the attribute clearly is best.

For more info, see e.g. If you are going to call Marshal.GetLastWin32Error, the function whose error you're retrieving had better have SetLastError=true - The Old New Thing[^]

(2)
You did not show the code that assigns some value to IntPtr handle; chances are that is where the problem is.

(3)
You did not show the creation of StringBuilder Buff; it should have a preallocated size ("capacity") equal to (or exceeding) whatever value nChars has.

(2)+(3) = whatever is wrong sits just before the code you have shown us!

:)
   
v6
Comments
AshutoshShine 1-Apr-22 7:32am
   
thanks for your valuable suggestion actually after using [DllImport("user32.dll", CharSet= CharSet.Auto, SetLastError= true)]

instead of [DllImport("user32.dll")] in my code i am not getting the issue anymore


//[DllImport("user32.dll")]
[DllImport("user32.dll", CharSet= CharSet.Auto, SetLastError= true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900