How to run exe in guest session

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
KrekerMeister
Posts: 15
Joined: 14. Sep 2022, 17:00

How to run exe in guest session

Post by KrekerMeister »

I want to run the program in the guest Windows, I create a session, everything is successful, only when it comes to processes, nothing happens. Methods on the type of file records, getting information through FileExists works.

The question is how to run the application so that it is displayed inside the guest GUI? Through vboxmanage guest control is works, although the result is displayed in the CMD host.

Code: Select all

public class Main {

    public static VirtualBoxManager mgr;
    public static IVirtualBox vbox;

    public static void main(String[] args) throws InterruptedException {

        mgr = VirtualBoxManager.createInstance(null);
        mgr.connect("http://localhost:18083", "User", "a12345678");
        vbox = mgr.getVBox();
        IMachine mach = Main.vbox.findMachine("Test");
        ISession session = Main.mgr.getSessionObject();
        mach.lockMachine(session, LockType.Shared);
        IGuestSession guestSession = null;
        try {
            guestSession = session.getConsole().getGuest().createSession("TestUser", "changeme", null,null);
            guestSession.waitFor(1l, 30 * 1000l);
            System.out.println("GuestSessionStatus: " + guestSession.getStatus());
            IProcess process = guestSession.processCreate("C:\\Windows\\System32\\tasklist.exe", null,null, Collections.singletonList(ProcessCreateFlag.Profile), 0L);
            IGuestProcess processa = IGuestProcess.queryInterface(process);
            System.out.println("firstProcessStatus: " + processa.getStatus().toString());
            processa.waitForArray(Collections.singletonList(ProcessWaitForFlag.StdOut), 0L);
            ProcessStatus processStatus = processa.getStatus();
            System.out.println("Process Status: " + processStatus.toString() + "|value: " + processStatus.value() + "|name" + processStatus.name());

            System.out.println("GuestSessionUser: " + guestSession.getUser());


            System.out.println("lastProcessStatus: " + processa.getStatus());
            System.out.println("statusSessionLast: " + guestSession.getStatus());
            System.out.println("processGet: " + guestSession.processGet(1L).toString());
        } finally {
            guestSession.close();
            session.unlockMachine();
            mgr.cleanup();
            mgr.disconnect();
        }
    }
}
I get this result:

Code: Select all

GuestSessionStatus: Started
firstProcessStatus: Started
Process Status: Started|value: 100|nameStarted
GuestSessionUser: TestUser
lastProcessStatus: Started
statusSessionLast: Started
Exception in thread "main" org.virtualbox_7_0.VBoxException: VirtualBox error: rc=0x80070057 No process with PID 1 found (0x80070057)
	at org.virtualbox_7_0.IGuestSession.processGet(IGuestSession.java:2092)
	at Main.main(Main.java:38)
Caused by: org.virtualbox_7_0.jaxws.RuntimeFaultMsg: VirtualBox error: rc=0x80070057 No process with PID 1 found (0x80070057)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:135)
	at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:189)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:276)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:104)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:77)
	at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:147)
	at com.sun.proxy.$Proxy35.iGuestSessionProcessGet(Unknown Source)
	at org.virtualbox_7_0.IGuestSession.processGet(IGuestSession.java:2083)
	... 1 more
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: How to run exe in guest session

Post by noteirak »

Code: Select all

System.out.println("processGet: " + guestSession.processGet(1L).toString());
You assume the PID of the process is 1L which is most likely not the case. Try by using the PID given to you by your IProcess variable. See the reference doc.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
KrekerMeister
Posts: 15
Joined: 14. Sep 2022, 17:00

Re: How to run exe in guest session

Post by KrekerMeister »

noteirak wrote:

Code: Select all

System.out.println("processGet: " + guestSession.processGet(1L).toString());
You assume the PID of the process is 1L which is most likely not the case. Try by using the PID given to you by your IProcess variable. See the reference doc.

I changed and get PID. Only in Windows itself there is no such process under the number. Can you please tell me how to run application programs in the machine itself?
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: How to run exe in guest session

Post by noteirak »

Please show the new code and the output so we can see what you changed and the result
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
KrekerMeister
Posts: 15
Joined: 14. Sep 2022, 17:00

Re: How to run exe in guest session

Post by KrekerMeister »

noteirak wrote:Please show the new code and the output so we can see what you changed and the result
Code with fixes

Code: Select all

public class Main {

    public static VirtualBoxManager mgr;
    public static IVirtualBox vbox;

    public static void main(String[] args) throws InterruptedException {

        mgr = VirtualBoxManager.createInstance(null);
        mgr.connect("http://localhost:18083", "TestUser", "a12345678");
        vbox = mgr.getVBox();
        IMachine mach = Main.vbox.findMachine("Market #561");
        ISession session = Main.mgr.getSessionObject();
        mach.lockMachine(session, LockType.Shared);
        IGuest guest = session.getConsole().getGuest();
        IGuestSession guestSession = null;
        try {

            guestSession = guest.createSession("Market #561", "changeme", null,null);
            guestSession.waitFor(1l, 30 * 1000l);
            System.out.println("GuestSessionStatus: " + guestSession.getStatus());
            IProcess process = guestSession.processCreate("C:\\Windows\\System32\\tasklist.exe", null,null, Collections.singletonList(ProcessCreateFlag.Profile), 0L);
            IGuestProcess processa = IGuestProcess.queryInterface(process);
            System.out.println("firstProcessStatus: " + processa.getStatus().toString());
            processa.waitForArray(Collections.singletonList(ProcessWaitForFlag.Start), 0L);
            ProcessStatus processStatus = processa.getStatus();
            System.out.println("Process Status: " + processStatus.toString() + "|value: " + processStatus.value() + "|name" + processStatus.name());

            System.out.println("GuestSessionUser: " + guestSession.getUser());
            System.out.println("lastProcessStatus: " + processa.getStatus());
            System.out.println("statusSessionLast: " + guestSession.getStatus());
            System.out.println("processGet: " + process.getExecutablePath().toString());
            System.out.println("processArguments: " + process.getArguments().toString());
            System.out.println("processName: " + process.getName());
            System.out.println("processPID: " + process.getPID());
            System.out.println("processRead: " + process.read(0L, 10 * 100000l, 0L).toString());
            Thread.sleep(60 * 1000l);
        } finally {
            guestSession.close();
            session.unlockMachine();
            mgr.cleanup();
            mgr.disconnect();
        }
    }
}
Console output

Code: Select all

GuestSessionStatus: Started
firstProcessStatus: Starting
Process Status: Started|value: 100|nameStarted
GuestSessionUser: Market #561
lastProcessStatus: Started
statusSessionLast: Started
processGet: C:\Windows\System32\tasklist.exe
processArguments: [C:\Windows\System32\tasklist.exe]
processName: 
processPID: 2672
processRead: [B@17d10166
I looked a little about the windows session, I need to run the application with administrator rights (autoit .exe script) in the console session (physical). In general, run the script and make it work like in the main session
If you need a screenshot of the processes in the task manager: https://imgur.com/a/POuke1p
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: How to run exe in guest session

Post by noteirak »

Thanks, so the process itself is running and you don't get errors anymore. For the configuration part of running it the way you want, I don't know how to do it on Windows. Hopefully someone else will reply and help you on that bit
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
KrekerMeister
Posts: 15
Joined: 14. Sep 2022, 17:00

Re: How to run exe in guest session

Post by KrekerMeister »

noteirak wrote:Thanks, so the process itself is running and you don't get errors anymore. For the configuration part of running it the way you want, I don't know how to do it on Windows. Hopefully someone else will reply and help you on that bit
Well, then one more question, is it possible to run the process as administrator through these sessions?
Post Reply