473,892 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ca st' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc (System::IntPtr hwnd)
{
::MessageBox(re interpret_cast< HWND> (hwnd.ToInt32() ), "Some text",
"Caption",
MB_ICONEXCLAMAT ION | 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)\AVI Generator\AVIGe nerator.h(217) : warning C4312:
'reinterpret_ca st' : conversion from 'int' to 'HWND' of greater size
Nov 16 '05 #1
3 11051
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_ca st' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc (System::IntPtr hwnd)
{
::MessageBox(re interpret_cast< HWND> (hwnd.ToInt32() ), "Some text",
"Caption",
MB_ICONEXCLAMAT ION | 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_cas t, e.g.

#using <mscorlib.dll >

typedef void* HWND;

void f(System::IntPt r hwnd)
{
reinterpret_cas t<HWND>(hwnd.To Int32());
}

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
specificall y 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_ca st' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc (System::IntPtr hwnd)
{
::MessageBox(re interpret_cast< HWND> (hwnd.ToInt32() ), "Some text",
"Caption",
MB_ICONEXCLAMAT ION | 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_cas t, e.g.

#using <mscorlib.dll >

typedef void* HWND;

void f(System::IntPt r hwnd)
{
reinterpret_cas t<HWND>(hwnd.To Int32());
}

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_cas t<HWND>(hwnd.To Pointer());

--
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.c om...
Doug Harrison [MVP] wrote:
Andrew Moore wrote:
Hi All,

I have a managed C++ class that makes calls into the Win32 API. I
specificall y 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_ca st' : conversion from 'int' to 'HWND' of
greater size.

My code looks like the following:

void MyCPlusPlusFunc (System::IntPtr hwnd)
{
::MessageBox(re interpret_cast< HWND> (hwnd.ToInt32() ), "Some text",
"Caption",
MB_ICONEXCLAMAT ION | 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_cas t, e.g.

#using <mscorlib.dll >

typedef void* HWND;

void f(System::IntPt r hwnd)
{
reinterpret_cas t<HWND>(hwnd.To Int32());
}

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_cas t<HWND>(hwnd.To Pointer());

--
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
11235
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 generates this warning ... "warning C4311: 'type cast' : pointer truncation from 'void *' to 'DWORD'" The warning is generated as the pointer is apparently 64 bits long (even though a quick sizeof() reports it to only be 4 bytes in length) and...
7
2899
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 are some. I've got this done using: std::string. I need to cast it from std::string(basic string) to a CString(LPCTSTR). When I use String.c_str function, it won't cast it to an LPCTSTR, i've also tried a nonconstant version: LPSTR. Any help...
29
2537
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
12256
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: void DisplayPropertyPage(IntPtr hwndParent) I need to pass hwndParent to a dialog, but I cant for the life of me figure out how to cast it to a windows.forms.form handle.
2
1198
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 msg, WPARAM wParam, LPARAM lParam) { }
5
3640
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 hStyle is HMENU type) This line is exactly the same as a line in another app I have but that one doesn't give a warning.
8
2021
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 this: warning: semantics of ">>" change in ANSI C; use explicit cast The statement to which this applies is: xuc = ((uc & 0xF0 ) >4);
45
2307
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 \n",*(address+1)*address); when I compile the code I get this warning from gcc (although it
3
3205
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 /W3 I get warnings that with /W4 are shown as remarks, e.g.: warning #177: variable "Foo" was declared but never referenced ....is displayed as a "remark #177" with /W4. That doesn't fit the
0
9981
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11241
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9644
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5857
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4683
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4279
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.