none
How convert char * (char pointer) to PCSZ? RRS feed

  • Question

  • I have a method that have a mandatory parameter as char * and i want convert to PCSZ before of RtlInitiAnsiStrinrg() to the

    result of "uName" after RtlAnsiStringToUnicodeString() be the correct value.

    How do this?

    NTSTATUS myMethod(char *myName) { ANSI_STRING aName; UNICODE_STRING uName; OBJECT_ATTRIBUTES ObjAttr; RtlInitAnsiString(&aName, myName); status = RtlAnsiStringToUnicodeString(&uName, &aName, TRUE); if(!NT_SUCCESS(status)) { DbgPrint("RtlAnsiStringToUnicodeString Error"); return status; } InitializeObjectAttributes(&ObjAttr, &uName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);

    // some code here, eg: DbgPrint("Result is: %s", uName);

    //...

    RtlFreeUnicodeString(&uName);

    return status;

    }


    EDIT:

    To a better understand here is how myMethod() is used:

    struct MyData
    {
        ULONG Value[3];
        char *Str1;
        char *Str2;
    };
    
    NTSTATUS Function_IRP_DEVICE_CONTROL(PDEVICE_OBJECT pDeviceObject, PIRP Irp)
    {
        PIO_STACK_LOCATION pIoStackLocation;
        struct MyData *pData = (struct MyData*) Irp->AssociatedIrp.SystemBuffer;
        pIoStackLocation = IoGetCurrentIrpStackLocation(Irp);
        switch (pIoStackLocation->Parameters.DeviceIoControl.IoControlCode)
        {
            case IOCTL_DATA :
                DbgPrint("IOCTL DATA");
                DbgPrint("%lu \n %lu \n %lu \n %s \n %s", pData->Value[0], pData->Value[1], pData->Value[2], pData->Str1, pData->Str2);
                ...
                break;
        }
    
    ...
    
    //////////// calling MyMethod() //////////////
    
    myMethod(pData->Str1);




    • Edited by FLASHCODER Tuesday, August 15, 2017 12:48 PM
    Tuesday, August 15, 2017 4:40 AM

Answers

  • You can simply cast it, I.E.

    RtlInitAnsiString(&aName, (PCSZ) myName);

    For that matter the code a presented should work.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by FLASHCODER Tuesday, August 15, 2017 1:21 PM
    Tuesday, August 15, 2017 11:08 AM
  • Because you are using %s which is for NULL terminated ANSI character strings.   You need:

    DbgPrint("Result is: %wZ", &uName);

    %Z indicates a counted string, the w indicated wide i.e. UNICODE characters, and finally you needed to pass the pointer to the string structure.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by FLASHCODER Tuesday, August 15, 2017 1:21 PM
    Tuesday, August 15, 2017 12:56 PM

All replies

  • You can simply cast it, I.E.

    RtlInitAnsiString(&aName, (PCSZ) myName);

    For that matter the code a presented should work.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by FLASHCODER Tuesday, August 15, 2017 1:21 PM
    Tuesday, August 15, 2017 11:08 AM
  • @Don Burn,

    i edited my question, see.

    Why uName in DbgPrint() is empty?

    Tuesday, August 15, 2017 12:51 PM
  • Because you are using %s which is for NULL terminated ANSI character strings.   You need:

    DbgPrint("Result is: %wZ", &uName);

    %Z indicates a counted string, the w indicated wide i.e. UNICODE characters, and finally you needed to pass the pointer to the string structure.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by FLASHCODER Tuesday, August 15, 2017 1:21 PM
    Tuesday, August 15, 2017 12:56 PM
  • Because you are using %s which is for NULL terminated ANSI character strings.   You need:

    DbgPrint("Result is: %wZ", &uName);

    %Z indicates a counted string, the w indicated wide i.e. UNICODE characters, and finally you needed to pass the pointer to the string structure.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Ok, worked.

    Thank you very much.

    Tuesday, August 15, 2017 1:20 PM