giftDev 2025. 6. 26. 10:46
반응형

#include <ntstrsafe.h> // RtlStringCbPrintfW
#include <ntddk.h>     // DbgPrint, UNICODE_STRING, ANSI_STRING 등

void WriteBlockDebugLog(
    PCWSTR prefix,             // 유니코드 로그 구분자(한글 OK)
    PCWSTR uniMD5FilePath,     // 로그 내용1
    PCWSTR wszImageFileName,   // 로그 내용2
    PCWSTR wszMD5              // 로그 내용3
)
{
    // NULL-safe: NULL이면 (null) 대체
    if (!prefix)           prefix = L"(null)";
    if (!uniMD5FilePath)   uniMD5FilePath = L"(null)";
    if (!wszImageFileName) wszImageFileName = L"(null)";
    if (!wszMD5)           wszMD5 = L"(null)";

    // 로그 문자열 생성
    WCHAR wszLogMsg[1024] = {0};
    RtlStringCbPrintfW(
        wszLogMsg, sizeof(wszLogMsg),
        L"%ws uniMD5FilePath: %ws\nwszImageFileName: %ws\nwszMD5: %ws\n",
        prefix, uniMD5FilePath, wszImageFileName, wszMD5
    );

    // UNICODE_STRING 래핑
    UNICODE_STRING ustr;
    RtlInitUnicodeString(&ustr, wszLogMsg);

    // ANSI_STRING 생성 (커널모드 변환)
    ANSI_STRING astr;
    NTSTATUS status = RtlUnicodeStringToAnsiString(&astr, &ustr, TRUE);

    if (NT_SUCCESS(status)) {
        // ANSI 문자열로 파일 로그
        WriteToLogFile(L"\\??\\C:\\Program Files\\Temp\\TmpDriver.log", astr.Buffer);
        // 반드시 해제
        RtlFreeAnsiString(&astr);
    }

    // DbgPrint에도 동일 출력 (Windbg에서 한글 포함 출력 가능)
    DbgPrint("%ws uniMD5FilePath: %ws\nwszImageFileName: %ws\nwszMD5: %ws\n",
        prefix, uniMD5FilePath, wszImageFileName, wszMD5);
}

반응형