Hm, what does -mms-bitfields do?
Anyway, I have found the problem. What was wrong was that I called the function system, which automatically opens a console window. Starting the new program with CreateProcess instead prevents that.
Oh, yeah and another thing - my code doesn't work if any of the arguments following on the first one contains a space, because my program doesn't wrap them in quotation signs. The best is to use the function GetCommandLine, parse the string you get to find where the second argument starts, and then just add the rest of the line from there to the name of the application that you want to open. Okay, if anyone is interested, here's new and (hopefully) working code, now with spaces instead of tabs:
Code:
#include <string.h>
#include <windows.h>
#define MALLOC_NUM(ptr, number) { \
ptr = (typeof(ptr)) malloc(sizeof(*ptr)*number); \
if (!ptr) { \
MessageBox(0, "Not enough memory", \
"open_with_emacs", MB_ICONERROR); \
exit(1); \
} \
}
int main(int argc, char **argv) {
const char* app = "runemacs";
const unsigned int app_name_length = strlen(app);
char *curr_cl, *new_cl; /* Current and new command line */
/* For creating the process */
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
/* Find arguments */
curr_cl = GetCommandLine();
if (argc >= 1) {
if (*curr_cl == '"' ) curr_cl += strlen(argv[0]) + 2;
else if (*curr_cl != '\0') curr_cl += strlen(argv[0]);
}
/* Create new command line */
MALLOC_NUM(new_cl, app_name_length + strlen(curr_cl) + 1);
strcpy(new_cl, app);
strcpy(new_cl + app_name_length, curr_cl);
/* Run command line */
if (CreateProcess(0, /* lpApplicationName, optional */
new_cl, /* lpCommandLine, The command line to be executed */
0, /* lpProcessAttributes */
0, /* lpThreadAttributes */
0, /* bInheritHandles */
0, /* dwCreationFlags */
0, /* lpEnvironment */
0, /* lpCurrentDirectory */
&si, /* lpStartupInfo */
&pi /* lpProcessInformation, receiving process id information */
)) {
/* optionally wait for process to finish */
/* WaitForSingleObject(pi.hProcess, INFINITE); */
/* Closing handles is completely safe */
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
free(new_cl);
return 0;
}
And this makefile can be used:
Code:
CC=gcc
CCFLAGS= -Wall -Wextra -pedantic -g -std=c++98
CCFLAGS+=-lmingw32
CCFLAGS+=-Wl,--subsystem,windows #Can sometimes be replaced with -mwindows
FILES=main.x
SRC=$(FILES:.x=.c)
OBJ=$(FILES:.x=.o)
OUT=open_with_emacs.exe
all: $(OUT)
rm $(OBJ) #Remove .o files
$(OBJ) :
$(CC) $(SRC) $(CCFLAGS) -c #Create .o files
$(OUT) : $(OBJ)
$(CC) $(OBJ) $(CCFLAGS) -o $@ #Link .o files