473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 11004
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
7
by: Daniel Moree | last post by:
I have a program that uses getline() to read a line from a file. It's doing this for an INI file reader. I'm trying to get the line parser function to remove the comments from each line if there...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
5
by: The Real Andy | last post by:
Sorry if this question sounds stupid, its early days for me when it comes to c# and com interop. I have a method imported from Win Media PLayer SDK, IWMPPluginUI:DisplayPropertyPage, like so: ...
2
by: Harry Whitehouse | last post by:
I'm porting an application from Borland C++ to VS .NET 2003. The VC++ compiler doesn't want to deal with declarations like this in my code: LONG FAR PASCAL _export StdPVDlgProc(HWND hWnd, UINT...
5
by: Lindsay | last post by:
I'm getting some strange warnings when I compile: pointer truncation from 'HMENU' to 'unsigned int' The line in question is: AppendMenu(hMenu,MF_STRING|MF_POPUP,UINT(hStyle),"Style"); (where...
8
by: Charles Sullivan | last post by:
I have a program written in C under Linux (gcc) which a user has ported to run under AT&T SysV R4. He sent me a copy of his makelog which displays a large number of compiler warnings similar to...
45
by: alertjean | last post by:
Or may be I am stubborn or dumb ... of not putting in a * in the typecast. This is code I am worrying about long long b=1; int *address ; address=(int)&b; printf ("%x %x...
3
by: gil | last post by:
Hi, I'm trying to find the best way to work with compiler warnings. I'd like to remove *all* warnings from the code, and playing around with the warning level, I've noticed that compiling with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.