00001 #include "stdafx.h"
00002 #include "VBoxCmdLine.h"
00003 #include <bzscmn/file.h>
00004 #include <bzscmn/textfile.h>
00005 #include <bzscmn/cmndef.h>
00006
00007 using namespace BazisLib;
00008
00009 #define UNIX_LINE_ENDING_SEQUENCE_A '\n'
00010 #define UNIX_LINE_ENDING_SEQUENCE_LEN 1
00011
00012 typedef _Core::_TextFile<char, UNIX_LINE_ENDING_SEQUENCE_A, UNIX_LINE_ENDING_SEQUENCE_LEN> TextUnixAnsiFileReader;
00013
00014
00015
00016 unsigned VBoxCmdLineToVMNameW( const wchar_t *pCmdLine, wchar_t *pNameBuffer, size_t MaxLength )
00017 {
00018 if (!pCmdLine || !pNameBuffer || !MaxLength)
00019 return 0;
00020 TCHAR tsz[MAX_PATH] = {0,};
00021 GetEnvironmentVariable(_T("USERPROFILE"), tsz, __countof(tsz));
00022 _tcsncat(tsz, _T("\\.VirtualBox\\VirtualBox.xml"), __countof(tsz));
00023 ManagedPointer<TextUnixAnsiFileReader> pReader = new TextUnixAnsiFileReader(new OSFile(tsz, FastFileFlags::OpenReadOnlyShareAll));
00024 if (!pReader->Valid())
00025 return 0;
00026
00027 const wchar_t *pGuid = wcsstr(pCmdLine, L"--startvm ");
00028 if (!pGuid)
00029 return 0;
00030 pGuid += 10;
00031
00032 std::string strGuid = StringToANSIString(pGuid);
00033
00034 do
00035 {
00036 std::string str = pReader->ReadLine();
00037 if (str.empty())
00038 continue;
00039 off_t off = (off_t)str.find("<MachineEntry uuid=\"{");
00040 if (off == -1)
00041 continue;
00042 if (_memicmp(str.c_str() + off + 21, strGuid.c_str(), 36))
00043 continue;
00044 off = (off_t)str.find("\" src=\"", off);
00045 if (off == -1)
00046 continue;
00047
00048 off_t offDot = (off_t)str.find_last_of('.');
00049 if (offDot == -1)
00050 offDot = (off_t)str.length();
00051 off_t offSlash = (off_t)str.find_last_of('\\');
00052 if (offSlash == -1)
00053 offSlash = off + 7;
00054 else
00055 offSlash++;
00056 if (offDot <= offSlash)
00057 offDot = (off_t)str.length();
00058
00059 size_t len = offDot - offSlash;
00060 if (len > MaxLength)
00061 len = MaxLength;
00062 wcsncpy(pNameBuffer, ANSIStringToString(str).c_str() + offSlash, len);
00063 return (unsigned)len;
00064
00065 } while (!pReader->IsEOF());
00066
00067 return 0;
00068 }
00069
00070 unsigned VBoxCmdLineToPipeNameW( const wchar_t *pCmdLine, wchar_t *pNameBuffer, size_t MaxLength, unsigned PID )
00071 {
00072 wchar_t wszVMName[128] = {0,};
00073 if (!VBoxCmdLineToVMNameW(pCmdLine, wszVMName, __countof(wszVMName)))
00074 _snwprintf(wszVMName, __countof(wszVMName), L"VBOX%d", PID ? PID : GetCurrentProcessId());
00075
00076 _snwprintf(pNameBuffer, MaxLength, L"\\\\.\\pipe\\kd_%s", wszVMName);
00077 for (int i = 0; pNameBuffer[i]; i++)
00078 if (pNameBuffer[i] == ' ')
00079 pNameBuffer[i] = '_';
00080 return (unsigned)wcslen(pNameBuffer);
00081 }