Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

type casting DWORD to char*

74 views
Skip to first unread message

Ryan Dawson

unread,
May 31, 2002, 1:57:24 PM5/31/02
to
I am trying to cast a DWORD into char*, and then display the string in an
edit control. For some reason I always get an access violation and it is
very frustrating. Any Help would be greatly appreciated or any other way to
get past this.
Ryan Dawson
Here is the code:
DWORD dwWindowThreadID = ::GetCurrentThreadId ();

const char* szWindowThreadID = (const char*)dwWindowThreadID;

::SetDlgItemText (hDlg, IDC_EDIT_WINDOW, szWindowThreadID);


Craig Powers

unread,
May 31, 2002, 3:18:40 PM5/31/02
to

This was answered in microsoft.public.vc.language, I believe.

Please don't multipost. If multiple groups are appropriate, crosspost.

Julius

unread,
May 31, 2002, 3:17:49 PM5/31/02
to
You want to use the _ltoa function instead of casting. If you cast a DWORD
to a char*, you basically end up with an array of bytes starting at whatever
address the DWORD equals. That array continues until the first NULL
character. So, when you go to use the char*, 99 times out of 100, you will
end up reading memory that doesn't belong to you.

In other words, after the cast szWindowThreadID actually points to an array
of chars at whatever address is equivalent to dwWindowThreadID.

This little snippet should work for you:


DWORD dwWindowThreadID = ::GetCurrentThreadId ();

char szWindowThreadID[20];
_ltoa(dwWindowThreadID, szWindowThreadID, 10);
::SetDlgItemText (hDlg, IDC_EDIT_WINDOW, szWindowThreadID);


Hope that helps....

"Ryan Dawson" <rd...@msn.com> wrote in message
news:#ZGeByMCCHA.1576@tkmsftngp04...

David (dkyc)

unread,
May 31, 2002, 3:44:35 PM5/31/02
to
What you're doing would result in an access violation,
because you are using the DWORD 'value' as a pointer
address. What you're doing just won't work. If you're
trying to display the ID, you need to convert the DWORD
into its string form using _ultoa() or something. Remember
0 (or null) terminates a string.

David (dkyc)

>.
>

0 new messages