Create VM in virtual box with VB SDK - c++

I need to create a VM in virtual box using the API in the VB SDK. I was able to create the machine but creating controller and adding device for the VM is troubling me a lot. Could you please help me out? I have given the code which i am using
CoInitialize(NULL);
IMachine *machine = NULL;
IVirtualBox *virtualBox = NULL;
BSTR guid = NULL;
HRESULT hr;
/* Instantiate the VirtualBox root object. */
hr = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */
NULL, /* no aggregation */
CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
IID_IVirtualBox, /* IID of the interface */
(void**)&virtualBox);
BSTR strMachineName = ::SysAllocString(L"SampleMachine");
IGuestOSType* os = NULL;
BSTR type = ::SysAllocString(L"unknown");
hr = virtualBox->GetGuestOSType(type, &os);
os->get_Id(&guid);
os->Release();
BSTR null = ::SysAllocString(L"00000000-0000-0000-0000-000000000000");
hr = virtualBox->CreateMachine(NULL, strMachineName, guid, null, TRUE, &machine);
::SysFreeString(null);
if (SUCCEEDED(hr))
{
IMedium* medium = NULL;
ISession *session = NULL;
IConsole *console = NULL;
IProgress *progress = NULL;
BSTR sessiontype = SysAllocString(L"gui");
BSTR bstrPath = SysAllocString(L"I:\\VHD\\Local_disk_(C).vhd");
hr = machine->SaveSettings();
hr = virtualBox->RegisterMachine(machine);
machine->Release();
hr = virtualBox->FindMachine(strMachineName, &machine);
hr = virtualBox->OpenMedium(bstrPath, DeviceType_HardDisk, AccessMode_ReadWrite,
TRUE, &medium);
/* Create the session object. */
hr = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */
NULL, /* no aggregation */
CLSCTX_INPROC_SERVER, /* the object lives in a server process on this machine */
IID_ISession, /* IID of the interface */
(void**)&session);
hr = machine->LockMachine(session, LockType_Write);
IStorageController* cont = NULL;
BSTR loc = ::SysAllocString(L"BusLogic");
hr = machine->AddStorageController(loc, StorageBus_SCSI, &cont);
hr = machine->AttachDevice(loc, 0, 0, DeviceType_HardDisk, medium);
/* Start a VM session using the delivered VBox GUI. */
hr = machine->LaunchVMProcess(session, sessiontype,
NULL, &progress);
/* Wait until VM is running. */
hr = progress->WaitForCompletion (-1);
/* Get console object. */
session->get_Console(&console);
/* Bring console window to front. */
machine->ShowConsoleWindow(0);
Code fails at hr = machine->AddStorageController(loc, StorageBus_SCSI, &cont);. What I am doing wrong?

You can add a AddStorageController to a mutable machine, get machine object from session.machine and do the addition on the mutablemachine
hr = machine->LockMachine(session, LockType_Write);
IStorageController* cont = NULL;
BSTR loc = ::SysAllocString(L"BusLogic");
hr = machine->AddStorageController(loc, StorageBus_SCSI, &cont);
Change to following
hr = machine->LockMachine(session, LockType_Write);
Imachine *mutableMachine = NULL;
hr= session->get_Machine(&mutableMachine );
IStorageController* cont = NULL;
BSTR loc = ::SysAllocString(L"BusLogic");
hr = mutableMachine ->AddStorageController(loc, StorageBus_SCSI, &cont);

Related

How to initialize IWICBitmapDecoder with Windows Imaging Component API

Actually I belive that this problem is easy to solve. It only needs a minor correction. This is the first time that I use Windows Imaging Component (WIC). I am trying to display a PNG picture in my window using Win32 or the Windows API as Microsoft likes to call it. I have written code to initialize it. It requires many steps. I have included a PNG image in my .exe file. Maybe you can look at my code to see what is missing:
void ShowImage() {
IWICBitmapDecoder** ppIDecoder;
IStream* pIStream;
HRESULT hr;
ID2D1RenderTarget* pRenderTarget;
IWICImagingFactory* pIWICFactory = 0;
PCWSTR resourceName, resourceType;
UINT destinationWidth;
UINT destinationHeight;
ID2D1Bitmap** ppBitmap;
IWICBitmapDecoder* pDecoder = NULL;
IWICBitmapFrameDecode* pSource = NULL;
IWICBitmapSource* pISource;
IWICStream* pStream = NULL;
IWICFormatConverter* pConverter = NULL;
IWICBitmapScaler* pScaler = NULL;
HRSRC imageResHandle = NULL;
HGLOBAL imageResDataHandle = NULL;
void* pImageFile = NULL;
DWORD imageFileSize = 0;
// Locate the resource. Aaahhh I should have called Makeintresource and written "PNG"
imageResHandle = FindResource(hInst, MAKEINTRESOURCE(103), L"PNG");
hr = imageResHandle ? S_OK : E_FAIL;
if (SUCCEEDED(hr)) {
// Load the resource.
imageResDataHandle = LoadResource(hInst, imageResHandle);
hr = imageResDataHandle ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr)) {
// Lock it to get a system memory pointer.
pImageFile = LockResource(imageResDataHandle);
hr = pImageFile ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr)) {
// Calculate the size.
imageFileSize = SizeofResource(hInst, imageResHandle);
hr = imageFileSize ? S_OK : E_FAIL;
}
// call coinitialize IWICImagingFactory
if(SUCCEEDED(hr))
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
// Create WIC factory
if (SUCCEEDED(hr)) {
hr = CoCreateInstance(CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIWICFactory)
);
}
if (SUCCEEDED(hr)) {
// Create a WIC stream to map onto the memory.
hr = pIWICFactory->CreateStream(&pStream);
}
if (SUCCEEDED(hr)) {
// Initialize the stream with the memory pointer and size.
hr = pStream->InitializeFromMemory(reinterpret_cast <BYTE*>(pImageFile),
imageFileSize);
}
if (SUCCEEDED(hr)) {
// Create a decoder for the stream.
--->hr = pIWICFactory->CreateDecoderFromStream(pStream, 0, WICDecodeMetadataCacheOnLoad, ppIDecoder);
}
}
I tried the above code which compiles without errors. All the function calls succeed except the last one that I marked with --->. Then the debugger shows a runtime exception because "The variable 'ppIDecoder' is being used without being initialized."
How do I initialize ppIDecoder?
As said, this problem should be quick to solve.

running .net application from c++

I have a byte array of a .NET application inside c++ code.
I want to execute this .NET application without writing those bytes on the disk. ICLRRuntimeHost::ExecuteInDefaultAppDomain expects a path to the assembly so it's out of the equation here. I'm looking for a possible way (or hack) to pass the binary directly to the clr.
So what i can do ?
//todo error checks/cleanup
HRESULT hr;
ICLRMetaHost *pMetaHost = NULL;
ICLRRuntimeInfo *pRuntimeInfo = NULL;
ICorRuntimeHost *pCorRuntimeHost = NULL;
IUnknownPtr spAppDomainThunk = NULL;
_AppDomainPtr spDefaultAppDomain = NULL;
bstr_t bstrAssemblyName(L"");
_AssemblyPtr spAssembly = NULL;
bstr_t bstrClassName(L"");
_TypePtr spType = NULL;
variant_t vtEmpty;
bstr_t bstrStaticMethodName(L"Main");
variant_t vtLengthRet;
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&pMetaHost));
const wchar_t* pszVersion = L"v2.0.50727";
hr = pMetaHost->GetRuntime(pszVersion, IID_PPV_ARGS(&pRuntimeInfo));
BOOL fLoadable;
hr = pRuntimeInfo->IsLoadable(&fLoadable);
if (!fLoadable) { wprintf(L".NET runtime %s cannot be loaded\n", pszVersion); return; }
hr = pRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_PPV_ARGS(&pCorRuntimeHost));
hr = pCorRuntimeHost->Start();
hr = pCorRuntimeHost->GetDefaultDomain(&spAppDomainThunk);
hr = spAppDomainThunk->QueryInterface(IID_PPV_ARGS(&spDefaultAppDomain));
SAFEARRAYBOUND bounds[1];
bounds[0].cElements = array_len;
bounds[0].lLbound = 0;
SAFEARRAY* arr = SafeArrayCreate(VT_UI1, 1, bounds);
SafeArrayLock(arr);
memcpy(arr->pvData, bytearray, array_len);
SafeArrayUnlock(arr);
hr = spDefaultAppDomain->Load_3(arr, &spAssembly);
hr = spAssembly->GetType_2(bstrClassName, &spType);
hr = spType->InvokeMember_3(bstrStaticMethodName, static_cast<BindingFlags>(BindingFlags_InvokeMethod | BindingFlags_Static | BindingFlags_Public), NULL, vtEmpty, nullptr, &vtLengthRet);
SafeArrayDestroy(arr);

