[vbox-dev] vbox-dev Digest, Vol 25, Issue 14

Mathias Herrmann mherrmann at assetive.com
Wed Nov 26 15:41:58 GMT 2008


Hey Dimitar,

I am exited to find another developer experimenting on the issue of running
serviced vms on windows hosts. Maybe you have read about my project
VBoxVmService, which aims in a similar direction but is way more fragmented
than your approach. If you are thinking about any further development of
your code, I believe it would be a great idea to team up our resources
instead of bringing up two parallel tools for the same purpose. 

You can get into contact with me via email or via the virtualbox forums
(forums.virtualbox.org). My username there is "mattz".

Best regards, Mathias


-----Ursprüngliche Nachricht-----
Von: vbox-dev-bounces at virtualbox.org
[mailto:vbox-dev-bounces at virtualbox.org] Im Auftrag von
vbox-dev-request at virtualbox.org
Gesendet: Mittwoch, 26. November 2008 16:05
An: vbox-dev at virtualbox.org
Betreff: vbox-dev Digest, Vol 25, Issue 14

Send vbox-dev mailing list submissions to
	vbox-dev at virtualbox.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://vbox.innotek.de/mailman/listinfo/vbox-dev
or, via email, send a message with subject or body 'help' to
	vbox-dev-request at virtualbox.org

You can reach the person managing the list at
	vbox-dev-owner at virtualbox.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of vbox-dev digest..."


Today's Topics:

   1. Auto start VM as Windows service (Dimitar Pashev)


----------------------------------------------------------------------

Message: 1
Date: Wed, 26 Nov 2008 17:05:14 +0200
From: Dimitar Pashev <mitko at banksoft-bg.com>
Subject: [vbox-dev] Auto start VM as Windows service
To: vbox-dev at virtualbox.org
Message-ID: <492D65AA.1050807 at banksoft-bg.com>
Content-Type: text/plain; charset="windows-1251"

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://vbox.innotek.de/pipermail/vbox-dev/attachments/20081126/c23d028e/atta
chment.bin 

------------------------------

_______________________________________________
vbox-dev mailing list
vbox-dev at virtualbox.org
http://vbox.innotek.de/mailman/listinfo/vbox-dev


End of vbox-dev Digest, Vol 25, Issue 14
****************************************





More information about the vbox-dev mailing list