Ansi/Unicode strings

Hi gurus ,
I am a starter.
Had a basic question on strings?


**CASE1**
PANSI_STRING File_Expression;

RtlInitAnsiString(File_Expression,"\DEVICE\HARDDISKVOLUME1");

Here I get a compliel error.
"Local Variable used without having been initialised."

BUT..

**CASE2**
ANSI_STRING File_Expression;

RtlInitAnsiString(&File_Expression,"\DEVICE\HARDDISKVOLUME1");

1>Here I do not get any error. WHY?
I guess in both cases we are making an ANSI string Constants right?

In the first case if I do like this..

**CASE3**

File_Expression=ExAllocatePool(NonPagedPool,sizeof(ANSI_STRING));

File_Expression->Buffer=ExAllocatePool(NonPagedPool,sizeof(ANSI_STRING)*
22);
RtlInitAnsiString(File_Expression,"\DEVICE\HARDDISKVOLUME1");

I get no error.
2> So in **CASE3** , Did I allocate memory dynamically ?Or is it still a
String constant.

3> Lastly I am not supposed to free memory for a string constant as the
memory belong to my code or data area.
So in which case above I do not use a free memory routine Like
RtlFreeAnsiString.

I thing I can Generalise these questions to UNICODE Strings also .

Any Comments will be very help full.

Thx & rds
Anurag

The correct way is using “&” sign. This is more a C language question then
a DDK one.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Anurag Sarin”
To: “Windows File Systems Devs Interest List”
Sent: Thursday, August 12, 2004 6:56 PM
Subject: [ntfsd] Ansi/Unicode strings

> Hi gurus ,
> I am a starter.
> Had a basic question on strings?
>
> -------------------------------------
> CASE1
> PANSI_STRING File_Expression;
>
> RtlInitAnsiString(File_Expression,“\DEVICE\HARDDISKVOLUME1”);
> ---------------------------------------------------
>
> Here I get a compliel error.
> “Local Variable used without having been initialised.”
>
> BUT…
> --------------------------------
> CASE2
> ANSI_STRING File_Expression;
>
> RtlInitAnsiString(&File_Expression,“\DEVICE\HARDDISKVOLUME1”);
> ---------------------------------------
>
> 1>Here I do not get any error. WHY?
> I guess in both cases we are making an ANSI string Constants right?
>
> In the first case if I do like this…
> ------------------------------------------------
> CASE3
>
> File_Expression=ExAllocatePool(NonPagedPool,sizeof(ANSI_STRING));
>
> File_Expression->Buffer=ExAllocatePool(NonPagedPool,sizeof(ANSI_STRING)*
> 22);
> RtlInitAnsiString(File_Expression,“\DEVICE\HARDDISKVOLUME1”);
> --------------------------------------------------------------
> I get no error.
> 2> So in CASE3 , Did I allocate memory dynamically ?Or is it still a
> String constant.
>
> 3> Lastly I am not supposed to free memory for a string constant as the
> memory belong to my code or data area.
> So in which case above I do not use a free memory routine Like
> RtlFreeAnsiString.
>
> I thing I can Generalise these questions to UNICODE Strings also .
>
> Any Comments will be very help full.
>
> Thx & rds
> Anurag
>
>
> —
> Questions? First check the IFS FAQ at
https://www.osronline.com/article.cfm?id=17
>
> You are currently subscribed to ntfsd as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

Anurag,

PANSI_STRING and ANSI_STRING are quite different. The first is a pointer to a ANSI_STRING struct, where the second is the ANSI_STRING struct itself. As such, it makes sense that you would have problems when compiling in the use you have shown.

You will notice in the docs that the whole purpose of RtlInitAnsiString() is to allocate and initialize the .Buffer to the length of the dest string, and then copy it in.

The easiest/cleanest way would be to use:

ANSI_STRING File_Expression;
RtlInitAnsiString( &File_Expression, “\DEVICE\HARDDISKVOLUME1” );

In this case you wouldn’t need to free the memory.

If I read between the lines here, I am guessing you are wanting a PANSI_STRING as its required. There is nothing wrong with just passing the ANSI_STRING by reference (notice the ‘&’ in the first argument of RtlInitAnsiString that I used) in this manner. This really isn’t a ntfsd/ddk issue, its more of practical C programming use.

BTW, never use RtlFreeAnsiString in the manner you suggest. The only time you would use this function would be if you were CONVERTING a UNICODE_STRING to an ANSI_STRING with functions like RtlUnicodeStringToAnsiString().

Good luck.


Regards,
Dana Epp
[Blog: http://silverstr.ufies.org/blog/]

Anurag Sarin wrote:

Hi gurus ,
I am a starter.
Had a basic question on strings?


**CASE1**
PANSI_STRING File_Expression;

RtlInitAnsiString(File_Expression,“\DEVICE\HARDDISKVOLUME1”);

Here I get a compliel error.
“Local Variable used without having been initialised.”

BUT…

**CASE2**
ANSI_STRING File_Expression;

RtlInitAnsiString(&File_Expression,“\DEVICE\HARDDISKVOLUME1”);

1>Here I do not get any error. WHY?
I guess in both cases we are making an ANSI string Constants right?

In the first case if I do like this…

**CASE3**

File_Expression=ExAllocatePool(NonPagedPool,sizeof(ANSI_STRING));

File_Expression->Buffer=ExAllocatePool(NonPagedPool,sizeof(ANSI_STRING)*
22);
RtlInitAnsiString(File_Expression,“\DEVICE\HARDDISKVOLUME1”);

I get no error.
2> So in **CASE3** , Did I allocate memory dynamically ?Or is it still a
String constant.

3> Lastly I am not supposed to free memory for a string constant as the
memory belong to my code or data area.
So in which case above I do not use a free memory routine Like
RtlFreeAnsiString.

I thing I can Generalise these questions to UNICODE Strings also .

Any Comments will be very help full.

Thx & rds
Anurag


Questions? First check the IFS FAQ at https://www.osronline.com/article.cfm?id=17

You are currently subscribed to ntfsd as: xxxxx@vulscan.com
To unsubscribe send a blank email to xxxxx@lists.osr.com