Changing Adapter NetConnectionID using IWBemServices in Visual-C++

I am trying to change the NetConnectionID value of a network adapter by creating an instance of the Win32_NetworkAdapter class using IWBemServices. I am locating the adapter by its DeviceId and then trying to put the new NetConnectionID into the instance.
I am not getting any errors, but the NetConnectionID remains unchanged.
This is what my function currently looks like:
int CIPConfigModule::setAdapterName(CString CAdapterName, unsigned int idAdapter)
{
int bNoErrors = NO_ERROR;
IWbemClassObject *pNewInstance = NULL;
IWbemClassObject *pObjectClass = NULL;
IWbemContext *pCtx = NULL;
IWbemCallResult *pResult = NULL;
// Get the class definition.
string strIndex = std::to_string((long double)idAdapter);
string strIndexCommand = "Win32_NetworkAdapter.DeviceID=\"";
strIndexCommand.append(strIndex).append("\"");
CString CSIndexCommand = strIndexCommand.c_str();
// BSTR PathToClass = SysAllocString(L"Example");
HRESULT hRes = m_pSvc->GetObject((bstr_t)CSIndexCommand, 0, pCtx,
&pObjectClass, &pResult);
if (hRes != WBEM_S_NO_ERROR){
bNoErrors=ERROR_ATTEMPTING_TO_CHANGE_NAME;
return bNoErrors;
}
// Create a new instance.
pObjectClass->SpawnInstance(0, &pNewInstance);
pObjectClass->Release(); // Don't need the class any more
VARIANT v;
VariantInit(&v);
// Set the NetConnectioID property.
V_VT(&v) = VT_BSTR;
V_BSTR(&v) = CAdapterName.AllocSysString();
//V_BSTR(&v) = SysAllocString(L"Testing");
BSTR KeyProp = SysAllocString(L"NetConnectionID");
pNewInstance->Put(KeyProp, 0, &v, 0);
SysFreeString(KeyProp);
VariantClear(&v);
// Other properties acquire the 'default' value specified
// in the class definition unless otherwise modified here.
// Write the instance to WMI.
hRes = m_pSvc->PutInstance(pNewInstance, 0, pCtx, &pResult);
pNewInstance->Release();
if (hRes != WBEM_S_NO_ERROR){
bNoErrors=ERROR_ATTEMPTING_TO_CHANGE_NAME;
return bNoErrors;
}
return bNoErrors;
}
Does anyone have an idea of what I could be missing?

How to distinguish between items returned by EnumObjects of IShellFolder

