同一套代码,用VisualStudio2022和QT同步开发,互相通信时,因为用的是同一套结构体,要注意两边的数据类型的大小是否一致,所以要做数据类型大小的判断,如果大小不同,则视作非法数据,要停止运行。
在相同项目下VisualStudio的数据类型所占字节大小输出:
int c = sizeof(OSVERSIONINFOEX);//156
char OsVerInfoEx[sizeof(OSVERSIONINFOEX)];// 版本信息
在相同项目下qt中数据类型所占字节大小输出:所以要和VisualStudio通信就只能用OSVERSIONINFOEXA来限定char的大小,两边的数据才会一致。
int c = sizeof(OSVERSIONINFOEX);//284
int c1 = sizeof(OSVERSIONINFOEXA);//156//
int c2 = sizeof(OSVERSIONINFOEXW);//284//UNICODE编码
char OsVerInfoEx[sizeof(OSVERSIONINFOEXA)];// 版本信息
查看源码得知,在VisualStudio中对编码类型做了判断,所以使用OSVERSIONINFOEX时,会指向OSVERSIONINFOEXA,所以占用156个字节。
typedef struct _OSVERSIONINFOEXA {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
CHAR szCSDVersion[ 128 ]; // Maintenance string for PSS usage
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wSuiteMask;
BYTE wProductType;
BYTE wReserved;
} OSVERSIONINFOEXA, *POSVERSIONINFOEXA, *LPOSVERSIONINFOEXA;
typedef struct _OSVERSIONINFOEXW {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
WCHAR szCSDVersion[ 128 ]; // Maintenance string for PSS usage
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wSuiteMask;
BYTE wProductType;
BYTE wReserved;
} OSVERSIONINFOEXW, *POSVERSIONINFOEXW, *LPOSVERSIONINFOEXW, RTL_OSVERSIONINFOEXW, *PRTL_OSVERSIONINFOEXW;
#ifdef UNICODE
typedef OSVERSIONINFOEXW OSVERSIONINFOEX;//如果是UNICODE编码,就指向OSVERSIONINFOEXW
typedef POSVERSIONINFOEXW POSVERSIONINFOEX;
typedef LPOSVERSIONINFOEXW LPOSVERSIONINFOEX;
#else
typedef OSVERSIONINFOEXA OSVERSIONINFOEX;//否则就指向OSVERSIONINFOEXA
typedef POSVERSIONINFOEXA POSVERSIONINFOEX;
typedef LPOSVERSIONINFOEXA LPOSVERSIONINFOEX;
#endif // UNICODE