GetClassName()

  • Thread starter Thread starter stephen.garner
  • Start date Start date
S

stephen.garner

Hi,

I am using the

IntPtr FindWindow(string lpClassName, string lpWindowName);

to get the handle for a window (notepad in this case), I am then using

int GetClassName(IntPtr hWnd, out StringBuilder lpClassName, int
nMaxCount);

to get the name of this window. Now it all works fine but as prove of
concept i want to print the name of the class to console. The
GetClassName returns 7 (Specifies the length, in TCHAR, of the buffer
pointed to by the lpClassName parameter) which would make sense as
notepad class name is Notepad. I'm having problems printing this to
console. It returns 3 characters to the console. I declared a local
StringBuilder class_name and passed that as an out parameter in my
method.

thanks,

steve.
 
Hi Stephen,
Without seeing your code, it's hard to say exactly what is going wrong,
but I'm guessing it's a problem with the "out" lpClassName parameter to
GetClassName.

Try this instead, it worked for me:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,
int nMaxCount);

static void Main(string[] args)
{
IntPtr hwnd = FindWindow("notepad", null);
StringBuilder lpClassName = new StringBuilder();
GetClassName(hwnd, out lpClassName, 100);
Console.WriteLine(lpClassName.ToString());
}

Hope that helps,
John
 
Thanks, that seemed to solve the problem!


John said:
Hi Stephen,
Without seeing your code, it's hard to say exactly what is going wrong,
but I'm guessing it's a problem with the "out" lpClassName parameter to
GetClassName.

Try this instead, it worked for me:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,
int nMaxCount);

static void Main(string[] args)
{
IntPtr hwnd = FindWindow("notepad", null);
StringBuilder lpClassName = new StringBuilder();
GetClassName(hwnd, out lpClassName, 100);
Console.WriteLine(lpClassName.ToString());
}

Hope that helps,
John

Hi,

I am using the

IntPtr FindWindow(string lpClassName, string lpWindowName);

to get the handle for a window (notepad in this case), I am then using

int GetClassName(IntPtr hWnd, out StringBuilder lpClassName, int
nMaxCount);

to get the name of this window. Now it all works fine but as prove of
concept i want to print the name of the class to console. The
GetClassName returns 7 (Specifies the length, in TCHAR, of the buffer
pointed to by the lpClassName parameter) which would make sense as
notepad class name is Notepad. I'm having problems printing this to
console. It returns 3 characters to the console. I declared a local
StringBuilder class_name and passed that as an out parameter in my
method.

thanks,

steve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top