String problem VS 2005

S

SQACSharp

How can I "convert" from a char to a string. I'm trying to display in
the console the Classname and the text in the notepad window.

Start notepad and type some text before running the following code.

#include <windows.h>
#include <iostream>
#include <winuser.h>


using namespace System;

int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(L"Notepad", NULL);
hEdit = FindWindowEx(hNotepad, NULL, L"edit", NULL);
SetForegroundWindow(hNotepad);

static char ClassName[51];
GetClassName(hNotepad,(LPTSTR) ClassName, 50 );
std::cout << "ClassName = " << ClassName; // *** NOT WORKING ***

int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::cout << "Text captured from notepad = " << textData; // *** NOT
WORKING ***

free((void *) textData);

}

Thanks.
 
D

David Wilkinson

SQACSharp said:
How can I "convert" from a char to a string. I'm trying to display in
the console the Classname and the text in the notepad window.

Start notepad and type some text before running the following code.

#include <windows.h>
#include <iostream>
#include <winuser.h>


using namespace System;

int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(L"Notepad", NULL);
hEdit = FindWindowEx(hNotepad, NULL, L"edit", NULL);
SetForegroundWindow(hNotepad);

static char ClassName[51];
GetClassName(hNotepad,(LPTSTR) ClassName, 50 );
std::cout << "ClassName = " << ClassName; // *** NOT WORKING ***

int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::cout << "Text captured from notepad = " << textData; // *** NOT
WORKING ***

free((void *) textData);

}

SQACharp:

In VS 2005 projects are Unicode by default. This means you must use L""
and wchar_t everywhere. Alternatively, use _T("") and TCHAR everywhere,
and your code will compile as either ANSI or UNICODE.

Right now, your code is a mix of narrow, wide and agnostic strings.

You do not say what "NOT WORKING" means, but one thing you should do is
check that hNotepas and hEdit are valid windows.
 
S

SQACSharp

SQACSharp said:
How can I "convert" from a char to a string. I'm trying to display in
the console the Classname and the text in the notepad window.
Start notepad and type some text before running the following code.
#include <windows.h>
#include <iostream>
#include <winuser.h>
using namespace System;
int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(L"Notepad", NULL);
hEdit = FindWindowEx(hNotepad, NULL, L"edit", NULL);
SetForegroundWindow(hNotepad);
static char ClassName[51];
GetClassName(hNotepad,(LPTSTR) ClassName, 50 );
std::cout << "ClassName = " << ClassName; // *** NOT WORKING ***
int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::cout << "Text captured from notepad = " << textData; // *** NOT
WORKING ***
free((void *) textData);

SQACharp:

In VS 2005 projects are Unicode by default. This means you must use L""
and wchar_t everywhere. Alternatively, use _T("") and TCHAR everywhere,
and your code will compile as either ANSI or UNICODE.

Right now, your code is a mix of narrow, wide and agnostic strings.

You do not say what "NOT WORKING" means, but one thing you should do is
check that hNotepas and hEdit are valid windows.

--
David Wilkinson
Visual C++ MVP- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

By NOT WORKING I mean :

The first output to the console (the classname) is "N" and not
"Notepad" as expected

The second output to the console (the text content of notepad) is
equal to a numeric value instead of the real content.

I dont understand how to work with all the C++ string format . What
kind of string i'm suppose to use? Is anybody can correct my code
example to use uniform string instead of my newbee mix of narrow, wide
and agnostic strings.

Thanks
 
D

David Wilkinson

SQACSharp said:
By NOT WORKING I mean :

The first output to the console (the classname) is "N" and not
"Notepad" as expected

The second output to the console (the text content of notepad) is
equal to a numeric value instead of the real content.

I dont understand how to work with all the C++ string format . What
kind of string i'm suppose to use? Is anybody can correct my code
example to use uniform string instead of my newbee mix of narrow, wide
and agnostic strings.

// untested agnostic version

#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif

int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(_T("Notepad"), NULL);
hEdit = FindWindowEx(hNotepad, NULL, _T("edit"), NULL);
SetForegroundWindow(hNotepad);

TCHAR ClassName[51];
GetClassName(hNotepad, ClassName, 50 );
std::tcout << _T("ClassName" = ") << ClassName;

int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::tcout << _T("Text captured from notepad = ") << textData;

free((void *) textData);

}
 
S

SQACSharp

SQACSharp said:
By NOT WORKING I mean :
The first output to the console (the classname) is "N" and not
"Notepad" as expected
The second output to the console (the text content of notepad) is
equal to a numeric value instead of the real content.
I dont understand how to work with all the C++ string format . What
kind of string i'm suppose to use? Is anybody can correct my code
example to use uniform string instead of my newbee mix of narrow, wide
and agnostic strings.

