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 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++
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++
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++ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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)
{
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |