Thread: How to concat PUNICODE_STRING

  1. #1
    Registered User
    Join Date
    Jul 2023
    Posts
    5

    Question How to concat PUNICODE_STRING

    In the process of writing a simple minifilter driver, I can't seem to figure out how to do this. Admittedly, I have extremely limited experience with C/C++ (although I have a decade's worth of other languages). The following works in a C project:

    Code:
        PUNICODE_STRING _d;
        _d = malloc(sizeof(UNICODE_STRING));
        NTSTATUS _d_status = RtlUnicodeStringInit(_d, d);
        // Check status...
        // Where _c is an existing PUNICODE_STRING
        NTSTATUS _c_d_status = RtlStringCbCatA(_c, sizeof(_c), _d);
        // Check status...
        printf("_c_d: %wZ\r\n", _c);
    When done in a KMDF project, I use ExAllocatePool2 instead. Fails on the first RtlUnicodeStringInit with 0xc0000005.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    So what is 'd' ?
    The 2nd parameter to RtlUnicodeStringInit ?

    RtlUnicodeStringInit function (ntstrsafe.h) - Windows drivers | Microsoft Learn
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2023
    Posts
    5
    Sorry, substitute that for L"abc" (or any sample text).

  4. #4
    Registered User
    Join Date
    Jul 2023
    Posts
    5
    So this works, but changes to TextA affect MainText (as expected).

    Code:
        // MainText is PUNICODE_STRING
        NTSTRSAFE_PWSTR TextA = MainText->Buffer;
    
        UNICODE_STRING TextB;
        RtlInitUnicodeString(&TextB, L"abc");
    
        NTSTATUS status = RtlStringCbCatW(TextA, MainText->Length + TextB.Length + 2, TextB.Buffer);

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > When done in a KMDF project, I use ExAllocatePool2 instead. Fails on the first RtlUnicodeStringInit with 0xc0000005.
    0xc0000005 means access violation.

    Did you bother to check ExAllocatePool2 returned valid memory?
    ExAllocatePool2 - Windows drivers | Microsoft Learn
    There's a whole bunch of 'gotcha's' you need to be aware of.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2023
    Posts
    5
    For whatever reason, I just assumed there's no reason that call could've failed. Besides, it was RtlUnicodeStringInit which was failing. Well, it'd help if I was initializing the memory with the correct param for Flags. POOL_FLAGS instead of POOL_TYPE got me moving forward.

    The following works, as a deeper copy:

    Code:
        UNICODE_STRING tst = EMPTY_UNICODE_STRING;
        tst.MaximumLength = MainText->Length + 1;
        tst.Buffer = ExAllocatePool2(POOL_FLAG_NON_PAGED, MainText->Length + 1, DRIVER_TAG);
        if (NULL == tst.Buffer) {
            DbgPrint("ExAllocatePool2 failed!\r\n");
        }
        else {
            RtlCopyUnicodeString(&tst, MainText);
        }

  7. #7
    Registered User
    Join Date
    Jul 2023
    Posts
    5
    Quote Originally Posted by tmontney View Post
    So this works, but changes to TextA affect MainText (as expected).

    Code:
        // MainText is PUNICODE_STRING
        NTSTRSAFE_PWSTR TextA = MainText->Buffer;
    
        UNICODE_STRING TextB;
        RtlInitUnicodeString(&TextB, L"abc");
    
        NTSTATUS status = RtlStringCbCatW(TextA, MainText->Length + TextB.Length + 2, TextB.Buffer);
    Should use the following instead of RtlStringCbCatW:

    Code:
    status = RtlUnicodeStringCatString(TextA, L"abc");
    I'm fumbling my way through this, and realized there's an exact function for appending Unicode with a "string".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concat strings
    By pollypocket4eva in forum C Programming
    Replies: 9
    Last Post: 11-10-2008, 01:17 PM
  2. string concat
    By guitarist809 in forum C++ Programming
    Replies: 6
    Last Post: 12-14-2007, 05:05 PM
  3. concat overload +,-
    By rip1968 in forum C++ Programming
    Replies: 5
    Last Post: 05-06-2002, 12:06 PM
  4. string concat
    By weedus in forum C Programming
    Replies: 5
    Last Post: 03-07-2002, 09:11 AM
  5. String Concat
    By Silverdream in forum C Programming
    Replies: 7
    Last Post: 02-06-2002, 12:46 PM

Tags for this Thread