// untested agnostic version

#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif

int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(_T("Notepad"), NULL);
hEdit = FindWindowEx(hNotepad, NULL, _T("edit"), NULL);
SetForegroundWindow(hNotepad);

TCHAR ClassName[51];
GetClassName(hNotepad, ClassName, 50 );
std::tcout << _T("ClassName" = ") << ClassName;

int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::tcout << _T("Text captured from notepad = ") << textData;

free((void *) textData);

}

Thanks...it work now!

Just one more thing.... if I want to compare my string...ex:

If i put the following lines after the first output (the classname)

if (strcmp(ClassName,"Notepad")==1)
{
std::tcout << "The classname is notepad";
}

I get the following error : cannot convert parameter 1 from 'TCHAR
[51]' to 'const char *'

There is really something I dont understand about String and
conversion.... any help?
 
S

SQACSharp

SQACSharp said:
By NOT WORKING I mean :
The first output to the console (the classname) is "N" and not
"Notepad" as expected
The second output to the console (the text content of notepad) is
equal to a numeric value instead of the real content.
I dont understand how to work with all the C++ string format . What
kind of string i'm suppose to use? Is anybody can correct my code
example to use uniform string instead of my newbee mix of narrow, wide
and agnostic strings.

// untested agnostic version

#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif

int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(_T("Notepad"), NULL);
hEdit = FindWindowEx(hNotepad, NULL, _T("edit"), NULL);
SetForegroundWindow(hNotepad);

TCHAR ClassName[51];
GetClassName(hNotepad, ClassName, 50 );
std::tcout << _T("ClassName" = ") << ClassName;

int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::tcout << _T("Text captured from notepad = ") << textData;

free((void *) textData);

}

Thanks!!!

