[C++]Copy file to guest host

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
Nitron
Posts: 2
Joined: 13. Dec 2018, 15:40

[C++]Copy file to guest host

Post by Nitron »

Host: Windows 7 64
Guest: Ubuntu 64 / WIndows 7 64
Version: 5.2.22
Guest Additions: 5.2.22

Hello,
I have the problem with copy file to guest host. While creation IGuestSession seems appear some problem.
The call CreateSession completed seems OK but WaitFor return errors.

CreateSession(userName, userPass, 0, 0, &pGSession);
GuestSessionWaitResult resWaitSess;
rc = pGSession->WaitFor( GuestSessionWaitForFlag_Start, 30 * 1000, &resWaitSess );
rc == 0x80bb0005 (The specified user was not able to logon on guest)

I've tryed this operation with guest machines on Ubuntu 16 and Windows 7. In both cases parameter user was correct. THe password parameter used only in Ubuntu.

Code: Select all

	
        HRESULT rc;

	IMachine *machine = NULL;
	BSTR machineName = SysAllocString(L"Ubuntu18");
	rc = virtualBox->FindMachine(machineName, &machine);
	if (!FAILED(rc))
	{
		ISession *session = NULL;
		IConsole *console = NULL;
		IProgress *progress = NULL;
		BSTR sessiontype = SysAllocString(L"gui");
		BSTR guid;
		do
		{
			rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
			if (!SUCCEEDED(rc))
			{
				printf("Error retrieving machine ID! rc = 0x%x\n", rc);
				break;
			}

			/* Create the session object. */
			rc = CoCreateInstance(CLSID_Session,        /* the VirtualBox base object */
				NULL,                 /* no aggregation */
				CLSCTX_INPROC_SERVER, /* the object lives in the current process */
				IID_ISession,         /* IID of the interface */
				(void**)&session);
			if (!SUCCEEDED(rc))
			{
				printf("Error creating Session instance! rc = 0x%x\n", rc);
				break;
			}

			/* Start a VM session using the delivered VBox GUI. */
			rc = machine->LaunchVMProcess(session, sessiontype,
				NULL, &progress);
			if (!SUCCEEDED(rc))
			{
				printf("Could not open remote session! rc = 0x%x\n", rc);
				break;
			}
			/* Wait until VM is running. */
			printf("Starting VM, please wait ...\n");
			rc = progress->WaitForCompletion(-1);

			/* Bring console window to front. */
			rc = machine->ShowConsoleWindow(0);

			Sleep(40000);
                        // in this poins the guest machine loaded completely

			rc = session->UnlockMachine();
			rc = machine->LockMachine(session, LockType_Shared);
			rc = session->get_Console(&console);

			IGuest* pGuest = nullptr;
			rc = console->get_Guest(&pGuest);

			IGuestSession* pGSession = nullptr;
			BSTR userName = SysAllocString(L"User");  [b]// The Name and password are correct[/b]
			BSTR userPass = SysAllocString(L"1");
			rc = pGuest->CreateSession(userName, userPass, 0, 0, &pGSession);
			GuestSessionWaitResult resWaitSess;
			rc = pGSession->WaitFor( GuestSessionWaitForFlag_Start, 30 * 1000, &resWaitSess );
			IErrorInfo* pInfoErr = nullptr;
			rc = GetErrorInfo(0, &pInfoErr);
			BSTR szErrStr = nullptr;
			pInfoErr->GetDescription(&szErrStr);
                        // szErrStr == "The specified user was not able to logon on guest"

			BSTR srcFile = SysAllocString(L"D:\\WishList.txt");
			BSTR dstFile = SysAllocString(L"./home/WishList.txt");
			rc = pGSession->FileCopyToGuest(srcFile, dstFile, 0, &progress);
			rc = progress->WaitForCompletion(-1);

                        // [b]last 2 times the rc had E_OK but file not appear in guest[/b]
			BSTR filePath = SysAllocString(L"./home/Temp2");
			rc = pGSession->DirectoryCreate(filePath, 0700, 0);

			session->UnlockMachine();

			pGSession->Close();

			MachineState state;
			rc = machine->get_State(&state);

			// Power Down VM
			rc = console->PowerDown(&progress);
			rc = progress->WaitForCompletion(-1);
		} while (0);
Thank you for any help!
Nitron
Posts: 2
Joined: 13. Dec 2018, 15:40

Re: [C++]Copy file to guest host

Post by Nitron »

It possible partial solved.
In Windows OS must be set a password in account. If password not been set then we will have 0x80bb0005 (The specified user was not able to logon on guest)
Using account without password on Windows OS is possible only on "Professional" if set special flag: gpedit.msc Windows Settings -> Security Settings -> Local Policies -> Security Options -> Accounts: Limit local account use of blank passwords to console logon only and set it to disabled.

The problem with Ubuntu still open.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: [C++]Copy file to guest host

Post by noteirak »

As per IGuest::createSession() doc:
Anonymous sessions, that is, sessions without specifying a valid user account in the guest are not allowed reasons of security.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
LordKaoS
Posts: 11
Joined: 29. Oct 2018, 09:04
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Ubuntu 18.04, MS Windows 7

Re: [C++]Copy file to guest host

Post by LordKaoS »

The problem with Ubuntu still open.
Not an expert, but I suggest replacing the 0 parameters in
CreateSession(userName, userPass, 0, 0, &pGSession);
with empty strings or nullptr.
Post Reply