home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / ioctls.cpp11 < prev    next >
Encoding:
Text File  |  2005-06-24  |  10.5 KB  |  280 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\Service
  4.  * File Name: ioctls.cpp
  5.  * Purpose  : Defines the ioctls to be used by the Service
  6.  */
  7.  
  8. #include "VirtualWiFiservice.h"
  9.  
  10. LPCTSTR DriverName = TEXT("\\\\.\\VWiFi");
  11.  
  12. /*
  13. VOID Usage(char *binName)
  14. {
  15.     printf("Usage:  %s <adapter>\n", binName);
  16.     printf("V. IMP! DO NOT SWITCH THIS SERVICE OFF BEFORE UNINSTALLING VirtualWiFi\n"); 
  17.     exit(1);
  18. }
  19. */
  20.  
  21. ULONG ioctlSet(DWORD dwIOControlCode,
  22.        LPVOID lpInBuffer,
  23.        DWORD lpInBufferSize)
  24. {
  25.  
  26.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  27.     BOOL b;
  28.     ULONG   bytesreturned;
  29.     UCHAR    testInput[10];
  30.     //ULONG   testLong;
  31.     char lpOutString[50];
  32.  
  33.     //RtlCopyMemory(testInput, lpInBuffer, lpInBufferSize);
  34.     //printf("The buffer is %s, buffer size is %d, string length %d\n", testInput, lpInBufferSize, strlen(testInput)); 
  35.     // testLong = atol(testInput);
  36.     //printf("The long input is %ld\n", testLong);
  37.  
  38.     //printf("Trying to open %s\n", DriverName);
  39.     
  40.     hDriver = CreateFile(DriverName,
  41.                          GENERIC_READ | GENERIC_WRITE, 
  42.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  43.                          0,                     // Default security
  44.                          OPEN_EXISTING,
  45.                          0,  
  46.                          0);                    // No template
  47.     if(hDriver == INVALID_HANDLE_VALUE) {
  48.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  49.         OutputDebugString(TEXT(lpOutString));
  50.     // Otherwise, print success and close the driver
  51.     } else {
  52.         //sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  53.         //OutputDebugString(TEXT(lpOutString));
  54.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  55.                     dwIOControlCode, // control code of operation to perform
  56.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  57.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  58.                     NULL,            //lpOutBuffer, pointer to buffer to receive output data
  59.                     0,               //nOutBufferSize, in bytes, of output buffer
  60.                     &bytesreturned,  // pointer to variable to receive byte count
  61.                     NULL         // pointer to structure for asynchronous 
  62.                     );
  63.         //sprintf(lpOutString, "IOCTL performed\n");
  64.         //OutputDebugString(TEXT(lpOutString));
  65.         if (!b) {
  66.             sprintf(lpOutString, "IOCTL FAILED!\n", GetLastError());
  67.             OutputDebugString(TEXT(lpOutString));
  68.         } 
  69.     }
  70.     CloseHandle(hDriver); // Close the driver
  71.     return 0;
  72. }
  73.  
  74. VOID ioctlGet(DWORD dwIOControlCode,
  75.        LPVOID lpOutBuffer,
  76.        DWORD lpOutBufferSize)
  77. {
  78.  
  79.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  80.     BOOL b;
  81.     ULONG   bytesreturned;
  82.     char lpOutString[50];
  83.  
  84.     sprintf(lpOutString, "Trying to open %s\n", DriverName);
  85.     
  86.     hDriver = CreateFile(DriverName,
  87.                          GENERIC_READ | GENERIC_WRITE, 
  88.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  89.                          0,                     // Default security
  90.                          OPEN_EXISTING,
  91.                          0,  
  92.                          0);                    // No template
  93.     if(hDriver == INVALID_HANDLE_VALUE) {
  94.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  95.         OutputDebugString(TEXT(lpOutString));
  96.     // Otherwise, print success and close the driver
  97.     } else {
  98.         //sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  99.         //OutputDebugString(TEXT(lpOutString));
  100.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  101.                     dwIOControlCode, // control code of operation to perform
  102.                     NULL,            //lpInBuffer, pointer to buffer to supply input data
  103.                     0,               //nInBufferSize, in bytes, of input buffer
  104.                     lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  105.                     lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  106.                     &bytesreturned,  // pointer to variable to receive byte count
  107.                     NULL         // pointer to structure for asynchronous 
  108.                     );
  109.         sprintf(lpOutString, "IOCTL performed\n");
  110.         OutputDebugString(TEXT(lpOutString));
  111.         if (!b) {
  112.             sprintf(lpOutString, "IOCTL FAILED!\n", GetLastError());
  113.             OutputDebugString(TEXT(lpOutString));
  114.         } 
  115.         else {
  116.             //sprintf(lpOutString, "IOCTL succeeded! lpoutBuffer value is %s, bytes received %d\n", lpOutBuffer, bytesreturned);
  117.             //OutputDebugString(TEXT(lpOutString));
  118.         }
  119.         CloseHandle(hDriver); // Close the driver
  120.     }
  121. }
  122.  
  123. ULONG ioctlGetFromAdapter(DWORD dwIOControlCode,
  124.        LPVOID lpInBuffer,
  125.        DWORD  lpInBufferSize,
  126.        LPVOID lpOutBuffer,
  127.        DWORD  lpOutBufferSize)
  128. {
  129.  
  130.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  131.     BOOL b;
  132.     ULONG   bytesreturned;
  133.     ULONG output;
  134.     char lpOutString[50];
  135.  
  136.     sprintf(lpOutString, "Trying to open %s\n", DriverName);
  137.  
  138.     hDriver = CreateFile(DriverName,
  139.                          GENERIC_READ | GENERIC_WRITE, 
  140.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  141.                          0,                     // Default security
  142.                          OPEN_EXISTING,
  143.                          0,  
  144.                          0);                    // No template
  145.     if(hDriver == INVALID_HANDLE_VALUE) {
  146.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  147.         OutputDebugString(TEXT(lpOutString));
  148.     // Otherwise, print success and close the driver
  149.     } else {
  150.         //sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  151.         //OutputDebugString(TEXT(lpOutString));
  152.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  153.                     dwIOControlCode, // control code of operation to perform
  154.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  155.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  156.                     lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  157.                     lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  158.                     &bytesreturned,  // pointer to variable to receive byte count
  159.                     NULL         // pointer to structure for asynchronous 
  160.                     );
  161.         //sprintf(lpOutString, "IOCTL performed\n");
  162.         //OutputDebugString(TEXT(lpOutString));
  163.         if (!b) {
  164.             sprintf(lpOutString, "IOCTL FAILED! %ld\n", GetLastError());
  165.             OutputDebugString(TEXT(lpOutString));
  166.             CloseHandle(hDriver); // Close the driver
  167.         } 
  168.         else {
  169.             sscanf((char *)lpOutBuffer, "%ld", &output);
  170.             //sprintf(lpOutString, "IOCTL succeeded! lpoutBuffer value is %d, bytes received %d\n", 
  171.             //    output, bytesreturned);
  172.             //OutputDebugString(TEXT(lpOutString));
  173.             CloseHandle(hDriver); // Close the driver
  174.             return output;
  175.         }
  176.     }
  177.     return 0;
  178. }
  179.  
  180. ULONG ioctlGetULongFromAdapter(DWORD dwIOControlCode,
  181.        LPVOID lpInBuffer,
  182.        DWORD  lpInBufferSize)
  183. {
  184.  
  185.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  186.     BOOL b;
  187.     ULONG   bytesreturned;
  188.     ULONG   recvdULong;
  189.     char lpOutString[50];
  190.  
  191.     sprintf(lpOutString, "Trying to open %s\n", DriverName);
  192.  
  193.     hDriver = CreateFile(DriverName,
  194.                          GENERIC_READ | GENERIC_WRITE, 
  195.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  196.                          0,                     // Default security
  197.                          OPEN_EXISTING,
  198.                          0,  
  199.                          0);                    // No template
  200.     if(hDriver == INVALID_HANDLE_VALUE) {
  201.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  202.         OutputDebugString(TEXT(lpOutString));
  203.     // Otherwise, print success and close the driver
  204.     } else {
  205.         sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  206.         OutputDebugString(TEXT(lpOutString));
  207.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  208.                     dwIOControlCode, // control code of operation to perform
  209.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  210.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  211.                     //lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  212.                     //lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  213.                     (LPVOID)&recvdULong,
  214.                     sizeof(ULONG),
  215.                     &bytesreturned,  // pointer to variable to receive byte count
  216.                     NULL         // pointer to structure for asynchronous 
  217.                     );
  218.         sprintf(lpOutString, "IOCTL performed\n");
  219.         OutputDebugString(TEXT(lpOutString));
  220.         if (!b) {
  221.             sprintf(lpOutString, "IOCTL FAILED! %s\n", GetLastError());
  222.             OutputDebugString(TEXT(lpOutString));
  223.             CloseHandle(hDriver); // Close the driver
  224.         } 
  225.         else {
  226.             sprintf(lpOutString, "IOCTL succeeded! long value is %ld, bytes received %d\n", 
  227.                 recvdULong, bytesreturned);
  228.             OutputDebugString(TEXT(lpOutString));
  229.             CloseHandle(hDriver); // Close the driver
  230.         }
  231.     }
  232.     return recvdULong;
  233. }
  234.  
  235. ULONG ioctlGetSSIDFromAdapter(DWORD dwIOControlCode,
  236.        LPVOID lpInBuffer,
  237.        DWORD  lpInBufferSize,
  238.        LPVOID lpOutBuffer,
  239.        DWORD  lpOutBufferSize)
  240. {
  241.  
  242.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  243.     BOOL b;
  244.     ULONG   bytesreturned;
  245.  
  246.     //printf("Trying to open %s\n", DriverName);
  247.     
  248.     hDriver = CreateFile(DriverName,
  249.                          GENERIC_READ | GENERIC_WRITE, 
  250.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  251.                          0,                     // Default security
  252.                          OPEN_EXISTING,
  253.                          0,  
  254.                          0);                    // No template
  255.     if(hDriver == INVALID_HANDLE_VALUE) {
  256.             printf("VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  257.     // Otherwise, print success and close the driver
  258.     } else {
  259.         //printf("VWiFi.sys - CreateFile Success.\n");
  260.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  261.                     dwIOControlCode, // control code of operation to perform
  262.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  263.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  264.                     lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  265.                     lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  266.                     &bytesreturned,  // pointer to variable to receive byte count
  267.                     NULL         // pointer to structure for asynchronous 
  268.                     );
  269.         //printf("IOCTL performed\n");
  270.         if (!b) {
  271.             printf("IOCTL FAILED!\n", GetLastError());
  272.             CloseHandle(hDriver); // Close the driver
  273.         } 
  274.         else {
  275.         }
  276.     }
  277.     return bytesreturned;
  278. }
  279.  
  280.