Win32 data types equivalant in Linux

前端 未结 4 1465
故里飘歌
故里飘歌 2021-02-09 17:41

I am trying to convert a C++ library which is using widely DWORD, CString and BYTE in the program, and now I am converting the code from C++ Win32 library to linux program . Als

相关标签:
4条回答
  • 2021-02-09 17:58

    CString will not convert directly to std::string, but it is a rough equivalent.

    BYTE is indeed unsigned char and DWORD is unsigned int. WORD is unsigned short.

    You should definitely use typedef actual_type WINDOWS_NAME; to fix the code up, don't go through everywhere to replace the types. I would add a new headerfile that is called something like "wintypes.h", and include that everywhere the "windows.h" is used.

    Edit for comment: With CString, it really depends on how it is used (and whether the code is using what MS calls "Unicode" or "ASCII" strings). I would be tempted to create a class CString and then use std::string inside that. Most of it can probably be done by simply calling the equivalent std::string function, but some functions may need a bit more programming - again, it does depend on what member functions of CString are actually being used.

    For LP<type>, that is just a pointer to the <type>, so typedef BYTE* LPBYTE; and typedef DWORD* LPDWORD; will do that.

    0 讨论(0)
  • 2021-02-09 18:01

    I would suggest to use uint32_t and uint8_t from for DWORD and BYTE and normal char * or const char * for strings (or the std:string class for C++).

    Probably best thought is to use typedefs for existing code:

    typedef unsigned char             BYTE;
    

    These can be changed easily.

    If you rewrite code use char, int, long were useful and the (u)intX_t types, were you need a defined size.

    0 讨论(0)
  • 2021-02-09 18:03
    • DWORD = uint32_t
    • BYTE = uint8_t

    These types are not OS specifics and were added to C++11. You need to include <cstdint> to get them. If you have an old compiler you could use boost/cstdint, which is header only.

    Use std::string instead CString, but you will need to change some code.

    With these changes your code should compile on both Windows and Linux.

    0 讨论(0)
  • 2021-02-09 18:07
    typedef unsigned long DWORD;
    typedef unsigned char BYTE;
    
    CString -> maybe basic_string<TCHAR> ?
    
    0 讨论(0)
提交回复
热议问题