📄 jnlp2msi.cpp
字号:
/* * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. * * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ // jnlp2msi.cpp : Defines the entry point for the console application.#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <direct.h>#include <stdio.h>#include <io.h>#include <sys/stat.h>#include <sys/types.h>#include <windows.h>//////////////////////////////////////////////////////////////////////////////#define BUFFER_SIZE 2048#define OPT_SIZE 100#define MY_FULL_VERSION "0.9"#define MY_RELEASE_NO "1"// Java executable file location indicator#define JAVA_EXE_IN_SYSTEM 1#define JAVA_EXE_IN_SYSTEM32 2#define JAVA_EXE_IN_JAVA_HOME 3// Enum for each optionsenum opts{ResourceDir = 0, PackageName, OutputDir, Version, Release, LicenseDir, BannerJpgFile, PanelJpgFile, MSSDKPath, EnableShortcut, EnableAssociation, EnableSystemCache, Echo, Showversion, Help, OPTS_COUNT};//acronym option indicator, such as "-pn" for packagenamechar sOpts[OPTS_COUNT][OPT_SIZE] = {0};//full length option indicator, such as "-packagename" for packagenamechar lOpts[OPTS_COUNT][OPT_SIZE] = {0};//Option names, such as "PackageName"char optNames[OPTS_COUNT][BUFFER_SIZE] = {0};//Option values, such as "c:\Program files\Microsoft SDK"char optValues[OPTS_COUNT][BUFFER_SIZE] = {0};//The given JNLP File pathchar JnlpFile [BUFFER_SIZE] = {0};//Class to be used to generate the MSI filechar ClassName[] = "org.jdesktop.jdic.packager.Jnlp2Msi";//Whether to print the java process cmd on terminalbool echoJavaCmd = false;//Whether to print the current jnlp2msi version info.bool echoVersion = false;//Whether to print the help info.bool echoHelp = false;//Whether the MS SDK Path has been specified as an env variablebool mssdkpathSetByEnv = false;//Whether user has specified the mssdkpath in the cmd linebool mssdkpathSetByUser = false;//The MSSDKDir env variable valuechar mssdkEnvVar[BUFFER_SIZE] = "\0";///////////////////////////////////////////////////////////////////////////////** * Init one option relevant arrays */void initOpt(int optIndex, char* loptValue, char* uoptValue, char* nameValue){ strcpy(sOpts[optIndex], loptValue); strcpy(lOpts[optIndex], uoptValue); strcpy(optNames[optIndex], nameValue); strcpy(optValues[optIndex], "\0");}///////////////////////////////////////////////////////////////////////////////** * Initialize arrays: lOpts, uOpts, optNames; */void initOpts(){ // optIndex acronym fullname -D name initOpt(ResourceDir, "-rd", "-resourcedir", "ResourceDir"); initOpt(PackageName, "-pn", "-packagename", "PackageName"); initOpt(OutputDir, "-od", "-outputdir", "OutputDir"); initOpt(Version, "-v", "-version", "Version"); initOpt(Release, "-r", "-release", "Release"); initOpt(LicenseDir, "-ld", "-licensedir", "LicenseDir"); initOpt(BannerJpgFile, "-bjf", "-bannerjpgfile", "BannerJpgFile"); initOpt(PanelJpgFile, "-pjf", "-paneljpgfile", "PanelJpgFile"); initOpt(MSSDKPath, "-msd", "-mssdkdir", "MSSDKDir"); initOpt(EnableShortcut, "-es", "-enableshortcut", "EnableShortcut"); initOpt(EnableAssociation, "-ea", "-enableassociation", "EnableAssociation"); initOpt(EnableSystemCache, "-esc", "-enablesystemcache", "EnableSystemCache"); initOpt(Echo, "-e", "-echo", ""); initOpt(Showversion, "-sv", "-showversion", ""); initOpt(Help, "-?", "-help", "");}///////////////////////////////////////////////////////////////////////////////* * Format the path str, replace any occurance of \ with \\. */void formatPathStr(char* originalPathStr, char* formatPathStr){ // delete all leading spaces in originalPathStr while (' ' == *originalPathStr) { originalPathStr++; } // delete all trailing spaces in originalPathStr char* pCur = originalPathStr + strlen(originalPathStr) -1; while (' ' == *pCur) { *pCur = 0; pCur--; } for (int i = 0; i < strlen(originalPathStr); i++) { if ((originalPathStr[i] == '\\') && (originalPathStr[i + 1] != '\\')) { strcat(formatPathStr, "\\\\"); } else { formatPathStr[strlen(formatPathStr)] = originalPathStr[i]; } }}///////////////////////////////////////////////////////////////////////////////** * Process option parametes and generate the target value */int optprocess(char* lopt, char* uopt, char* paraName, char* param, char* argv[], int& iLeft){ if (!strcmp(lopt, argv[iLeft]) || !strcmp(uopt, argv[iLeft])) { int argLen = strlen(argv[iLeft]); if (!strcmp(argv[iLeft], sOpts[Echo]) || !strcmp(argv[iLeft], lOpts[Echo])) { //For echo case, we just set echoJavaCmd to be true. echoJavaCmd = true; } else if (!strcmp(argv[iLeft], sOpts[Showversion]) || !strcmp(argv[iLeft], lOpts[Showversion])) { //For showversion info echoVersion = true; } else if (!strcmp(argv[iLeft], sOpts[Help]) || !strcmp(argv[iLeft], lOpts[Help])) { //For showhelp info echoHelp = true; } else if ((!strcmp(argv[iLeft], sOpts[EnableShortcut]) || !strcmp(argv[iLeft], lOpts[EnableShortcut])) || (!strcmp(argv[iLeft], sOpts[EnableAssociation]) || !strcmp(argv[iLeft], lOpts[EnableAssociation])) || (!strcmp(argv[iLeft], sOpts[EnableSystemCache]) || !strcmp(argv[iLeft], lOpts[EnableSystemCache]))) { //For EnableShortcut, EnableAssociation, EnableSystemCache sprintf(param, "%s%s%s%s", "-D", paraName, "=", "true"); } else if ((!strcmp(argv[iLeft], sOpts[ResourceDir]) || !strcmp(argv[iLeft], lOpts[ResourceDir])) || (!strcmp(argv[iLeft], sOpts[LicenseDir]) || !strcmp(argv[iLeft], lOpts[LicenseDir])) || (!strcmp(argv[iLeft], sOpts[BannerJpgFile]) || !strcmp(argv[iLeft], lOpts[BannerJpgFile])) || (!strcmp(argv[iLeft], sOpts[PanelJpgFile]) || !strcmp(argv[iLeft], lOpts[PanelJpgFile])) || (!strcmp(argv[iLeft], sOpts[MSSDKPath]) || !strcmp(argv[iLeft], lOpts[MSSDKPath]))) { //For param ResourceDir, LicenseDir, BannerJpgFile, PanelJpgFile & MSSDKPath //we need to set param as -DMSSDKPath="... ..." if (!strcmp(argv[iLeft], sOpts[MSSDKPath]) || !strcmp(argv[iLeft], lOpts[MSSDKPath])) { mssdkpathSetByUser = true; } char * argvIndex = argv[++iLeft]; char formatArg[BUFFER_SIZE] = "\0"; formatPathStr(argvIndex, formatArg); sprintf(param, "-D%s=\"%s\"", paraName, formatArg); } else if ((!strcmp(argv[iLeft], sOpts[PackageName]) || !strcmp(argv[iLeft], lOpts[PackageName])) || (!strcmp(argv[iLeft], sOpts[OutputDir]) || !strcmp(argv[iLeft], lOpts[OutputDir])) || (!strcmp(argv[iLeft], sOpts[Version]) || !strcmp(argv[iLeft], lOpts[Version])) || (!strcmp(argv[iLeft], sOpts[Release]) || !strcmp(argv[iLeft], lOpts[Release]))) { //For PackageName, OutputDir, Version, Release, no quotes. //we just set param as -DPackageName=... ... sprintf(param, "%s%s%s%s", "-D", paraName, "=", argv[++iLeft]); } iLeft ++; return 1; } //return 0: no param gets handled in this process return 0;}///////////////////////////////////////////////////////////////////////////////** * Evaluate if the the JDK version is 1.5.0+ */BOOL isJavaVersionOK(char* pFirstPath, char* pMidPath){ // if no JAVA_HOME, exit char pJavaPath[BUFFER_SIZE] = {0}; strcat(pJavaPath, pFirstPath); strcat(pJavaPath, "\\"); strcat(pJavaPath, pMidPath); strcat(pJavaPath, "\\java.exe"); // We determine the version through "java -fullversion" // TCHAR szCommandLine[BUFFER_SIZE]; wsprintf(szCommandLine, "%s -fullversion", pJavaPath); HANDLE hread[3], hwrite[3]; SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = 0; sa.bInheritHandle = TRUE; memset(hread, 0, sizeof(hread)); memset(hwrite, 0, sizeof(hwrite)); // Create pipe to read stdin/stdout/stderr from another process // if (!(CreatePipe(&hread[0], &hwrite[0], &sa, 1024) && CreatePipe(&hread[1], &hwrite[1], &sa, 1024) && CreatePipe(&hread[2], &hwrite[2], &sa, 1024))) { if (hread[0] != NULL) CloseHandle(hread[0]); if (hread[1] != NULL) CloseHandle(hread[1]); if (hread[2] != NULL) CloseHandle(hread[2]); if (hwrite[0] != NULL) CloseHandle(hwrite[0]); if (hwrite[1] != NULL) CloseHandle(hwrite[1]); if (hwrite[2] != NULL) CloseHandle(hwrite[2]); } STARTUPINFO si; ::ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = hread[0]; si.hStdOutput = hwrite[1]; si.hStdError = hwrite[2]; SetHandleInformation(hwrite[0], HANDLE_FLAG_INHERIT, FALSE); SetHandleInformation(hread[1], HANDLE_FLAG_INHERIT, FALSE); SetHandleInformation(hread[2], HANDLE_FLAG_INHERIT, FALSE); PROCESS_INFORMATION pi; ::ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); // Run "java -fullversion" // BOOL ret = ::CreateProcess(NULL, szCommandLine, NULL, NULL, TRUE, CREATE_NO_WINDOW | DETACHED_PROCESS, NULL, NULL, &si, &pi); if (ret) { char szOutput[256]; DWORD dwByteRead = 0; // Read output ReadFile(hread[2], szOutput, 256, &dwByteRead, NULL); // Extract version info // const char* versionInfo = strstr(szOutput, "\""); const char* milestoneInfo = strstr(versionInfo + 1, "-"); const char* endInfo; char lpszUpdVersionInfo[BUFFER_SIZE] = {0}; char lpszFullVersionInfo[BUFFER_SIZE] = {0}; char lpszVersionInfo[BUFFER_SIZE] = {0}; if (milestoneInfo == NULL) { endInfo = strstr(versionInfo+1, "\""); strncpy(lpszUpdVersionInfo, versionInfo + 1, endInfo - versionInfo - 1); lpszUpdVersionInfo[endInfo - versionInfo - 1] = '\0'; } else { endInfo = strstr(milestoneInfo+1, "\""); strncpy(lpszUpdVersionInfo, versionInfo + 1, milestoneInfo - versionInfo - 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -