[Solved] Can't run a process inside guest system through IGuestSession::processCreate with arguments

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
gorin86@mail.ru
Posts: 2
Joined: 10. Aug 2018, 11:19

[Solved] Can't run a process inside guest system through IGuestSession::processCreate with arguments

Post by gorin86@mail.ru »

Hello! I can't run a process inside guest system through IGuestSession::processCreate with arguments. The process runs, but without arguments. How can I run the process with arguments?

Code: Select all

public class Test {

    public static void main(String[] args){
        test();
    }

    private static void test() {
        VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
        try {
            vboxManager.connect("hxxp://localhost:18083", null, null);
            try {
                ISession session = vboxManager.getSessionObject();
                IMachine vm = vboxManager.getVBox().findMachine("test");
                vm.launchVMProcess(session, "headless", null).waitForCompletion(-1);
                IConsole console = session.getConsole();
                try {
                    IGuestSession guestSession =
                            console.getGuest().createSession("<login>","<password>", null, null);
                    try {
                        guestSession.waitFor(1L, 30 * 1000L);
                        if (!guestSession.getStatus().equals(GuestSessionStatus.Started)) {
                            throw new RuntimeException("Guest session did not start after 30 sec");
                        }
                        for (;;) {
                            try {
                                guestSession.processCreate(
                                        "C:\\Windows\\System32\\tasklist.exe",
                                        null,
                                        null,
                                        null,
                                        0L
                                ).waitFor((long) ProcessWaitForFlag.Start.value(), 0L);
                                break;
                            } catch (VBoxException e) {
                                if (!e.toString().contains("The guest execution service is not ready (yet)")) {
                                    throw e;
                                }
                            }
                            Thread.sleep(1000);
                        }
                        for (;;) {
                            String processes = execute(guestSession, "C:\\Windows\\System32\\tasklist.exe", null);
                            if (processes.contains("VBoxTray.exe")) {
                                break;
                            }
                            Thread.sleep(1000);
                        }
                        List<String> args = new ArrayList <>();
                        //args.add("C:\\Windows\\System32\\tasklist.exe");
                        //args.add(null);
                        args.add("/FO LIST");
                        System.out.println(execute(guestSession, "C:\\Windows\\System32\\tasklist.exe", args));
                    } finally {
                        guestSession.close();
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                } finally {
                    System.out.println("Machine unlock");
                    session.unlockMachine();
                }
            } finally {
                vboxManager.disconnect();
                System.out.println("Disconnected");
            }
        } finally {
            vboxManager.cleanup();
            System.out.println("Closing");
        }
    }

    private static String execute(IGuestSession session, String file, List<String> args) {
        IGuestProcess process = session.processCreate(
                file,
                args,
                null,
                Collections.singletonList(ProcessCreateFlag.WaitForStdOut),
                0L
        );
        IEventSource es = process.getEventSource();
        IEventListener el = es.createListener();
        es.registerListener(el, Collections.singletonList(VBoxEventType.Any), false);
        StringBuilder builder = new StringBuilder();
        try {
            ProcessWaitResult pwr = process.waitFor((long) ProcessWaitForFlag.Start.value(), 30 * 1000L);
            boolean keepLooping;
            do {
                IEvent ev = es.getEvent(el, 200);
                if (ev != null) {
                    es.eventProcessed(el, ev);
                }
                byte[] stdOut = process.read(1L, 1024L * 1024L, 0L);
                builder.append(new String(stdOut, "cp866"));
                keepLooping = !process.getStatus().toString().contains("Terminated");
            } while (keepLooping);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } finally {
            es.unregisterListener(el);
            if (!process.getStatus().toString().contains("Terminated")) {
                process.terminate();
            }
        }
        return builder.toString();
    }
}
The output:

Code: Select all

Connected to the target VM, address: '127.0.0.1:53656', transport: 'socket'

Имя образа                     PID Имя сессии          № сеанса       Память
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0         4 КБ
System                           4 Services                   0       268 КБ
smss.exe                       264 Services                   0       916 КБ
csrss.exe                      352 Services                   0     3 644 КБ
wininit.exe                    408 Services                   0     3 464 КБ
csrss.exe                      416 Console                    1    13 876 КБ
winlogon.exe                   460 Console                    1    15 808 КБ
services.exe                   500 Services                   0     5 448 КБ
lsass.exe                      508 Services                   0     7 880 КБ
svchost.exe                    580 Services                   0     9 596 КБ
svchost.exe                    624 Services                   0     5 756 КБ
dwm.exe                        716 Console                    1    43 452 КБ
VBoxService.exe                724 Services                   0     6 480 КБ
svchost.exe                    820 Services                   0    15 180 КБ
svchost.exe                    872 Services                   0    27 200 КБ
svchost.exe                    924 Services                   0    10 764 КБ
svchost.exe                    972 Services                   0     9 148 КБ
svchost.exe                    276 Services                   0    10 636 КБ
spoolsv.exe                    952 Services                   0     3 844 КБ
svchost.exe                   1032 Services                   0    16 960 КБ
dasHost.exe                   1248 Services                   0     3 664 КБ
MsMpEng.exe                   1328 Services                   0    69 984 КБ
svchost.exe                   1700 Services                   0     5 308 КБ
sppsvc.exe                    1940 Services                   0    13 928 КБ
WmiPrvSE.exe                  1948 Services                   0     6 292 КБ
svchost.exe                   2040 Services                   0     4 512 КБ
taskhostex.exe                1212 Console                    1     8 028 КБ
userinit.exe                  1308 Console                    1     3 368 КБ
explorer.exe                  1796 Console                    1    74 572 КБ
SppExtComObj.Exe              2476 Services                   0     5 892 КБ
ThumbnailExtractionHost.e     2540 Console                    1     4 504 КБ
SearchIndexer.exe             2628 Services                   0     8 700 КБ
mobsync.exe                   2948 Console                    1     7 924 КБ
VBoxTray.exe                  1224 Console                    1     6 332 КБ
tasklist.exe                   828 Console                    1     5 352 КБ
conhost.exe                   1880 Console                    1     4 968 КБ

Machine unlock
Disconnected
Closing
Disconnected from the target VM, address: '127.0.0.1:53656', transport: 'socket'

Process finished with exit code 0
I tried different options of filling arguments:

1) args.add("C:\\Windows\\System32\\tasklist.exe");
args.add("/FO LIST");

2) args.add(null);
args.add("/FO LIST");

3) args.add("/FO LIST");

But result is same.

The output format of "C:\Windows\System32\tasklist.exe /FO LIST" should be the following:

Code: Select all

Имя образа:     System Idle Process
PID:            0
Имя сессии:     Services
№ сеанса:       0
Память:         24 КБ

Имя образа:     System
PID:            4
Имя сессии:     Services
№ сеанса:       0
Память:         1 692 КБ
My environment:
VirtualBox version 5.2.16
VirtualBoxSDK version 5.2.16
VirtualBox Extension Pack version 5.2.16
VirtualBox Guest Edditions version 5.2.16 inside guest system
Connection type Webservice
Java 8
Guest system Windows 8.1 x64
Host system Windows 7 x64
Last edited by socratis on 31. Oct 2018, 10:24, edited 1 time in total.
Reason: Marked as [Solved].
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: Can't run a process inside guest system through IGuestSession::processCreate with arguments

Post by noteirak »

Code: Select all

/FO LIST
This is two arguments, not one.

You need to do:

Code: Select all

args.add("/FO");
args.add("LIST");
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
gorin86@mail.ru
Posts: 2
Joined: 10. Aug 2018, 11:19

Re: Can't run a process inside guest system through IGuestSession::processCreate with arguments

Post by gorin86@mail.ru »

Thanks! I overlooked it. It works with

Code: Select all

args.add(null);
args.add("/FO");
args.add("LIST");
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Can't run a process inside guest system through IGuestSession::processCreate with arguments

Post by socratis »

Marking as [Solved].
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Post Reply