472,131 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

Compiler warnings casting int to HWND

Hi All,

I have a managed C++ class that makes calls into the Win32 API. I
specifically am trying to take a Handle from a .NET form and convert it to a
HWND to pass to a Win32 functions. The code works properly but I get the
following compiler warning:

warning C4312: 'reinterpret_cast' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc(System::IntPtr hwnd)
{
::MessageBox(reinterpret_cast<HWND> (hwnd.ToInt32()), "Some text",
"Caption",
MB_ICONEXCLAMATION | MB_APPLMODAL);
}
void CSharpFunc()
{
System.Windows.Forms.Form f;

MyCPlusPlusFunc(f.Handle);
}

The size of int and HWND are both 4 bytes. Is there a way to get this to
work without the compiler warnings?

Thanks for your help.

Andrew
c:\Arena3D\v1.0.0.260(AVI)\AVIGenerator\AVIGenerat or.h(217) : warning C4312:
'reinterpret_cast' : conversion from 'int' to 'HWND' of greater size
Nov 16 '05 #1
3 10822
Andrew Moore wrote:
Hi All,

I have a managed C++ class that makes calls into the Win32 API. I
specifically am trying to take a Handle from a .NET form and convert it to a
HWND to pass to a Win32 functions. The code works properly but I get the
following compiler warning:

warning C4312: 'reinterpret_cast' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc(System::IntPtr hwnd)
{
::MessageBox(reinterpret_cast<HWND> (hwnd.ToInt32()), "Some text",
"Caption",
MB_ICONEXCLAMATION | MB_APPLMODAL);
}
void CSharpFunc()
{
System.Windows.Forms.Form f;

MyCPlusPlusFunc(f.Handle);
}

The size of int and HWND are both 4 bytes.
Not on Win64. On Win64, int remains 32 bits, but HWND widens to 64.
Is there a way to get this to work without the compiler warnings?


I guess you're using MessageBox through PInvoke? Where are you getting the
HWND type? If it's a 64 bit type and you're targeting Win32, you have a
problem. Besides ToInt32, IntPtr defines ToInt64 and ToPointer methods. The
ToPointer method is probably the one you want, but it isn't CLS-compliant,
if that matters to you. For a testbed, I'd suggest putting together a tiny
console application fragment that consists of your HWND definition and a
function containing a single statement, your reinterpret_cast, e.g.

#using <mscorlib.dll>

typedef void* HWND;

void f(System::IntPtr hwnd)
{
reinterpret_cast<HWND>(hwnd.ToInt32());
}

Show me how to make this fail in the way you described.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #2
Doug Harrison [MVP] wrote:
Andrew Moore wrote:
Hi All,

I have a managed C++ class that makes calls into the Win32 API. I
specifically am trying to take a Handle from a .NET form and convert it to a
HWND to pass to a Win32 functions. The code works properly but I get the
following compiler warning:

warning C4312: 'reinterpret_cast' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc(System::IntPtr hwnd)
{
::MessageBox(reinterpret_cast<HWND> (hwnd.ToInt32()), "Some text",
"Caption",
MB_ICONEXCLAMATION | MB_APPLMODAL);
}
void CSharpFunc()
{
System.Windows.Forms.Form f;

MyCPlusPlusFunc(f.Handle);
}

The size of int and HWND are both 4 bytes.


Not on Win64. On Win64, int remains 32 bits, but HWND widens to 64.
Is there a way to get this to work without the compiler warnings?


I guess you're using MessageBox through PInvoke? Where are you getting the
HWND type? If it's a 64 bit type and you're targeting Win32, you have a
problem. Besides ToInt32, IntPtr defines ToInt64 and ToPointer methods. The
ToPointer method is probably the one you want, but it isn't CLS-compliant,
if that matters to you. For a testbed, I'd suggest putting together a tiny
console application fragment that consists of your HWND definition and a
function containing a single statement, your reinterpret_cast, e.g.

#using <mscorlib.dll>

typedef void* HWND;

void f(System::IntPtr hwnd)
{
reinterpret_cast<HWND>(hwnd.ToInt32());
}

Show me how to make this fail in the way you described.


OK, I can duplicate this problem if I compile the fragment above with /Wp64.
To avoid the problem in the IDE, you could go into Project Settings | C/C++
| General and turn off "Detect 64-bit portability issues", but this is a
better way:

reinterpret_cast<HWND>(hwnd.ToPointer());

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #3
Doug,

Thank you for you help.

Andrew Moore

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:ti********************************@4ax.com...
Doug Harrison [MVP] wrote:
Andrew Moore wrote:
Hi All,

I have a managed C++ class that makes calls into the Win32 API. I
specifically am trying to take a Handle from a .NET form and convert it to aHWND to pass to a Win32 functions. The code works properly but I get the
following compiler warning:

warning C4312: 'reinterpret_cast' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc(System::IntPtr hwnd)
{
::MessageBox(reinterpret_cast<HWND> (hwnd.ToInt32()), "Some text",
"Caption",
MB_ICONEXCLAMATION | MB_APPLMODAL);
}
void CSharpFunc()
{
System.Windows.Forms.Form f;

MyCPlusPlusFunc(f.Handle);
}

The size of int and HWND are both 4 bytes.
Not on Win64. On Win64, int remains 32 bits, but HWND widens to 64.
Is there a way to get this to work without the compiler warnings?


I guess you're using MessageBox through PInvoke? Where are you getting theHWND type? If it's a 64 bit type and you're targeting Win32, you have a
problem. Besides ToInt32, IntPtr defines ToInt64 and ToPointer methods. TheToPointer method is probably the one you want, but it isn't CLS-compliant,if that matters to you. For a testbed, I'd suggest putting together a tinyconsole application fragment that consists of your HWND definition and a
function containing a single statement, your reinterpret_cast, e.g.

#using <mscorlib.dll>

typedef void* HWND;

void f(System::IntPtr hwnd)
{
reinterpret_cast<HWND>(hwnd.ToInt32());
}

Show me how to make this fail in the way you described.


OK, I can duplicate this problem if I compile the fragment above with

/Wp64. To avoid the problem in the IDE, you could go into Project Settings | C/C++ | General and turn off "Detect 64-bit portability issues", but this is a
better way:

reinterpret_cast<HWND>(hwnd.ToPointer());

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Daniel Moree | last post: by
29 posts views Thread by junky_fellow | last post: by
5 posts views Thread by The Real Andy | last post: by
2 posts views Thread by Harry Whitehouse | last post: by
5 posts views Thread by Lindsay | last post: by
8 posts views Thread by Charles Sullivan | last post: by
45 posts views Thread by alertjean | last post: by
3 posts views Thread by gil | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.