I am trying to get the computers in my workgroup using Shell
HRESULT hr = S_OK;
ULONG celtFetched;
LPITEMIDLIST pidlItems = NULL;
LPITEMIDLIST netItemIdLst = NULL;
IShellFolder* pFolder = NULL;
IEnumIDList* pEnumIds = NULL;
IShellFolder* pDesktopFolder = NULL;
LPITEMIDLIST full_pid;
hr = SHGetMalloc(&pMalloc);
hr = SHGetDesktopFolder(&pDesktopFolder);
hr = SHGetSpecialFolderLocation(NULL, CSIDL_COMPUTERSNEARME, &netItemIdLst);
hr = pDesktopFolder->BindToObject(netItemIdLst, NULL,
IID_IShellFolder, (void **)&pFolder);
if(SUCCEEDED(hr))
{
hr = pFolder->EnumObjects(NULL, SHCONTF_NONFOLDERS, &pEnumIds);
if(SUCCEEDED(hr))
{
STRRET strDisplayName;
while((hr = pEnumIds->Next(1, &pidlItems, &celtFetched)) == S_OK && celtFetched > 0)
{
hr = pFolder->GetDisplayNameOf(pidlItems, SHGDN_NORMAL, &strDisplayName);
if(SUCCEEDED(hr))
{
wprintf(L"%s\n", strDisplayName.pOleStr);
}
}
}
}
But this returns the Computers, Media Devices, and Streaming Devices (is that it name?).
Like say, a computer is called OSOFEM on my workgroup... STRRET::pOleStr returns strings OSOFEM (which is the computer), OSOFEM (I think streaming device), and OSOFEM:example#email.com: (which is the Windows Media Player).
My question is how can I distinguish which is the Computer? Of course, there names can't be used because two exactly the same name will always be returned...

ITuner::put_TuneRequest() call ignored

I have a DirectShow graph with a "Microsoft DVBT Network Provider", "AVerMedia BDA DVBT Tuner", "AVerMEdia BDA Digital Capture", "Sample Grabber" and "NULL Renderer".
These filters are connected.
Beside that I also have an "MPEG-2 Demultiplexer" and a "BDA MPEG2 Transport Information Filter", but these two filters are NOT connected! It seems like they have to be here in order to run the graph.
When I start the graph, I'm receiving TS data, but no matter what I do, I'm not able to put the tuning request. I can only capture the MUX data from the last tuned frequency with some other application like Windows Media Center.
Here is the code for putting the tune request:
// creating tuning space
CComPtr<IDVBTuningSpace> pDVBTuningSpace;<br>
hr = pDVBTuningSpace.CoCreateInstance( __uuidof( DVBTuningSpace ) );
WCHAR szFriendlyName[ 64 ] = L"Local DVB-T Digital Antenna";<br> BSTR bstrFriendlyName = SysAllocString( szFriendlyName );
hr = pDVBTuningSpace->put_UniqueName( bstrFriendlyName );<br>
hr = pDVBTuningSpace->put_FriendlyName( bstrFriendlyName );
SysFreeString( bstrFriendlyName );
CComBSTR clsid_dvbt = ("{216C62DF-6D7F-4e9a-8571-05F14EDB766A}");<br>
hr = pDVBTuningSpace->put_NetworkType( clsid_dvbt );<br>
hr = pDVBTuningSpace->put_SystemType( DVB_Terrestrial );<br>
// creating tune request<br>
CComPtr<ITuneRequest> pTuneRequest;
hr = pDVBTuningSpace->CreateTuneRequest( &pTuneRequest );
CComQIPtr<IDVBTuneRequest> pDVBTuneRequest( pTuneRequest );
hr = pDVBTuneRequest->put_ONID( -1 );<br>
hr = pDVBTuneRequest->put_TSID( -1 );<br>
hr = pDVBTuneRequest->put_SID( -1 );
// locator<br>
CComPtr<IDVBTLocator> pDVBTLocator;
hr = pDVBTLocator.CoCreateInstance( __uuidof( DVBTLocator ) );<br>
hr = pDVBTLocator->put_Bandwidth( 8 );<br>
hr = pDVBTLocator->put_CarrierFrequency( 506000 );
hr = pDVBTuneRequest->put_Locator( pDVBTLocator );
CComQIPtr<ITuner> pTuner( pNetworkProvider_ );
hr = pTuner->put_TuneRequest( pDVBTuneRequest );
This is executed immediately after adding the "Microsoft DVBT Network Provider" filter in the graph.
All "hr" values from the above code are S_OK.
What am I doing wrong? Or, did I miss something big in this "tune request" thing.
(Bandwidth and frequency values are correct)
I think put_Bandwidth( 8 ) is wrong, it should be a bandwidth in Hz. Anyway, I show you some code I use. Maybe it helps.
HRESULT hr;
CComBSTR TuningName;
hr = pDVBTuningSpace2.CoCreateInstance(CLSID_DVBTuningSpace);
hr = pDVBTuningSpace2->put_SystemType(DVB_Terrestrial);
TuningName = L"My DVB-T";
hr = pDVBTuningSpace2->put__NetworkType(CLSID_DVBTNetworkProvider);
CComPtr <IDVBTLocator> pDVBTLocator;
hr = pDVBTLocator.CoCreateInstance(CLSID_DVBTLocator);
hr = pDVBTLocator->put_CarrierFrequency(config->GetFreq());
hr = pDVBTLocator->put_Bandwidth(config->GetSymbolRate());
hr = pDVBTuningSpace2->put_DefaultLocator(pDVBTLocator);
hr = pDVBTuningSpace2->put_UniqueName(TuningName);
hr = pDVBTuningSpace2->put_FriendlyName(TuningName);
hr = pDVBTuningSpace2->put_FrequencyMapping(L"");
CComPtr <ITuningSpaceContainer> pTuningSpaceContainer;
hr = pTuningSpaceContainer.CoCreateInstance(CLSID_SystemTuningSpaces);
VARIANT tiIndex;
hr = pTuningSpaceContainer->Add(pDVBTuningSpace2,&tiIndex);
if (!SUCCEEDED(hr)) {
// Get the enumerator for the collection.
CComPtr<IEnumTuningSpaces> pTuningSpaceEnum;
hr = pTuningSpaceContainer->get_EnumTuningSpaces(&pTuningSpaceEnum);
if (SUCCEEDED(hr)) {
// Loop through the collection.
CComPtr<ITuningSpace> pTuningSpace;
//ITuningSpace *pTuningSpace;
tiIndex.intVal=0;
while (S_OK == pTuningSpaceEnum->Next(1, &pTuningSpace, NULL)) {
USES_CONVERSION;
BSTR Name;
hr = pTuningSpace->get_UniqueName(&Name);
if (SUCCEEDED(hr)) {
if (wcscmp(OLE2W(Name), TuningName) == 0) {
hr = pTuningSpaceContainer->put_Item(tiIndex,pDVBTuningSpace2);
}
SysFreeString(Name);
}
tiIndex.intVal++;
//pTuningSpace->Release();
pTuningSpace.Release();
}
}
}
CComPtr<ITuneRequest> pTuneRequest;
hr = pDVBTuningSpace2->CreateTuneRequest(&pTuneRequest);
CComQIPtr<IDVBTuneRequest> pDVBTuneRequest(pTuneRequest);
if(pDVBTuneRequest) {
hr = pDVBTuneRequest->put_SID(config->GetSid());
hr = pDVBTuneRequest->put_TSID(config->GetTsid());
hr = pDVBTuneRequest->put_ONID(config->GetOnid());
}
GUID CLSIDNetworkType;
hr = pDVBTuningSpace2->get__NetworkType(&CLSIDNetworkType);
hr = CoCreateInstance(CLSIDNetworkType, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **) &pNetworkProvider);
hr = graph->AddFilter(pNetworkProvider,L"Network Provider");
// Query for ITuner.
CComQIPtr<ITuner> pTuner(pNetworkProvider);
if (pTuner) {
// Submit the tune request to the network provider.
hr = pTuner->put_TuneRequest(pTuneRequest);
}
hr = graph->AddFilter(pBdaNetworkTuner,L"BDA Source");
hr = ConnectFilters(pNetworkProvider,pBdaNetworkTuner);
CComPtr<IBaseFilter> pBdaReceiver;
hr = FindDevice(KSCATEGORY_BDA_RECEIVER_COMPONENT, &pBdaReceiver, 0, 0, 0);
hr = graph->AddFilter(pBdaReceiver,L"BDA Receiver");
hr = ConnectFilters(pBdaNetworkTuner,pBdaReceiver);
CComPtr<IBaseFilter> pMpegDemux;
hr = pMpegDemux.CoCreateInstance(CLSID_MPEG2Demultiplexer);
hr = graph->AddFilter(pMpegDemux,L"MPEG Demux");
hr = ConnectFilters(pBdaReceiver,pMpegDemux);
You are doing some things in a different order, but I'm not sure if it matters.