NO

Author Topic: problem of winsock2.h  (Read 2460 times)

liut

  • Guest
problem of winsock2.h
« on: August 24, 2015, 08:41:26 AM »
Hi I just found an issue in winsock2.h (7.0 32-bit). in this header file, "pshpack4.h" is included, and it causes DeviceIoControl() failure. The following code can run for test. You can comment the 1st line to see the different results. If using winsock.h, no problem too.
I guess it's for some historical or compatibility reason and "#pragma pack(push,4)" must be used. Can anybody let me know more?

#include <winsock2.h>
//#include <winsock.h>
//#pragma pack(push,8)

#include <stdio.h>
#include <windows.h>

int main(int argc,char *argv[])
{
   typedef struct _ABC{
      DWORD a;
      LARGE_INTEGER b;
   }ABC;

   ABC abc;

   printf("sizeof(abc)=%d\n",sizeof(abc));

   DWORD data_ret=0;
   DISK_PERFORMANCE dp;

   printf("sizeof(dp)=%d\n",sizeof(dp));

   HANDLE hdd;

   hdd=CreateFile("\\\\.\\PhysicalDrive0",0,0,NULL,OPEN_EXISTING,0,NULL);
   if(hdd!=INVALID_HANDLE_VALUE){
      if(DeviceIoControl(hdd,IOCTL_DISK_PERFORMANCE,NULL,0,(LPVOID)&dp,sizeof(DISK_PERFORMANCE),&data_ret,NULL)){
         printf("DeviceIoControl() succeed. %ld bytes read.\n",data_ret);
      }
      else{
         printf("DeviceIoControl() failed. Err code=%ld\n",GetLastError());
      }
      CloseHandle(hdd);
   }
   else{
      printf("Invalid hard disk.\n");
   }

   return 0;
}