/*************************************************************** Simple wrapper to start Win32 console applications without showing the console. (c) Peter van Eerten, November 24, 2002 -Adjusted for MinGW at february 27, 2004 -Adjusted for programs using a DLL to realize a GUI at february 16, 2006 December 22, 2006: -Added functionality for stop.ini -Improvement on code -The run.exe should also work correct in Win9x/ME now Licensed under the GPL license. ***************************************************************/ #include #include #define MAX_LENGTH 1024 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { /* Declarations for current filename */ char filename[MAX_PATH]; DWORD len; char line[MAX_LENGTH]; char *end; char *exe = NULL; char *flag = NULL; char *ininame = NULL; /* Declarations for new process */ STARTUPINFO si = {sizeof(si)}; PROCESS_INFORMATION pi; int ret; FILE *setupini; FILE *pidini; /* Needed for OS detection */ OSVERSIONINFOEX osvi; BOOL bOsVersionInfoEx; /* First get current filename */ len=GetModuleFileName(NULL, filename, MAX_PATH); end=filename+len-1; /* Check format */ if (strstr(filename, ".") == NULL){ MessageBox(NULL, "ERROR: Wrong name of executable! Exiting...", "Error!", MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } /* Put the INI extension behind the filename */ while (*end!='.') end--; end++; *end='i'; end++; *end='n'; end++; *end='i'; end++; *end='\0'; /* Open the INI file */ setupini = fopen(filename, "r"); /* Does the INI file exist? */ if (setupini != NULL){ /* Yes, get the settings from the INI file */ while(fgets(line, MAX_LENGTH, setupini) != NULL) { if (strncmp(line, "#", 1) && strlen(line) > 1) { /* Is there an executable string? */ if (!strncmp(line, "EXECUTE", 7)){ strtok(line, "="); exe = strdup(strtok(NULL, "=")); /* Remove spaces */ while (*exe == 32) exe++; } /* Should we create PID? */ if (!strncmp(line, "CREATE_STOP_INI", 15)){ strtok(line, "="); flag = strdup(strtok(NULL, "=")); /* Remove spaces */ while (*flag == 32) flag++; } /* Name of the INI file */ if (!strncmp(line, "STOP_INI_NAME", 13)){ strtok(line, "="); ininame = strdup(strtok(NULL, "=")); /* Remove spaces and newlines */ while (*ininame == 32) ininame++; while (ininame[strlen(ininame) - 1] == 10 || ininame[strlen(ininame) - 1] == 13) ininame[strlen(ininame) - 1] = '\0'; } } } fclose(setupini); if (exe == NULL){ MessageBox(NULL, "ERROR: No executable string found! Exiting...", "Error!", MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } if (flag == NULL) flag = strdup("NO"); /* Trim any newline off the string */ if (exe[strlen(exe) - 1] == '\n' || exe[strlen(exe) - 1] == '\r') exe[strlen(exe) - 1] = '\0'; /* Setup startup flags */ si.dwFlags=STARTF_USESHOWWINDOW; /* Check the Operating System version */ ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi))) { osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if (! GetVersionEx ((OSVERSIONINFO *) &osvi)) exit(EXIT_FAILURE); } /* If windows9x/ME then hide */ if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { si.wShowWindow=SW_HIDE; } else { si.wShowWindow=SW_SHOW; } ret = CreateProcess (NULL, (char*)exe, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); /* Starting process had problems? */ if (ret == 0){ MessageBox(NULL, "ERROR: Could not launch your application!", (char*)exe, MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } /* If configured, save processID in external file */ if(!strncmp(flag, "YES", 3) || !strncmp(flag, "yes", 3) || !strncmp(flag, "1", 1) ){ pidini = fopen(ininame, "w"); if (fprintf(pidini, "%d", (int)pi.dwProcessId) < 0){ MessageBox(NULL, "WARNING: Could not write PID file!", "Warning!", MB_OK | MB_ICONWARNING); } fclose(pidini); } } /* The INI file does not exist? */ else { MessageBox(NULL, "ERROR: Cannot find the INI file!", "Error!", MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }