execvp() equivalent for Win32

Hi to all,
first of all, a little introduction.
I decided to create a Java Local Application (JAR file) some time ago, and now it's finished (at least the beta). Later on I decided to migrate the options check outside of the Java code, building a C launcher that checks all the given options and, if they are correct, invokes the JVM using the GNU Unix function execvp(const char*, char *const[]). I chose the C language for its execution speed, and also because checking parameters did not required OO-programming. I also chose a specifically Unix function because I'm a Linux developer, so I wanted to test the application firstly on my system. The code's section looks like this:

1
2
char *const arguments[7] = {cmd, option, archive, debugSymbol, sourceFilename, destFilename, (char*) 0};
execValue = execvp(command, arguments);

where cmd (and command too) are strings that points to the Java binary, option is the "-jar" option, archive the location of my JAR, debugSymbol enables the Java stack trace, sourceFilename and destFilename are the input (already existent) and output (to be created) files of the JVM.

Problem is, I want to release a Win32 launcher that has the same behaviour of the Unix one, i.e. that it replaces the current executing process area with the one of the new process (JVM), but I don't know what function to use. system() won't work for me, because it will fork a new process, then execute the given command over the newly created process. For the Win32 launcher I intend to use plain C/C++ code, not a demi-interpreted one by .NET.

By the way, the JAR is a simple Universal Turing Machine.
I think I'm going to release the project over SourceForge, but I still haven't. I'll let you know when I'll do it.

Thank you for your help,
Marco
Last edited on
So what you want is to start a new process, right? That's easy, but keep in mind that this function is asynchronous (there are ways to hold the program until the other process finishes, if necessary):
Oh, brother... This is one of those functions:
1
2
3
4
5
6
7
8
9
10
11
12
BOOL WINAPI CreateProcess(
  __in          LPCTSTR lpApplicationName,
  __in_out      LPTSTR lpCommandLine,
  __in          LPSECURITY_ATTRIBUTES lpProcessAttributes,
  __in          LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in          BOOL bInheritHandles,
  __in          DWORD dwCreationFlags,
  __in          LPVOID lpEnvironment,
  __in          LPCTSTR lpCurrentDirectory,
  __in          LPSTARTUPINFO lpStartupInfo,
  __out         LPPROCESS_INFORMATION lpProcessInformation
);

Example:
1
2
3
4
5
6
7
8
9
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
ZeroMemory(&pi,sizeof(pi));
if(!CreateProcess(L"C:/WINDOWS/notepad.exe",L"notepad.exe c:/readme.txt",0,0,0,0,0,0,&si,&pi))
	//Could not start process;
//Now 'pi.hProcess' contains the process HANDLE, which you can use to wait for it like this:
WaitForSingleObject(pi.hProcess,INFINITE);
Hmm.. it so happens I also just posted an example here:
http://www.cplusplus.com/forum/beginner/1988/page3.html#msg14102
Works with or without UNICODE and shows how to use arguments.
(Use the first as the exe name, and for the command line, concatenate all the arguments, including the exe name, together into one string.)
First of all, thanks for your fast replies.
I was forced by events to be unavailable for these 4 days, so I'm going to reply to you now.
The code has sense, and I will gladly implement it, but I don't think it will work for me... Fact is, if I have correctly understood it, the code you have so kindly suggested to me, will spawn a new process, then waits until the spawned thread will terminate. My Java application, unfortunately, at least this first beta version, is console-based without a GUI. So I need to launch the application inside the same console that started the launcher, otherwise I won't be able to see the results of the computation.
So I'm going to ask you, will the code you suggested spawn a new console or not?

I'm sorry if right now I'm a bit lazy, but as I already told you this is not a good moment, so I just need some time to reflect and then start again with my normal life.
Thanks very much for your replies
Marco
Last edited on
It should, as long as you specify that the child inherits the parent's standard handles.
Thanks so much for all your help...
Unfortunately for me, I should have firstly checked if MS Visual C++ is supported by Wine, which in fact is not. So right now I'm stuck with the code because I deleted my Win partition and I can't install (least of all execute) Visual C++, and I don't know of a single Win compiler (other than MingW) that runs under Wine... So the first (temporary) solution I've found is that of compiling the source with MingW.
I hope to find very soon a compiler that will work for me, and then I'll implement both functions (execvp and CreateProcess).

@Duoas: I saw in an old post that you use Kubuntu; if that's still right, how do you compile your code for Visual C++? Maybe you already discussed this elsewhere, but I couldn't find it.

Sorry for bothering you
Marco
Last edited on
Uhh... Can't you compile MinGW from Linux? That would make it a cross-compiler, wouldn't it?
@helios:
Uhh... Can't you compile MinGW from Linux? That would make it a cross-compiler, wouldn't it?

Yes indeed, that's what I've done for now, and I've already done that. But, considering that the execvp function is system dependant, I wanted to write a code which has a similar behaviour to the one of execvp AND uses a Win32 function. Problem is, I can't install Visual C++ because I removed Vista some time ago, and VC++ doesn't run over the Wine layer.
Last edited on
Topic archived. No new replies allowed.