c++cvisual-studiodriverkernel-mode

How convert char * (char pointer) to PCSZ?


I have a method that has a mandatory parameter as char* and I want convert to PCSZ before RtlInitiAnsiString() and the result of uName after RtlAnsiStringToUnicodeString() to be the correct value.

How can I 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
    //...
    RtlFreeUnicodeString(&uName);
    return status;    
}

EDITION 01:

To a better understand here is how MyMethod() is used in my kernel driver:

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

Solution

  • There's nothing to convert. PCSZ is a Pointer to Constant String Zero-terminated. So, it's just const char *. char * is implicitly convertible to const char *.

    I consider such typedefs horrible, but unfortunately, Microsoft APIs make heavy use of them.