Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,

I have a function is designed to return integer type, but I need to return HWND ( handle of an window) of an window from the same function.

I am trying to cast as below.However I am not seeing results as expected.
Am I doing somthing somthing wrong in conversion?

Please let me know, if you have any idea.

Thanks in advance.

Regards,
Joy
Posted
Comments
Sergey Alexandrovich Kryukov 14-May-13 9:45am    
Why? If it actually, by its semantic, returns HWND, it cannot be "arithmetic" integer. Change the function itself.
—SA

In most cases, you don't need such case. I can explain: on the user size, there is no arithmetic operations with HWND; they are some abstract pointers; you only get HWND values from Windows API and pass HWND as parameters of other API function, never do any calculations with then on the user side.

However, there are rare cases when it is needed, such as casting between HWND and LPARAM, when sending or handling messages. Then, well, make a type cast.

Anyway, it's a must to learn C++ typecast, which is very important part of programming: http://www.cplusplus.com/doc/tutorial/typecasting/[^].

—SA
 
Share this answer
 
v3
Hi,

the worst case is to use a type casting. In this case you have to be very carefull about the data types and the conversion that you use, because of possible corruption of the value.

Much better sollution would be to modify the function/method, so that it will return the HWND in an output parameter:
C++
int WonderfullMethod(HWND& outWnd)
{
   // the integer to return in the end
   int   iRetVal = 0;
   HWND  someWnd;

   // some code modifying the local variables declared above
   // ...

   // before we return, assign the value to the output parameter
   outWnd = someWnd;

   return iRetVal; 
}

In this case, the method will return an integer, and fill the output parameter with the HWND value.

Hope this helps.

Best regards,
J. K.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900