简体   繁体   English

Linux中的Win32数据类型等效

[英]Win32 data types equivalant in Linux

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 . 我试图转换一个在程序中广泛使用DWORD,CString和BYTE的C ++库,现在我将代码从C ++ Win32库转换为linux程序。 Also I am using openSUSE 12.3 and Anjuta IDE to do this , please help me which types I should use instead of mentioned types ? 另外我使用openSUSE 12.3和Anjuta IDE来做这个,请帮助我使用哪种类型而不是提到的类型? I think I should use unsigned int for DWORD and string for CString and unsigned char instead of BYTE is it right ? 我想我应该使用unsigned int for DWORD和string for CString and unsigned char而不是BYTE是不是?

CString will not convert directly to std::string , but it is a rough equivalent. CString不会直接转换为std::string ,但它是一个粗略的等价物。

BYTE is indeed unsigned char and DWORD is unsigned int . BYTE确实是unsigned charDWORDunsigned int WORD is unsigned short . WORD没有unsigned short

You should definitely use typedef actual_type WINDOWS_NAME; 你绝对应该使用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. 我会添加一个名为“wintypes.h”的新头文件,并且包含使用“windows.h”的地方。

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). 编辑注释:使用CString ,它实际上取决于它的使用方式(以及代码是否使用MS调用“Unicode”或“ASCII”字符串)。 I would be tempted to create a class CString and then use std::string inside that. 我很想创建一个类CString ,然后在其中使用std::string 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. 其中大部分都可以通过简单地调用等效的std::string函数来完成,但是某些函数可能需要更多的编程 - 再次,它确实取决于实际使用CString成员函数。

For LP<type> , that is just a pointer to the <type> , so typedef BYTE* LPBYTE; 对于LP<type> ,这只是指向<type>的指针,所以typedef BYTE* LPBYTE; and typedef DWORD* LPDWORD; typedef DWORD* LPDWORD; will do that. 会这样做的。

typedef unsigned long DWORD;
typedef unsigned char BYTE;

CString -> maybe basic_string<TCHAR> ?

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++). 我建议对于DWORD和BYTE使用uint32_t和uint8_t,对于字符串使用普通的char *或const char *(或者对于C ++使用std:string类)。

Probably best thought is to use typedefs for existing code: 可能最好的想法是对现有代码使用typedef:

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. 如果你重写代码使用char,int,long是有用的,而(u)intX_t类型,你需要一个定义的大小。

  • DWORD = uint32_t DWORD = uint32_t
  • BYTE = uint8_t BYTE = uint8_t

These types are not OS specifics and were added to C++11. 这些类型不是OS特定的,并且已添加到C ++ 11中。 You need to include <cstdint> to get them. 你需要include <cstdint>来获取它们。 If you have an old compiler you could use boost/cstdint, which is header only. 如果你有一个旧的编译器,你可以使用boost / cstdint,它只是标题。

Use std::string instead CString , but you will need to change some code. 使用std::string而不是CString ,但您需要更改一些代码。

With these changes your code should compile on both Windows and Linux. 通过这些更改,您的代码应该在Windows和Linux上进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM