>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++
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
> 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
"Doug Harrison [MVP]" <d...@mvps.org> wrote in message
news:3dok90p3udei4lt5f...@4ax.com...
>%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.
...so you're not expecting your code to work in a couple of years' time?
S.
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 - 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).