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

Re: pointer truncation from hwnd to long

568 views
Skip to first unread message

Doug Harrison [MVP]

unread,
May 6, 2004, 12:16:33 PM5/6/04
to
Repstat wrote:

>Hi
>I am writing a program that necessitates converting an HWND into a string (char*), for this I'm using
>
>sprintf(lpszHwnd, "%d", (long)hwnd);
>
>it's giving me warning 'pointer truncation from hwnd to long'. Although the program's working fine and I can easily disable the warning, is there any better way to do it?
>
>I'm writing an unmanaged console application with VC7.1.

That sounds like a 64 bit portability warning. BTW, "%ld" is the conversion
specifier for long; "%d" is for int. You could use an INT_PTR cast, but then
how do you write the specifier, given that INT_PTR can be int or __int64?
Perhaps the most portable approach would be the following:

// Convert to hex
sprintf(lpszHwnd, "%I64x", (unsigned __int64) hwnd);

--
Doug Harrison
Microsoft MVP - Visual C++

andrea catto'

unread,
May 6, 2004, 12:32:57 PM5/6/04
to
here's the problem,
you may have in the project settings, the portability to 64bit detection set
up.
I had it, and it was driving me nuts.
I turned it off..

since AMD is so ahead of time with opteron/athlon64 now and microsoft is
developing so much for 64bit....... they are all starging to be somewhat
64bit aware.

if all you need is to convert into string........ use atoi()/_atoi64()
functions instead, they are WAY faster too, because they don't do
stdarg/formatting.

also anotherthing you can do is, use native types that ms provides, such as
__int8, __int16, __int32, __int64 (all of these may be preceeded by
unsigned).
these type casts are very "specific" so they may work better.

"Repstat" <anon...@discussions.microsoft.com> wrote in message
news:03C3FE19-D3FB-47A8...@microsoft.com...


> Hi
> I am writing a program that necessitates converting an HWND into a string
(char*), for this I'm using
>
> sprintf(lpszHwnd, "%d", (long)hwnd);
>
> it's giving me warning 'pointer truncation from hwnd to long'. Although
the program's working fine and I can easily disable the warning, is there
any better way to do it?
>
> I'm writing an unmanaged console application with VC7.1.
>

> Thanks


David Webber

unread,
May 6, 2004, 1:26:42 PM5/6/04
to

"Repstat" <anon...@discussions.microsoft.com> wrote in message
news:03C3FE19-D3FB-47A8...@microsoft.com...

> Hi
> I am writing a program that necessitates converting an HWND into a
string (char*), for this I'm using
>
> sprintf(lpszHwnd, "%d", (long)hwnd);
>
> it's giving me warning 'pointer truncation from hwnd to long'.
Although the program's working fine and I can easily disable the
warning, is there any better way to do it?
>
> I'm writing an unmanaged console application with VC7.1.

Use %ld in sprintf. (long is the same as int in VC7.1, so it
will work, but it is not safe if you go to a different compiler).

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


Ted Miller

unread,
May 6, 2004, 10:51:31 PM5/6/04
to
%p

"Doug Harrison [MVP]" <d...@mvps.org> wrote in message
news:3dok90p3udei4lt5f...@4ax.com...

Doug Harrison [MVP]

unread,
May 6, 2004, 11:44:50 PM5/6/04
to
Ted Miller wrote:

>%p

That should be fine, too. (Strictly speaking, you should cast the
corresponding pointer argument to void*, but that isn't necessary for any
architecture I know of.)

I wonder, does anyone have a pointer to documentation that says Windows
handle types like HWND, HDC, etc. are always some sort of pointer type
compatible with %p? I know that for Win32, with STRICT defined, they're
opaque pointers to unique types, and without STRICT, they're void*, IIRC,
but I learned that from spelunking in the Windows headers.

Simon Trew

unread,
May 7, 2004, 4:56:49 AM5/7/04
to

"andrea catto'" <aca...@dataflight.com> wrote in message
news:e48PKf4M...@TK2MSFTNGP11.phx.gbl...

> here's the problem,
> you may have in the project settings, the portability to 64bit detection
set
> up.
> I had it, and it was driving me nuts.
> I turned it off..

...so you're not expecting your code to work in a couple of years' time?

S.


andrea catto'

unread,
May 7, 2004, 12:55:51 PM5/7/04
to
here's the answer if you want to go 64bit.

the printf() formatting allows you to reference a 64bit value by using the
I64 modifier.

here's an example.

DWORD64 iValue = 4;
DWORD32 iAnother = 3; /* Since this is 32bit, I will cast it on the fly
later on */
char szString[128];
sprintf(szString, "%I64d", iValue); /* In decimal */
sprintf(szString, "%I64X", (DWORD64)iAnother); /* In Hexadecimal-uppercase
*/

/* REMEMBER WHEN USING I64 the stack will be used consequentially from
printf()
* in fact instead of a 32bit read, two will occur, so MAKE SURE the casting
is perfect, else the following
* arguments will be badly shifted and read */

"Repstat" <anon...@discussions.microsoft.com> wrote in message
news:03C3FE19-D3FB-47A8...@microsoft.com...

> Hi
> I am writing a program that necessitates converting an HWND into a string
(char*), for this I'm using
>
> sprintf(lpszHwnd, "%d", (long)hwnd);
>
> it's giving me warning 'pointer truncation from hwnd to long'. Although
the program's working fine and I can easily disable the warning, is there
any better way to do it?
>
> I'm writing an unmanaged console application with VC7.1.
>

> Thanks


Doug Harrison [MVP]

unread,
May 7, 2004, 3:10:46 PM5/7/04
to
Repstat wrote:

>Doug - I've used
>sprintf(strHwnd, "%ld", (unsigned __int64)hwnd);
>
>would that work on a 64bit computer?

Nope. The %ld specifier means long, and the type long remains 32 bits in
Win64. As you are formatting an unsigned __int64, you should use %I64x (hex)
or %I64u (decimal).

0 new messages