VirtualBox

Changeset 96459 in vbox


Ignore:
Timestamp:
Aug 24, 2022 10:02:55 PM (2 years ago)
Author:
vboxsync
Message:

Add/VBoxGuest: Made VBoxGuestInst.cpp build in no-CRT mode. Cleaned up code. bugref:10261

Location:
trunk/src/VBox/Additions/common/VBoxGuest
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuest/Makefile.kmk

    r96453 r96459  
    269269  VBoxGuestInstNT_LDFLAGS := -Include:_vcc100_kernel32_fakes_asm # Temporary kludge to deal with some link order issue.
    270270 endif
     271 VBoxGuestInstNT_INCS     = ../../WINNT/include
    271272 VBoxGuestInstNT_SOURCES  = win/VBoxGuestInst.cpp
    272273 VBoxGuestInstNT_USES     = vboximportchecker
  • trunk/src/VBox/Additions/common/VBoxGuest/win/VBoxGuestInst.cpp

    r96407 r96459  
    11/* $Id$ */
    22/** @file
    3  * Small tool to (un)install the VBoxGuest device driver.
     3 * Small tool to (un)install the VBoxGuest device driver (for testing).
    44 */
    55
     
    4141#include <iprt/win/windows.h>
    4242
    43 #include <stdio.h>
    44 #include <stdlib.h>
    45 #include <string.h>
    46 
    4743#include <VBox/VBoxGuest.h> /* for VBOXGUEST_SERVICE_NAME */
    4844#include <iprt/err.h>
    49 
    50 
    51 //#define TESTMODE
    52 
    53 
    54 
    55 static int installDriver(bool fStartIt)
     45#include <iprt/message.h>
     46#include <iprt/stream.h>
     47#include <iprt/utf16.h>
     48
     49
     50
     51
     52static RTEXITCODE installDriver(bool fStartIt)
    5653{
    5754    /*
    5855     * Assume it didn't exist, so we'll create the service.
    5956     */
    60     SC_HANDLE   hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
     57    SC_HANDLE   hSMgrCreate = OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG);
    6158    if (!hSMgrCreate)
    62     {
    63         printf("OpenSCManager(,,create) failed rc=%lu\n", GetLastError());
    64         return -1;
    65     }
    66 
    67     const char    *pszSlashName = "\\VBoxGuest.sys";
    68     char szDriver[MAX_PATH * 2];
    69     GetCurrentDirectory(MAX_PATH, szDriver);
    70     strcat(szDriver, pszSlashName);
    71     if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
    72     {
    73         GetSystemDirectory(szDriver, sizeof(szDriver));
    74         strcat(strcat(szDriver, "\\drivers"), pszSlashName);
     59        return RTMsgErrorExitFailure("OpenSCManager(,,create) failed: %u", GetLastError());
     60
     61    const wchar_t  *pwszSlashName = L"\\VBoxGuest.sys";
     62    wchar_t         wszDriver[MAX_PATH * 2];
     63    GetCurrentDirectoryW(MAX_PATH, wszDriver);
     64    RTUtf16Cat(wszDriver, RT_ELEMENTS(wszDriver), pwszSlashName);
     65    if (GetFileAttributesW(wszDriver) == INVALID_FILE_ATTRIBUTES)
     66    {
     67        GetSystemDirectoryW(wszDriver, MAX_PATH);
     68        RTUtf16Cat(wszDriver, RT_ELEMENTS(wszDriver), L"\\drivers");
     69        RTUtf16Cat(wszDriver, RT_ELEMENTS(wszDriver), pwszSlashName);
    7570
    7671        /* Try FAT name abbreviation. */
    77         if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
    78         {
    79             pszSlashName = "\\VBoxGst.sys";
    80             GetCurrentDirectory(MAX_PATH, szDriver);
    81             strcat(szDriver, pszSlashName);
    82             if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
     72        if (GetFileAttributesW(wszDriver) == INVALID_FILE_ATTRIBUTES)
     73        {
     74            pwszSlashName = L"\\VBoxGst.sys";
     75            GetCurrentDirectoryW(MAX_PATH, wszDriver);
     76            RTUtf16Cat(wszDriver, RT_ELEMENTS(wszDriver), pwszSlashName);
     77            if (GetFileAttributesW(wszDriver) == INVALID_FILE_ATTRIBUTES)
    8378            {
    84                 GetSystemDirectory(szDriver, sizeof(szDriver));
    85                 strcat(strcat(szDriver, "\\drivers"), pszSlashName);
    86 
     79                GetSystemDirectoryW(wszDriver, MAX_PATH);
     80                RTUtf16Cat(wszDriver, RT_ELEMENTS(wszDriver), L"\\drivers");
     81                RTUtf16Cat(wszDriver, RT_ELEMENTS(wszDriver), pwszSlashName);
    8782            }
    8883        }
    8984    }
    9085
    91     SC_HANDLE hService = CreateService(hSMgrCreate,
    92                                        VBOXGUEST_SERVICE_NAME,
    93                                        "VBoxGuest Support Driver",
    94                                        SERVICE_QUERY_STATUS | (fStartIt ? SERVICE_START : 0),
    95                                        SERVICE_KERNEL_DRIVER,
    96                                        SERVICE_BOOT_START,
    97                                        SERVICE_ERROR_NORMAL,
    98                                        szDriver,
    99                                        "System",
    100                                        NULL, NULL, NULL, NULL);
     86    RTEXITCODE rcExit;
     87    SC_HANDLE hService = CreateServiceW(hSMgrCreate,
     88                                        RT_CONCAT(L,VBOXGUEST_SERVICE_NAME),
     89                                        L"VBoxGuest Support Driver",
     90                                        SERVICE_QUERY_STATUS | (fStartIt ? SERVICE_START : 0),
     91                                        SERVICE_KERNEL_DRIVER,
     92                                        SERVICE_BOOT_START,
     93                                        SERVICE_ERROR_NORMAL,
     94                                        wszDriver,
     95                                        L"System",
     96                                        NULL, NULL, NULL, NULL);
    10197    if (hService)
    10298    {
    103         printf("Successfully created service '%s' for driver '%s'.\n", VBOXGUEST_SERVICE_NAME, szDriver);
     99        RTMsgInfo("Successfully created service '%s' for driver '%ls'.\n", VBOXGUEST_SERVICE_NAME, wszDriver);
     100        rcExit = RTEXITCODE_SUCCESS;
    104101        if (fStartIt)
    105102        {
    106103            if (StartService(hService, 0, NULL))
    107                 printf("successfully started driver '%s'\n", szDriver);
     104                RTMsgInfo("successfully started driver '%ls'\n", wszDriver);
    108105            else
    109                 printf("StartService failed: %lu\n", GetLastError());
     106                rcExit = RTMsgErrorExitFailure("StartService failed: %u", GetLastError());
    110107        }
    111108        CloseServiceHandle(hService);
    112109    }
    113110    else
    114         printf("CreateService failed! lasterr=%lu (szDriver=%s)\n", GetLastError(), szDriver);
     111        rcExit = RTMsgErrorExitFailure("CreateService failed! %u (wszDriver=%ls)\n", GetLastError(), wszDriver);
    115112    CloseServiceHandle(hSMgrCreate);
    116     return hService ? 0 : -1;
    117 }
    118 
    119 static int uninstallDriver(void)
    120 {
    121     int rc = -1;
    122     SC_HANDLE   hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
     113    return rcExit;
     114}
     115
     116
     117static RTEXITCODE uninstallDriver(void)
     118{
     119    SC_HANDLE hSMgr = OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG);
    123120    if (!hSMgr)
    124     {
    125         printf("OpenSCManager(,,delete) failed rc=%lu\n", GetLastError());
    126         return -1;
    127     }
    128     SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE);
     121        return RTMsgErrorExitFailure("OpenSCManager(,,change_config) failed: %u", GetLastError());
     122
     123    RTEXITCODE rcExit;
     124    SC_HANDLE hService = OpenServiceW(hSMgr, RT_CONCAT(L, VBOXGUEST_SERVICE_NAME), SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE);
    129125    if (hService)
    130126    {
     
    135131        QueryServiceStatus(hService, &Status);
    136132        if (Status.dwCurrentState == SERVICE_STOPPED)
    137             rc = VINF_SUCCESS;
     133            rcExit = RTEXITCODE_SUCCESS;
    138134        else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
    139135        {
     
    145141            }
    146142            if (Status.dwCurrentState == SERVICE_STOPPED)
    147                 rc = VINF_SUCCESS;
     143                rcExit = RTEXITCODE_SUCCESS;
    148144            else
    149             {
    150                 printf("Failed to stop service. status=%ld (%#lx)\n", Status.dwCurrentState, Status.dwCurrentState);
    151                 rc = VERR_GENERAL_FAILURE;
    152             }
     145                rcExit = RTMsgErrorExitFailure("Failed to stop service! Service status: %u (%#x)\n",
     146                                                Status.dwCurrentState, Status.dwCurrentState);
    153147        }
    154148        else
    155         {
    156             DWORD dwErr = GetLastError();
    157             if (   Status.dwCurrentState == SERVICE_STOP_PENDING
    158                 && dwErr == ERROR_SERVICE_CANNOT_ACCEPT_CTRL)
    159                 rc = VERR_RESOURCE_BUSY;    /* better than VERR_GENERAL_FAILURE */
    160             else
    161             {
    162                 printf("ControlService failed with dwErr=%ld. status=%lu (%#lx)\n",
    163                        dwErr, Status.dwCurrentState, Status.dwCurrentState);
    164                 rc = -1;
    165             }
    166         }
     149            rcExit = RTMsgErrorExitFailure("ControlService failed: %u, Service status: %u (%#x)",
     150                                            GetLastError(), Status.dwCurrentState, Status.dwCurrentState);
    167151
    168152        /*
    169153         * Delete the service.
    170154         */
    171         if (RT_SUCCESS(rc))
     155        if (rcExit == RTEXITCODE_SUCCESS)
    172156        {
    173157            if (DeleteService(hService))
    174                 rc = 0;
     158                RTMsgInfo("Successfully deleted the %s service\n", VBOXGUEST_SERVICE_NAME);
    175159            else
    176             {
    177                 printf("DeleteService failed lasterr=%lu\n", GetLastError());
    178                 rc = -1;
    179             }
    180         }
     160                rcExit = RTMsgErrorExitFailure("DeleteService failed: %u", GetLastError());
     161        }
     162
    181163        CloseServiceHandle(hService);
    182164    }
    183165    else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
    184         rc = 0;
     166    {
     167        RTMsgInfo("Nothing to do, the service %s does not exist.\n", VBOXGUEST_SERVICE_NAME);
     168        rcExit = RTEXITCODE_SUCCESS;
     169    }
    185170    else
    186         printf("OpenService failed lasterr=%lu\n", GetLastError());
     171        rcExit = RTMsgErrorExitFailure("OpenService failed: %u", GetLastError());
     172
    187173    CloseServiceHandle(hSMgr);
    188     return rc;
    189 }
    190 
    191 #ifdef TESTMODE
    192 
    193 static HANDLE openDriver(void)
    194 {
    195     HANDLE hDevice;
    196 
    197     hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
    198                          GENERIC_READ | GENERIC_WRITE,
    199                          FILE_SHARE_READ | FILE_SHARE_WRITE,
    200                          NULL,
    201                          OPEN_EXISTING,
    202                          FILE_ATTRIBUTE_NORMAL,
    203                          NULL);
    204     if (hDevice == INVALID_HANDLE_VALUE)
    205     {
    206         printf("CreateFile did not work. GetLastError() %lu\n", GetLastError());
    207     }
    208     return hDevice;
    209 }
    210 
    211 static int closeDriver(HANDLE hDevice)
    212 {
    213     CloseHandle(hDevice);
    214     return 0;
    215 }
    216 
    217 static int performTest(void)
    218 {
    219     int rc = 0;
    220 
    221     HANDLE hDevice = openDriver();
    222 
     174    return rcExit;
     175}
     176
     177
     178static RTEXITCODE performTest(void)
     179{
     180    HANDLE hDevice = CreateFileW(RT_CONCAT(L,VBOXGUEST_DEVICE_NAME), // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
     181                                 GENERIC_READ | GENERIC_WRITE,
     182                                 FILE_SHARE_READ | FILE_SHARE_WRITE,
     183                                 NULL,
     184                                 OPEN_EXISTING,
     185                                 FILE_ATTRIBUTE_NORMAL,
     186                                 NULL);
    223187    if (hDevice != INVALID_HANDLE_VALUE)
    224         closeDriver(hDevice);
     188    {
     189        CloseHandle(hDevice);
     190        RTMsgInfo("Test succeeded\n");
     191        return RTEXITCODE_SUCCESS;
     192    }
     193    return RTMsgErrorExitFailure("Test failed! Unable to open driver (CreateFileW -> %u).", GetLastError());
     194}
     195
     196
     197static RTEXITCODE usage(const char *pszProgName)
     198{
     199    RTPrintf("\n"
     200             "Usage: %s [install|uninstall|test]\n", pszProgName);
     201    return RTEXITCODE_SYNTAX;
     202}
     203
     204
     205int main(int argc, char **argv)
     206{
     207    if (argc != 2)
     208    {
     209        RTMsgError(argc < 2 ? "Too few arguments! Expected one." : "Too many arguments! Expected only one.");
     210        return usage(argv[0]);
     211    }
     212
     213    RTEXITCODE rcExit;
     214    if (strcmp(argv[1], "install") == 0)
     215        rcExit = installDriver(true);
     216    else if (strcmp(argv[1], "uninstall") == 0)
     217        rcExit = uninstallDriver();
     218    else if (strcmp(argv[1], "test") == 0)
     219        rcExit = performTest();
    225220    else
    226         printf("openDriver failed!\n");
    227 
    228     return rc;
    229 }
    230 
    231 #endif /* TESTMODE */
    232 
    233 static int usage(char *programName)
    234 {
    235     printf("error, syntax: %s [install|uninstall]\n", programName);
    236     return 1;
    237 }
    238 
    239 int main(int argc, char **argv)
    240 {
    241     bool installMode;
    242 #ifdef TESTMODE
    243     bool testMode = false;
    244 #endif
    245 
    246     if (argc != 2)
    247         return usage(argv[0]);
    248 
    249     if (strcmp(argv[1], "install") == 0)
    250         installMode = true;
    251     else if (strcmp(argv[1], "uninstall") == 0)
    252         installMode = false;
    253 #ifdef TESTMODE
    254     else if (strcmp(argv[1], "test") == 0)
    255         testMode = true;
    256 #endif
    257     else
    258         return usage(argv[0]);
    259 
    260 
    261     int rc;
    262 #ifdef TESTMODE
    263     if (testMode)
    264         rc = performTest();
    265     else
    266 #endif
    267     if (installMode)
    268         rc = installDriver(true);
    269     else
    270         rc = uninstallDriver();
    271 
    272     if (rc == 0)
    273         printf("operation completed successfully!\n");
    274     else
    275         printf("error: operation failed with status code %d\n", rc);
    276 
    277     return rc;
    278 }
    279 
     221    {
     222        RTMsgError("Unknown argument: '%s'", argv[1]);
     223        rcExit = usage(argv[0]);
     224    }
     225    return rcExit;
     226}
     227
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette