Compiler warnings casting int to HWND

A

Andrew Moore

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\AVIGenerator.h(217) : warning C4312:
'reinterpret_cast' : conversion from 'int' to 'HWND' of greater size
 
D

Doug Harrison [MVP]

Andrew said:
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.
 
D

Doug Harrison [MVP]

Doug said:
Not on Win64. On Win64, int remains 32 bits, but HWND widens to 64.


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());
 
A

Andrew Moore

Doug,

Thank you for you help.

Andrew Moore

Doug Harrison said:
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());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top