CreateProcess / WaitForSingleObject linux command

Open forum for end-user questions about Wine. Before asking questions, check out the Wiki as a first step.
Forum Rules
Locked
darseq
Newbie
Newbie
Posts: 1
Joined: Fri Oct 31, 2014 10:23 am

CreateProcess / WaitForSingleObject linux command

Post by darseq »

Probably this is the complete wrong place to put this, but I didn't know how to update the Wiki (link below)

http://wine-wiki.org/index.php/Advanced ... ns_another

So I thought I just write down some info here so someone might add it to the wiki. It has to do with this (old) bug report https://bugs.winehq.org/show_bug.cgi?id=22338. For obvious reasons the bug is a "won't fix". For the ShellExecute & WaitForSingleObject combination however I have come up with a solution (based on code in the aforementioned wiki page). To see how it works, have a look at the c++ code below and compile it to a windows executable, say spawn.exe. Then associate a windows file extention with spawn.exe using regedit as described here: http://wiki.winehq.org/FAQ#head-bc5b677 ... 1b2341cb98. When spawn runs, it generates a temporary windows file in c:\ and spawns a linux script of your own making (in this example open_doc.sh). From within your script access your document with: "`wine winepath -u "$1"`" and after it has done its work, simply remove the temporary file using: rm "`wine winepath -u "$2"`". This is an indicator for spawn to stop waiting and the calling process (waiting for the hProcess handle of spawn.exe) can continue its work. Note that the quoting function should be improved on but in most cases it does the job:

Code: Select all

#include <sys/stat.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * add_quotes(char * string) {
	int length = strlen(string);
	char * quoted = (char *) malloc(sizeof(char)*(length+3));
	quoted[0] = '"';
	quoted[1] = '\0';
	strncat(quoted, string, length);
	strncat(quoted, "\"", 1);
	return quoted;
}

int main(int argc, char *argv[]) {
	if (argc == 2) {
		printf("spawning: %s\n", argv[1]);
		char * pidfile = tmpnam(NULL);
		if (pidfile) {
			printf("pidfile: %s\n", pidfile);
			FILE * file = fopen(pidfile, "w");
			if (file) {
				fclose(file);
				char * quoted_path = add_quotes(argv[1]);
				char * quoted_pidfile = add_quotes(pidfile);
				spawnlp(_P_NOWAIT, "open_doc.sh", "open_doc.sh",
					quoted_path, quoted_pidfile, NULL);
				free(quoted_path);
				free(quoted_pidfile);
				struct stat result;
				while (stat(pidfile, &result) == 0);
			}
		}
	}
	printf("exiting...\n");
	return 0;
}
User avatar
dimesio
Moderator
Moderator
Posts: 13367
Joined: Tue Mar 25, 2008 10:30 pm

Re: CreateProcess / WaitForSingleObject linux command

Post by dimesio »

darseq wrote:Probably this is the complete wrong place to put this, but I didn't know how to update the Wiki (link below)

http://wine-wiki.org/index.php/Advanced ... ns_another
This is the wrong place; wine-wiki.org is not part of WineHQ. Contact them about how to update their wiki.

If you want to add the info to our wiki, just create an account for yourself and add it.
Locked