本文介绍了字符串问题VS 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能转换从char到字符串。我正试图在控制台中显示类别和记事本窗口中的文字。


在运行以下内容之前,先启动记事本并输入一些文字代码。


#include< windows.h>

#include< iostream>

#include< winuser。 h>

使用命名空间系统;


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 =" <<班级名称; // ***不工作***

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<< 从记事本中捕获的文字= <<的TextData; // *** NOT

工作***


免费((void *)textData);


}


谢谢。

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.

推荐答案



SQACharp:


在VS 2005中,项目默认为Unicode。这意味着你必须在任何地方使用L"

和wchar_t。或者,在任何地方使用_T()和TCHAR,

,您的代码将编译为ANSI或UNICODE。


现在,您的代码是一个狭窄,广泛和不可知的字符串的混合。


你没有说什么不工作意思是,但你应该做的一件事是

检查hNotepas和hEdit是否是有效窗口。


-

David Wilkinson

Visual C ++ MVP

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




SQACharp:


在VS 2005中,项目默认为Unicode。这意味着你必须在任何地方使用L"

和wchar_t。或者,在任何地方使用_T()和TCHAR,

,您的代码将编译为ANSI或UNICODE。


现在,您的代码是一个狭窄,广泛和不可知的字符串的混合。


你没有说什么不工作意思是,但你应该做的一件事是

检查hNotepas和hEdit是否是有效窗口。


-

David Wilkinson

Visual C ++ MVP- Masquer le texte desmessagesprécédents -


- Afficher le texte desmessagesprécédents -


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 -



通过NOT WORKING我的意思是:


控制台的第一个输出(类名)是N。而不是

记事本正如预期的那样


控制台的第二个输出(记事本的文本内容)

等于数值而不是真实内容。


我不明白如何使用所有C ++字符串格式。什么

我想要使用哪种字符串?是否有人可以更正我的代码

示例使用统一字符串而不是我的newbee混合的窄,宽
和不可知的字符串。


谢谢

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




//未经测试的不可知版本


#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(从notepad ="捕获的文本)<< textData;


免费((void *)textData);


}


-

David Wilkinson

Visual C ++ MVP

// 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


这篇关于字符串问题VS 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:29