home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / ioctls.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  11.1 KB  |  297 lines

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