_T和L在C ++中是什么意思,如何传递它们? [英] What do _T and L mean in C++ and how can I pass them?
问题描述
我正在尝试用C ++构建一个键盘记录程序.我的键盘记录程序的一部分是捕获屏幕.
经过大量搜索,我决定尝试通过构建一个实例来立即了解它的工作原理.
I am trying to build a keylogger in C++.
Part of my keylogger is to capture the screen.
After a lot of searching, I decided to try to understand how it works instantly by trying to build one.
这是我的屏幕捕获代码:
This is my screen capture code:
HDC hdc = GetDC(NULL); // get the desktop device context
HDC hDest = CreateCompatibleDC(hdc); // create a device context to use yourself
// get the height and width of the screen
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
// create a bitmap
HBITMAP hbDesktop = CreateCompatibleBitmap(hdc, width, height);
// use the previously created device context with the bitmap
SelectObject(hDest, hbDesktop);
// copy from the desktop device context to the bitmap device context
// call this once per 'frame'
BitBlt(hDest, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
/*CImage image; // from this code i tried to understand how to save the bitmap
image.Attach(hbDesktop);
image.Save(pathname, Gdiplus::ImageFormatBMP);*/
CImage image;//this code is what I came up with eventually
image.Attach(hbDesktop);
CHAR buffer[100] = _T("this is a literal string"); // THIS IS WHERE MY PROBLEM STARTS
sprintf(buffer,_T("this is a literal string"), 1);
image.Save(_T(buffer), Gdiplus::ImageFormatBMP);
我试图使程序每次使用不同的名称保存位图文件.问题是,除非我使用 _T
符号,否则最后一行将不起作用,并且当我放入 _T
时,它不会占用缓冲区并说"identifier"Lbuffer"未定义".这是什么意思?我需要在哪里放置这个L标志,为什么?另外,有没有一种更好的方法可以在不使用此 _T
符号的情况下将位图保存到文件中?
I tried to make the program save the bitmap file each time with a different name.
The problem is that the last line doesn't work unless I use _T
sign, and when I do put the _T
, it won't take the buffer and says "identifier "Lbuffer" is undefined". What does this mean? Where do I need to put this L sign and why?
Also, is there a better way to save bitmap to a file without using this _T
sign?
我尝试查找此内容,并在 https://msdn上进行了查找.microsoft.com/en-us/library/dybsewaf.aspx 他们说这与Unicode有关.为什么我需要Unicode才能使用此功能?
I've tried to look this up, and on https://msdn.microsoft.com/en-us/library/dybsewaf.aspx they say this is about Unicode. Why do I need Unicode for this function?
推荐答案
_T()
及其Win32等价的 TEXT()
是预处理器宏,如果分别定义了 _UNICODE
或 UNICODE
,则在输入值前加上 L
.
_T()
, and its Win32 equivilent TEXT()
, are preprocessor macros that prepend the input value with L
if _UNICODE
or UNICODE
are defined, respectively.
这些宏只能与字符/字符串文字一起使用.您不能将它们与变量一起使用.因此 _T(buffer)
是无效的代码.这就是为什么您收到未定义的 identifer"Lbuffer"
错误(显然在您的项目中定义了 _UNICODE
的原因)的原因.
These macros are meant to be used with character/string literals only. You cannot use them with variables. So _T(buffer)
is not valid code. That is why you are getting an identifer "Lbuffer" not defined
error (_UNICODE
is obviously defined in your project).
如果定义了 _UNICODE
/ UNICODE
,则 _T("literal")
和 TEXT("literal")
被编译器评估为 L"literal"
-类型为 wchar_t []
的宽字符串文字.
If _UNICODE
/UNICODE
are defined, _T("literal")
and TEXT("literal")
are evaluated by the compiler as L"literal"
- a wide string literal of type wchar_t[]
.
如果未定义 _UNICODE
/ UNICODE
,则 _T("literal")
和 TEXT("literal")编译器将code>评估为
"literal"
-一种 char <]类型的窄字符串文字.
If _UNICODE
/UNICODE
are not defined, _T("literal")
and TEXT("literal")
are evaluated by the compiler as "literal"
- a narrow string literal of type char[]
.
CHAR
不是宏,它是 char
的 typedef ,并且是无条件的(存在等效的 WCHAR
用于 wchar_t
的typedef).
CHAR
is not a macro, it is a typedef for char
, and is unconditional (there is an equivalent WCHAR
typedef for wchar_t
).
因此,这一行:
CHAR buffer[100] = _T("this is a literal string");
被评估为:
// when _UNICODE is defined
char buffer[100] = L"this is a literal string"; // DOES NOT COMPILE!
// You cannot assign/fill a char[] array with wchar_t data!
// when _UNICODE is not defined
char buffer[100] = "this is a literal string"; // OK
如果要使用 _T()
/ TEXT()
,则必须使用 _TCHAR
/ TCHAR
而不是 CHAR
,因此它们匹配:
If you want to use _T()
/TEXT()
, you must use _TCHAR
/TCHAR
instead of CHAR
so they match:
_TCHAR buffer[100] = _T("this is a literal string");
_TCHAR
/ TCHAR
是 _UNICODE
/ UNICODE
敏感的预处理器 #define
s.定义 _UNICODE
/ UNICODE
时,它们解析为 wchar_t
,否则它们解析为 char
:
_TCHAR
/TCHAR
are _UNICODE
/UNICODE
-sensitive preprocessor #define
s. When _UNICODE
/UNICODE
are defined, they resolve to wchar_t
, otherwise they resolve to char
instead:
// when _UNICODE is defined
wchar_t buffer[100] = L"this is a literal string"; // OK
// when _UNICODE is not defined
char buffer[100] = "this is a literal string"; // OK
sprint()
仅适用与 char
数据一起使用. wchar_t
版本是 swprintf()
,而 _TCHAR
/ TCHAR
版本是 _stprintf()
:
sprint()
only works with char
data. The wchar_t
version is swprintf()
, and the _TCHAR
/TCHAR
version is _stprintf()
:
char buffer[100];
sprintf(buffer, "this is a literal string", 1); // OK
wchar_t buffer[100];
swprintf(buffer, L"this is a literal string", 1); // OK
_TCHAR buffer[100];
_stprintf(buffer, _T("this is a literal string"), 1); // OK
CImage :: Save()
接受 LPCTSTR
作为输入. LPCTSTR
是 const TCHAR *
的 typedef ,因此在 UNICODE 时解析为
const wchar_t *
.code>已定义,否则解析为 const char *
.由于它对 UNICODE
敏感,但是您的 buffer
始终是 char []
,因此您将无法传递 buffer
到 Save()
(如果已定义 UNICODE
).您需要改用 TCHAR
:
CImage::Save()
accepts a LPCTSTR
as input. LPCTSTR
is a typedef for const TCHAR*
, thus resolves to const wchar_t*
when UNICODE
is defined, otherwise resolves to const char*
. As it is UNICODE
-sensitive, but your buffer
is always a char[]
, you will not be able to pass buffer
to Save()
if UNICODE
is defined. You need to use TCHAR
instead:
// In this case, you should use the Win32 macros instead of the C macros.
// It is not good practice to mix C's _T()/_TCHAR macros with Win32 APIs,
// and Win32's TEXT()/TCHAR macros with C APIs, though technically it will
// work fine. Best to keep the two APIs separate...
TCHAR buffer[100];
_stprintf(buffer, TEXT("filename_%d.bmp"), 1);
image.Save(buffer, Gdiplus::ImageFormatBMP);
这篇关于_T和L在C ++中是什么意思,如何传递它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!