0

I'm trying to execute a .jar file from my own C++ .dll code with parameters and get result string which will be printed. Unfortunately, code doesn't find the popen or _popen command although I already tried to find the right imports like #include <stdio.h> or #include <stdlib.h>. Is it even possible to execute a .jar from a .dll with an acceptable performance and how is this possible? I didn't find an example which worked for me.

The .jar file will just be called like java -jar test.jar "samplestring". Is it possible to do what the console does when I type this command and get the result?

I tried this example (problem: popen and pclose aren't defined): popen() writes output of command executed to cout

Edit: UWP doesn't support popen or _popen

Now i got this:

STARTUPINFOW        siStartupInfo;
PROCESS_INFORMATION piProcessInfo;

memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));

if (CreateProcess(TEXT("C:\\Program Files (x86)\\Java\\jdk1.8.0_161\\bin\\java.exe"), TEXT("-jar decode.jar"), NULL, NULL, false, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo, &piProcessInfo) == false) {
    return -1; //Error
}


CloseHandle(piProcessInfo.hProcess);
CloseHandle(piProcessInfo.hThread);

How can I get the output of the CreateProcess command? Is this possible?

Updater
  • 459
  • 2
  • 13
  • 2
    Please provide some small snippet of what you are trying. May be the Cpp code and the Java Code that are being called from within. However, the ideal way to call Java code is via JNI Interfaces. Hope you find this link https://stackoverflow.com/questions/992836/how-to-access-the-java-method-in-a-c-application helpful. – piy26 Feb 11 '19 at 09:07
  • The .jar file will just be called like java -jar test.jar "samplestring". It will produce a simple output. I will edit the question. – Updater Feb 11 '19 at 09:26
  • 2
    `popen` is a POSIX API. It doesn't exist on Windows. See [this question](https://stackoverflow.com/questions/450865/what-is-the-equivalent-to-posix-popen-in-the-win32-api) for more details. For better performance you might want to [embed the JVM](https://stackoverflow.com/questions/7506329/embed-java-into-a-c-application) instead of starting `java` as a separate process. – rustyx Feb 11 '19 at 09:30
  • 1
    `_popen` exists in Windows though, but [not in UWP apps](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/popen-wpopen?view=vs-2017). I'd use `CreateProcess` instead. – Ted Lyngmo Feb 11 '19 at 09:43
  • I edited the question. I am able to execute the jar now, but i have to get 1 unsigned long value as output somehow. Have you got any idea how this could work? Maybe with CreatePipe or smth – Updater Feb 13 '19 at 14:01

0 Answers0