Need to convert an INT to WORD*
I'm working with a third party software that only allows passing of Strings, Doubles, and Integers to the DLL that I'm attempting to write. The DLL(MyDLL) is being written to work as a wrapper around a DLL(OtherDLL) that the third party is unable to call. Some of the calls to OtherDLL require me to send a WORD* but I have no way of sending a WORD* directly from the third party software. I'm attempting to send a INT from the third party software and converting it to the WORD* type but it won't let me. I keep getting the following message from Visual Studio 2017: "'type cast': conversion from 'INT' to 'WORD *' of greater size".
Sample:
WORD *wpAddress;
INT nAddress;
wpAddress = (WORD*)nAddress; //type cast
Thanks,
George
Re: Need to convert an INT to WORD*
Re: Need to convert an INT to WORD*
My wrapper has to be 32bit since the third party only supports 32bit.
Thanks,
George
Re: Need to convert an INT to WORD*
You are compiling as 64bit - Compiler warning C4312 - 'type cast': conversion from 'INT' to 'WORD *' of greater size is only issued when trying to assign a 32-bit value to a 64-bit pointer type, for example, casting a 32-bit int or long to a 64-bit pointer.
If you compile as 32 bit then this warning should disappear. However, to remove the warning as compiling for 64 bit, first cast to long long:
Code:
INT nAddress {};
auto wpAddress {(WORD*)(long long)nAddress};
Re: Need to convert an INT to WORD*
As a last resort, there's always the reinterpret_cast,
Code:
wpAddress = reinterpret_cast<WORD*>(nAddress);
https://en.cppreference.com/w/cpp/la...interpret_cast
Re: Need to convert an INT to WORD*
That still gives the compiler warning - as it's just the same as the (WORD*) c-style cast.
Re: Need to convert an INT to WORD*
Quote:
Originally Posted by
2kaud
That still gives the compiler warning - as it's just the same as the (WORD*) c-style cast.
Well, according to the link in my previous post, according to the C++ standard, a reinterpret_cast should work,
"3) A value of any integral or enumeration type can be converted to a pointer type."
But I realize now that the OP had just a warning and not an error. Still, C4312 is a level 1 warning, and even though it can be suppressed, the cast is still risky.