打印到日志的PUNICODE_STRING
PUNICODE_STRING printed to a log
我有一个密码过滤器dll,至少现在,当用户帐户的密码被成功更改时,它只是将用户名和密码写入日志。我遇到了c字符串的一些老问题。
我感兴趣的两个变量的类型是PUNICODE_STRING(它只是一个*UNICODE_STRING)。我很自然地在网上搜索这种数据类型,它把我带到这里:http://msdn.microsoft.com/en-us/library/windows/desktop/aa380518(v=vs.85).aspx
给出这个信息,我知道我可以访问这个动物的长度,并且它有一个指向宽字符串的指针。太棒了。这是足够的知识来提取我需要的内容(所以我认为)。
这是我的dll代码(dllmain.cpp)
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <iostream>
#define LOGFILE "c:\PasswordFilter.txt"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
void WriteToLog (const wchar_t* UserName, const wchar_t* NewPassword)
{
#ifdef LOGFILE
FILE* log = fopen(LOGFILE, "a");
if(NULL == log)
{
return;
}
fwprintf(log,L"%s password was successfully reset to %srn",UserName,NewPassword);
fclose(log);
#endif
return;
}
LPWSTR __stdcall ConvertWideChar(PUNICODE_STRING input)
{
wchar_t* wszString = new wchar_t[input->Length + 1];
memset(wszString, 0, sizeof(wchar_t) * (input->Length + 1));
wcsncpy(wszString,input->Buffer,input->Length);
#ifdef LOGFILE
FILE* log = fopen(LOGFILE, "a");
if(NULL != log)
{
fwprintf(log,L"value of wszString: %srn",wszString);
fclose(log);
}
#endif
return wszString;
}
BOOLEAN __stdcall InitializeChangeNotify(void)
{
return TRUE;
}
NTSTATUS __stdcall PasswordChangeNotify(
PUNICODE_STRING UserName,
ULONG RelativeId,
PUNICODE_STRING NewPassword
)
{
LPWSTR ConvertedUserName = ConvertWideChar(UserName);
LPWSTR ConvertedNewPassword = ConvertWideChar(NewPassword);
WriteToLog(ConvertedUserName,ConvertedNewPassword);
return 0;
}
BOOLEAN __stdcall PasswordFilter(
PUNICODE_STRING AccountName,
PUNICODE_STRING FullName,
PUNICODE_STRING Password,
BOOLEAN SetOperation
)
{
return TRUE;
}
这里是头文件的内容(stdafx.h)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <stdio.h>
#include <windows.h>
#include <winnt.h>
#include <NTSecAPI.h>
// TODO: reference additional headers your program requires here
当然,我关心的是保持准确性。我的痛苦就是从这里开始的。我们有一个wchar_t,它告诉我这可能是UTF16格式的unicode(就像所有其他基于windows的字符串一样)。上面的代码将PUNICODE_STRING的缓冲区复制到以零结尾的wchar_t(因为缓冲区本身不能保证以零结尾),然后尝试将该字符串输出到日志文件。
我第一次尝试使用WideCharToMultiByte作为一种将缓冲区复制到字符串的方式,但这似乎从未奏效。每次结果都是null。这也产生了将unicode值转换为更简单的格式的问题,这可能会导致数据丢失。因此,我决定使用我现在所拥有的,但是记录的值后面有?'s和其他垃圾,这可能是unicode问题。
如果可能的话,我真的想保留这个数据结构的unicode编码类型。我如何做到这一点,并将我的值打印到日志中?您没有正确使用UNICODE_STRING::Length
字段。它以字节表示,而不是以字符表示。因此,您没有正确分配+填充以空结束的缓冲区。您还泄漏了您分配的缓冲区。
你根本不需要分配缓冲区。您可以将原始的Buffer
和Length
值直接传递给fwprintf
,不需要null终止符:
NTSTATUS __stdcall PasswordChangeNotify(
PUNICODE_STRING UserName,
ULONG RelativeId,
PUNICODE_STRING NewPassword
)
{
#ifdef LOGFILE
FILE* log = fopen(LOGFILE, "a");
if (NULL != log)
{
fwprintf(log, L"%.*s password was successfully reset to %.*srn",
UserName->Length / sizeof(WCHAR), UserName->Buffer,
NewPassword->Length / sizeof(WCHAR), NewPassword->Buffer);
fclose(log);
}
#endif
return 0;
}
如果您确实需要添加空终止符,请使用std::wstring
而不是手动分配+复制:
#include <string>
NTSTATUS __stdcall PasswordChangeNotify(
PUNICODE_STRING UserName,
ULONG RelativeId,
PUNICODE_STRING NewPassword
)
{
#ifdef LOGFILE
FILE* log = fopen(LOGFILE, "a");
if (NULL != log)
{
fwprintf(log, L"%.s password was successfully reset to %.srn",
std::wstring(UserName->Buffer, UserName->Length / sizeof(WCHAR)).c_str(),
std::wstring(NewPassword->Buffer, NewPassword->Length / sizeof(WCHAR)).c_str());
fclose(log);
}
#endif
return 0;
}
实际上可以使用printf修饰符%wZ打印UNICODE_STRING结构体。不需要任何花哨的Buffer转换,也不需要担心null终止符。
UNICODE_STRING someStr;
RtlInitUnicodeString(&someStr, L"Hello");
printf("String: %wZn", &someStr);
菲尼.
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串