Now If a want to do a comparison on the ClassName (with the same
example with classname and the content of the notepad window??? ex:

if (strcmp(ClassName,"Notepad")==1)
{
std::tcout << "notepad found";
}


It return error : 'strcmp' : cannot convert parameter 1 from 'TCHAR
[51]' to 'const char *'

It's hard to understand understand what kind of string format to use
and how to convert it...any help?

Thanks again!
 
B

Ben Voigt [C++ MVP]

SQACSharp said:
SQACSharp said:
By NOT WORKING I mean :
The first output to the console (the classname) is "N" and not
"Notepad" as expected
The second output to the console (the text content of notepad) is
equal to a numeric value instead of the real content.
I dont understand how to work with all the C++ string format . What
kind of string i'm suppose to use? Is anybody can correct my code
example to use uniform string instead of my newbee mix of narrow, wide
and agnostic strings.

// untested agnostic version

#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif

int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(_T("Notepad"), NULL);
hEdit = FindWindowEx(hNotepad, NULL, _T("edit"), NULL);
SetForegroundWindow(hNotepad);

TCHAR ClassName[51];
GetClassName(hNotepad, ClassName, 50 );
std::tcout << _T("ClassName" = ") << ClassName;

int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::tcout << _T("Text captured from notepad = ") << textData;

free((void *) textData);

}

Thanks...it work now!

Just one more thing.... if I want to compare my string...ex:

If i put the following lines after the first output (the classname)

if (strcmp(ClassName,"Notepad")==1)
{
std::tcout << "The classname is notepad";
}

I get the following error : cannot convert parameter 1 from 'TCHAR
[51]' to 'const char *'

You just won't be able to use ASCII strings anymore. Instead try:

_tcscmp(ClassName, _T("Notepad"))
 
S

SQACSharp

SQACSharp wrote:
By NOT WORKING I mean :
The first output to the console (the classname) is "N" and not
"Notepad" as expected
The second output to the console (the text content of notepad) is
equal to a numeric value instead of the real content.
I dont understand how to work with all the C++ string format . What
kind of string i'm suppose to use? Is anybody can correct my code
example to use uniform string instead of my newbee mix of narrow, wide
and agnostic strings.
// untested agnostic version
#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif
int main()
{
HWND hNotepad, hEdit;
hNotepad = FindWindow(_T("Notepad"), NULL);
hEdit = FindWindowEx(hNotepad, NULL, _T("edit"), NULL);
SetForegroundWindow(hNotepad);
TCHAR ClassName[51];
GetClassName(hNotepad, ClassName, 50 );
std::tcout << _T("ClassName" = ") << ClassName;
int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
TCHAR * textData = (TCHAR *) malloc((iLength + 1) * sizeof(TCHAR));
SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
std::tcout << _T("Text captured from notepad = ") << textData;
free((void *) textData);
}
Thanks...it work now!
Just one more thing.... if I want to compare my string...ex:
If i put the following lines after the first output (the classname)
if (strcmp(ClassName,"Notepad")==1)
{
std::tcout << "The classname is notepad";
}
I get the following error : cannot convert parameter 1 from 'TCHAR
[51]' to 'const char *'

You just won't be able to use ASCII strings anymore. Instead try:

_tcscmp(ClassName, _T("Notepad"))- Hide quoted text -

- Show quoted text -


Still not working..._TCSCPM return 0

HWND hNotepad, hEdit;
hNotepad = FindWindow(L"Notepad", NULL);
hEdit = FindWindowEx(hNotepad, NULL, L"edit", NULL);
SetForegroundWindow(hNotepad);

static TCHAR ClassName[51];
GetClassName(hNotepad,(LPTSTR) ClassName, 50 );
std::tcout << _T("Classname---> ") << ClassName;
std::tcout <<_tcscmp(ClassName,_T("Notepad"));

if (_tcscmp(ClassName,_T("Notepad"))!=0)
{
std::tcout << "Notepad classname found"; //It never enter here
}
 
B

Ben Voigt [C++ MVP]

Still not working..._TCSCPM return 0
HWND hNotepad, hEdit;
hNotepad = FindWindow(L"Notepad", NULL);
hEdit = FindWindowEx(hNotepad, NULL, L"edit", NULL);
SetForegroundWindow(hNotepad);

static TCHAR ClassName[51];
GetClassName(hNotepad,(LPTSTR) ClassName, 50 );
std::tcout << _T("Classname---> ") << ClassName;
std::tcout <<_tcscmp(ClassName,_T("Notepad"));

if (_tcscmp(ClassName,_T("Notepad"))!=0)
{
std::tcout << "Notepad classname found"; //It never enter here
}

Well, considering that strcmp, wcscmp, and _tcscmp all return zero when the
strings are equal, I'm not surprised. They are comparison functions, useful
to pass to a sort routine, which means they have to differentiate between
(less than/equal/greater than).
 
D

David Wilkinson

SQACSharp said:
Thanks!!!

Now If a want to do a comparison on the ClassName (with the same
example with classname and the content of the notepad window??? ex:

if (strcmp(ClassName,"Notepad")==1)
{
std::tcout << "notepad found";
}


It return error : 'strcmp' : cannot convert parameter 1 from 'TCHAR
[51]' to 'const char *'

It's hard to understand understand what kind of string format to use
and how to convert it...any help?

If you look up strcmp in the Help, you will find the answer to your
question.

BTW, the MFC CString class has methods that deal with the narrow/wide
character issue in a transparent way.

BTW, again, you are really in the wrong group. Questions about
traditional C++ are better asked in

microsoft.public.vc.language
microsoft.public.vc.mfc
 
S

SQACPP

Still not working..._TCSCPM return 0
HWND hNotepad, hEdit;
hNotepad = FindWindow(L"Notepad", NULL);
hEdit = FindWindowEx(hNotepad, NULL, L"edit", NULL);
SetForegroundWindow(hNotepad);
static TCHAR ClassName[51];
GetClassName(hNotepad,(LPTSTR) ClassName, 50 );
std::tcout << _T("Classname---> ") << ClassName;
std::tcout <<_tcscmp(ClassName,_T("Notepad"));
if (_tcscmp(ClassName,_T("Notepad"))!=0)
{
std::tcout << "Notepad classname found"; //It never enter here
}

Well, considering that strcmp, wcscmp, and _tcscmp all return zero when the
strings are equal, I'm not surprised. They are comparison functions, useful
to pass to a sort routine, which means they have to differentiate between
(less than/equal/greater than).- Hide quoted text -

- Show quoted text -

Thanks again!...everything work now!
 
S

SQACPP

SQACSharp said:
Thanks!!!
Now If a want to do a comparison on the ClassName (with the same
example with classname and the content of the notepad window??? ex:
if (strcmp(ClassName,"Notepad")==1)
{
std::tcout << "notepad found";
}
It return error : 'strcmp' : cannot convert parameter 1 from 'TCHAR
[51]' to 'const char *'
It's hard to understand understand what kind of string format to use
and how to convert it...any help?

If you look up strcmp in the Help, you will find the answer to your
question.

BTW, the MFC CString class has methods that deal with the narrow/wide
character issue in a transparent way.

BTW, again, you are really in the wrong group. Questions about
traditional C++ are better asked in

microsoft.public.vc.language
microsoft.public.vc.mfc

--
David Wilkinson
Visual C++ MVP- Hide quoted text -

- Show quoted text -

What i need to include to be able to use MFC CString class?
 

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

Top