Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2007

Tip: Looking for answers? Try searching our database.

String problem VS 2005

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SQACSharp - 11 Sep 2007 21:06 GMT
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.
David Wilkinson - 11 Sep 2007 21:51 GMT
> 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.
[quoted text clipped - 27 lines]
>
> }

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.

Signature

David Wilkinson
Visual C++ MVP

SQACSharp - 11 Sep 2007 22:22 GMT
> > 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.
[quoted text clipped - 44 lines]
>
> - 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
David Wilkinson - 11 Sep 2007 23:28 GMT
> By NOT WORKING I mean :
>
[quoted text clipped - 8 lines]
> 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);

}

Signature

David Wilkinson
Visual C++ MVP

SQACSharp - 12 Sep 2007 00:25 GMT
> > By NOT WORKING I mean :
>
[quoted text clipped - 40 lines]
> 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?
Ben Voigt [C++ MVP] - 12 Sep 2007 01:52 GMT
>> > By NOT WORKING I mean :
>>
[quoted text clipped - 54 lines]
> 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"))
SQACSharp - 12 Sep 2007 02:03 GMT
> >> > By NOT WORKING I mean :
>
[quoted text clipped - 60 lines]
>
> - 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
}
Ben Voigt [C++ MVP] - 12 Sep 2007 02:37 GMT
> Still not working..._TCSCPM return 0
>
[quoted text clipped - 12 lines]
> 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).
SQACPP - 12 Sep 2007 02:49 GMT
> > Still not working..._TCSCPM return 0
>
[quoted text clipped - 19 lines]
>
> - Show quoted text -

Thanks again!...everything work now!
SQACSharp - 12 Sep 2007 01:45 GMT
> > By NOT WORKING I mean :
>
[quoted text clipped - 40 lines]
> 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!
David Wilkinson - 12 Sep 2007 02:42 GMT
> Thanks!!!
>
[quoted text clipped - 11 lines]
> 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

Signature

David Wilkinson
Visual C++ MVP

SQACPP - 12 Sep 2007 02:58 GMT
> > Thanks!!!
>
[quoted text clipped - 29 lines]
>
> - Show quoted text -

What i need to include to be able to use MFC CString class?
David Wilkinson - 12 Sep 2007 10:19 GMT
> What i need to include to be able to use MFC CString class?

You need to look up CString in the Help.

Signature

David Wilkinson
Visual C++ MVP


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.