CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2008
    Posts
    3

    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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need to convert an INT to WORD*

    32bit or 64bit dll?

  3. #3
    Join Date
    Oct 2008
    Posts
    3

    Re: Need to convert an INT to WORD*

    My wrapper has to be 32bit since the third party only supports 32bit.

    Thanks,
    George

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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};
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    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

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Feb 2017
    Posts
    677

    Re: Need to convert an INT to WORD*

    Quote Originally Posted by 2kaud View Post
    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.
    Last edited by wolle; January 13th, 2021 at 02:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured