473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

String problem VS 2005

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.

Sep 11 '07 #1
12 2971
SQACSharp wrote:
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
Sep 11 '07 #2
On 11 sep, 16:51, David Wilkinson <no-re...@effisols.comwrote:
SQACSharp wrote:
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

Sep 11 '07 #3
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);

}

--
David Wilkinson
Visual C++ MVP
Sep 11 '07 #4
On Sep 11, 6:28 pm, David Wilkinson <no-re...@effisols.comwrote:
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);

}

--
David Wilkinson
Visual C++ MVP
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?

Sep 11 '07 #5
On Sep 11, 6:28 pm, David Wilkinson <no-re...@effisols.comwrote:
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);

}

--
David Wilkinson
Visual C++ MVP
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!

Sep 12 '07 #6

"SQACSharp" <ls*********@hotmail.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.com...
On Sep 11, 6:28 pm, David Wilkinson <no-re...@effisols.comwrote:
>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);

}

--
David Wilkinson
Visual C++ MVP

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"))
Sep 12 '07 #7
On Sep 11, 8:52 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"SQACSharp" <lsdiscip...@hotmail.comwrote in message

news:11*********************@22g2000hsm.googlegrou ps.com...


On Sep 11, 6:28 pm, David Wilkinson <no-re...@effisols.comwrote:
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);
}
--
David Wilkinson
Visual C++ MVP
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
}

Sep 12 '07 #8
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).
Sep 12 '07 #9
SQACSharp wrote:
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
Sep 12 '07 #10
On Sep 11, 9:37 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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!

Sep 12 '07 #11
On Sep 11, 9:42 pm, David Wilkinson <no-re...@effisols.comwrote:
SQACSharp wrote:
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?

Sep 12 '07 #12
SQACPP wrote:
What i need to include to be able to use MFC CString class?
You need to look up CString in the Help.

--
David Wilkinson
Visual C++ MVP
Sep 12 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
1
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was...
3
by: andrew | last post by:
Hi: I am already using TreeMap to massage records in my export file such that each record has a unique key combination ( LastName + FirstName + Member Key) . Thus I am sorting the records by...
4
by: lkrubner | last post by:
I'd like to write a PHP script to be used from the command line on a Unix machine. I'd like for the script to put together a string, turn it into a web page, print it, then return control the...
1
by: amitbadgi | last post by:
Welcome back amitbadgi | Logout | Faq Knowledge Discovery Keys COMPUTER PROGRAMMING, DATA MINING, STATISTICS, ARTIFICIAL INTELLIGENCE * Settings * Photos * Lists * MVPs * Forums * Blogs
16
by: BBM | last post by:
This is so bizarre I hesitate to post it, but I need to get this working. My code looks like this... Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _ " WHERE Scenario =...
6
by: Maileen | last post by:
Hi, I have the following code : Function GetRequestType(ByVal EvDt As String, ByVal StPeriod As String, ByVal EdPeriod As String, ByVal TaskType As String) As Integer Dim strtest As String Dim...
0
by: Hongbo | last post by:
Hi, I have a test server with Windows 2003 Standard Server. SQL Server 2000 Standard Edition was installed earlier as default instance. My ASP.Net web site works fine with the connection...
15
by: Lighter | last post by:
I find a BIG bug of VS 2005 about string class! #include <iostream> #include <string> using namespace std; string GetStr() { return string("Hello");
8
by: thunderbolt | last post by:
Hi, How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005 (Dec)" in C#? The number within the brackets represent month.. am new to c# and breaking head for couple of hours over...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.