Hello,

I'm having a problem using UNICODE_STRING in a structure.

First, I have a structure with a PUNICODE type data and
others values, this a chained list and I declare a global
variable :
---
typedef struct _FOO
{
PUNICODE_STRING string;
WORD value;
struct FOO* next;
} FOO, *PFOO;

PFOO GLOBAL;
---

Second, I have a function receive a WCHAR* type data and
convert it to UNICODE_STRING and call a second function :
---
void foo(WCHAR *value)
{
UNICODE_STRING uString;

RtlInitUnicodeString(&uString, value);
foo2(&uString);
testList();
}
---

Third, the second function. It fill the structure with the
string :
---
void foo2(PUNICODE_STRING uString)
{
PFOO foo_s, e;

foo_s = ExAllocatePoo(NonPagedPool, sizeof(FOO));
foo_s->string = uString;

if (GLOBAL)
{
e = GLOBAL;
while (e->next)
e = e->next;
e->next = foo_s;
} else {
GLOBAL = foo_s;
}
}
---

Fourth, Hmm...this the "testList()" that iterate into the list:

---
void testList(void)
{
while (GLOBAL)
{
DbgPrint("%wZ\n", GLOBAL->string);
GLOBAL = GLOBAL->next;
}
}
---

My problem is the PUNICODE_STRING type data. When I display it
I have nothing !!! I diddn't put it in the test but the DWORD
value in the list is ok.

Where I'm wrong ?


Regards,

Re: Retrieve UNICODE_STRING... by janemba

janemba
Mon Jul 16 18:22:59 CDT 2007

On Tue, 17 Jul 2007 01:21:31 +0200, janemba wrote:


> My problem is the PUNICODE_STRING type data. When I display it
> I have nothing !!! I diddn't put it in the test but the DWORD
> value in the list is ok.
>
> Where I'm wrong ?
>

I forgot one thing...I'm in DDK.

Regards,

RE: Retrieve UNICODE_STRING... by dannyzhao

dannyzhao
Tue Jul 17 21:56:01 CDT 2007

hi,janemba
your FOO->string is the pointer, the real unicode_string is allocated on
stack.
when it is out of scope, FOO->string is invalid. for example ,when you
call foo the second time, when DbgPrint("%wZ\n", GLOBAL->string) is first
called, Global->string is already invalid;
danny
"janemba" wrote:

> Hello,
>
> I'm having a problem using UNICODE_STRING in a structure.
>
> First, I have a structure with a PUNICODE type data and
> others values, this a chained list and I declare a global
> variable :
> ---
> typedef struct _FOO
> {
> PUNICODE_STRING string;
> WORD value;
> struct FOO* next;
> } FOO, *PFOO;
>
> PFOO GLOBAL;
> ---
>
> Second, I have a function receive a WCHAR* type data and
> convert it to UNICODE_STRING and call a second function :
> ---
> void foo(WCHAR *value)
> {
> UNICODE_STRING uString;
>
> RtlInitUnicodeString(&uString, value);
> foo2(&uString);
> testList();
> }
> ---
>
> Third, the second function. It fill the structure with the
> string :
> ---
> void foo2(PUNICODE_STRING uString)
> {
> PFOO foo_s, e;
>
> foo_s = ExAllocatePoo(NonPagedPool, sizeof(FOO));
> foo_s->string = uString;
>
> if (GLOBAL)
> {
> e = GLOBAL;
> while (e->next)
> e = e->next;
> e->next = foo_s;
> } else {
> GLOBAL = foo_s;
> }
> }
> ---
>
> Fourth, Hmm...this the "testList()" that iterate into the list:
>
> ---
> void testList(void)
> {
> while (GLOBAL)
> {
> DbgPrint("%wZ\n", GLOBAL->string);
> GLOBAL = GLOBAL->next;
> }
> }
> ---
>
> My problem is the PUNICODE_STRING type data. When I display it
> I have nothing !!! I diddn't put it in the test but the DWORD
> value in the list is ok.
>
> Where I'm wrong ?
>
>
> Regards,
>

Re: Retrieve UNICODE_STRING... by Tim

Tim
Wed Jul 18 01:05:54 CDT 2007

janemba <janemba@wanadoo.fr> wrote:
>
>I'm having a problem using UNICODE_STRING in a structure.
>
>First, I have a structure with a PUNICODE type data and
>others values, this a chained list and I declare a global
>variable :

Danny correctly diagnosed the problem. I thought I would suggest a fix.

>---
>typedef struct _FOO
>{
> PUNICODE_STRING string;

Make this a UNICODE_STRING instead of a pointer. A UNICODE_STRING is only
12 bytes, so it doesn't cost you very much.

> WORD value;
> struct FOO* next;
>} FOO, *PFOO;

However, there are more problems in your code:

>PFOO GLOBAL;

It's true that this will be initialized to zero, but I never like to rely
on that in case I change it to a function local variable at some point. I
would write

PFOO GLOBAL;

Actually, I would not make this a global variable, and I would never, ever
name a global variable with all caps.

>Fourth, Hmm...this the "testList()" that iterate into the list:
>
>---
>void testList(void)
>{
> while (GLOBAL)
> {
> DbgPrint("%wZ\n", GLOBAL->string);
> GLOBAL = GLOBAL->next;
> }
>}

At the end of this, you have leaked all of the memory you allocated. You
have lost the pointer to the list. You should use a local, like you did in
the previous function.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.