Sprintf variable LPCWSTR

I am trying to debug a string LPCWSTR, but I am having a problem while clicking sprintfin the buffer, because it only extracts the first character from the string.

Here is the code:

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);
}

For example, I get CreateFileW: Cor CreateFileW: \.

How to paste it into the buffer?

Thank.

+5
source share
3 answers

Use swprintf_s , which is a version of sprintf_s designed for wide character strings.

You will also need an array wchar_tinstead charand useOutputDebugStringW()

, , swprintf_w , . , , , - . .

+6

sprintf(), . % ls:

 sprintf_s(buffer, 1024, "CreateFileW: %ls", lpFileName);

, . Unicode. char [] , . . , wchar_t + wsprintf(). #define UNICODE, OutputDebugStringW(), . ++ , .

+6

unicode ( , , ), - , :

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);
}
+5

All Articles