[vbox-dev] Auto start VM as Windows service

Dimitar Pashev mitko at banksoft-bg.com
Wed Nov 26 15:05:14 GMT 2008


I wrote simple service appication that start all machines /via
VBoxHeadless.exe/ which have !EnableAutoStart string in description

ProjectFiles attached below.

VMStart() and VMStop()

//
// Purpose:
//   Start all machines which have !EnableAutoStart sting in description
//
// Parameters:
//   None
//
// Return value:
//   0: Error, 1:Success
//
int VMStart()
{
    wchar_t szExePath[_MAX_PATH];//VBoxHeadless path
    CComBSTR    bstrHomePath;
    HRESULT hr;
    unsigned long i,max_i;

    CComPtr<IVirtualBox> vbox;
    CComPtr<IMachineCollection> machines;

    //Get Virtualbox install dir
    CRegKey key;
    if( ERROR_SUCCESS !=
key.Open(HKEY_LOCAL_MACHINE,L"SOFTWARE\\Sun\\xVM VirtualBox",KEY_READ )
) return 0; // on error exit
    ULONG ulen = MAX_PATH-1;
    if( ERROR_SUCCESS !=
key.QueryStringValue(L"InstallDir",szExePath,&ulen) ) return 0;
    key.Close();

    wcsncat(szExePath,L"VBoxHeadless.exe",_MAX_PATH);

    hr = vbox.CoCreateInstance(CLSID_VirtualBox);
    if(S_OK != hr) return 0;
    hr = vbox->get_Machines(&machines);
    if(S_OK == hr) hr = machines->get_Count(&max_i);
    if(S_OK != hr) return 0;

    for( i=0;i<max_i;i++) {
        CComPtr<IMachine> machine;
        hr = machines->GetItemAt(i,&machine);
        if(S_OK != hr) continue;
        CComBSTR temp;
        hr = machine->get_Description(&temp);
        if( hr != S_OK ||NULL == temp || NULL ==
wcsstr(temp,L"!EnableAutoStart") ) continue;

        CComBSTR name;

        //construct command line
        hr = machine->get_Name(&name);
        temp = TEXT(" -v config -s ");
        temp += name;

        //create process
        STARTUPINFO si;
        memset(&si,0,sizeof(si));
        si.cb = sizeof(si);
        PROCESS_INFORMATION pi;
        int ret = CreateProcess(szExePath,temp,0,0,0,
            CREATE_NEW_PROCESS_GROUP|DETACHED_PROCESS,
            0,0,&si,&pi);
        ret = GetLastError();
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);


    }
    return 1;
}
//
// Purpose:
//   Stop all running VM's which have !EnableAutoStart sting in description
//
// Parameters:
//   None
//
// Return value:
//   0: Error, 1:Success
//
int VMStop()
{
    CComPtr<IVirtualBox> vbox;
    CComPtr<IMachineCollection> machines;
    HRESULT hr;
    unsigned long i,max_i;

    hr = vbox.CoCreateInstance(CLSID_VirtualBox);
    if(S_OK != hr) return 0;
    hr = vbox->get_Machines(&machines);
    if(S_OK == hr) hr = machines->get_Count(&max_i);
    if(S_OK != hr) return 0;
    for( i=0;i<max_i;i++) {
        CComPtr<IMachine>    machine;
        MachineState        vmstate;

        hr = machines->GetItemAt(i,&machine);
        if(S_OK != hr) continue;


        //check for autostart flag
        CComBSTR temp;
        hr = machine->get_Description(&temp);
        if( hr != S_OK ||NULL == temp || NULL ==
wcsstr(temp,L"!EnableAutoStart") ) continue;

        //check for running state
        machine->get_State(&vmstate);
        if( MachineState_Running == vmstate || MachineState_Starting ==
vmstate ) {
            GUID vmID;
            CComPtr<ISession>   sess;
            CComPtr<IConsole>   console;
            hr    = machine->get_Id(&vmID);
            hr  = sess.CoCreateInstance(CLSID_Session);
            if(S_OK == hr ) hr = vbox->OpenExistingSession(sess,vmID);
            if(S_OK == hr ) hr = sess->get_Console(&console);
            if(S_OK == hr ) hr = console->PowerButton();
            if(S_OK == hr ) hr = sess->Close();
        }
    }
    return 1;
}
------------------------------------
Usage
install service: VBoxAutoStartSvc -install <user> <password>
remove  service: VBoxAutoStartSvc -remove

I hope this will be useful.



-------------- next part --------------
A non-text attachment was scrubbed...
Name: VBoxAutoStartSvc.zip
Type: application/x-zip-compressed
Size: 25368 bytes
Desc: not available
URL: <http://www.virtualbox.org/pipermail/vbox-dev/attachments/20081126/c23d028e/attachment.bin>


More information about the vbox-dev mailing list