sprintf一个LPCWSTR变量

Jul*_*lio 5 c++ windows

我正在尝试调试打印LPCWSTR字符串,但是sprintf在缓冲区中推送时遇到问题,因为它只检索字符串中的第一个字符.

这是代码:

HANDLE WINAPI hookedCreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) {
    char buffer[1024];
    sprintf_s(buffer, 1024, "CreateFileW: %s", lpFileName);
    OutputDebugString(buffer); 
    return trueCreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwFlagsAndAttributes, dwCreationDisposition, hTemplateFile);
}
Run Code Online (Sandbox Code Playgroud)

例如,我得到CreateFileW: CCreateFileW: \.

如何正确地将其推入缓冲区?

谢谢.

sho*_*osh 6

使用swprintf_s这是sprintf_s的版本,它是为宽字符串设计的.

你还需要一个wchar_t代替char和使用的数组OutputDebugStringW()

另外,请注意您swprintf_w可能不是想要调用的内容.如果它遇到的字符串长于你给它的大小,它会执行某种断言.我建议你具体测试这种情况.


Han*_*ant 6

你需要告诉sprintf()你传递一个宽字符串.使用%ls说明符:

 sprintf_s(buffer, 1024, "CreateFileW: %ls", lpFileName);
Run Code Online (Sandbox Code Playgroud)

请注意这是多么无效.您的代码在Unicode操作系统上运行.它必须将char []字符串转换回宽字符串,然后才能将其发送到调试器.这只是浪费了CPU周期,导致数据丢失的风险很大.当你在罗马时,像罗马人一样使用wchar_t + wsprintf().并且#define UNICODE这样你就可以自动调用快速的OutputDebugStringW(),它不需要转换字符串.使用C++的关键是编写快速代码,故意制作慢是没有意义的.


Ofe*_*lon 5

除非你有一个具体的理由在这个单一函数中定位unicode(而不是在你的整个项目中),否则尽可能使用charset-agnostic宏是明智的:

HANDLE WINAPI hookedCreateFile(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) {
 TCHAR buffer[1024];
 _stprintf_s(buffer, 1024, _T("CreateFileW: %s"), lpFileName);
 OutputDebugString(buffer); 
 return trueCreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwFlagsAndAttributes, dwCreationDisposition, hTemplateFile);
}
Run Code Online (Sandbox Code Playgroud)