📄 msiactions.cpp
字号:
/** * Generate a unique tempory directory name * * @param baseDir Specify the base directory (in) * @param tempDir String buffer containing the generated dir name (in, out) * * @return 1 if succeed */int GetTempDirName(char* pBaseDir, char* pTempDir){ char dirPrefix[] = "javaws"; if (GetTempFileName(pBaseDir, dirPrefix, 0, pTempDir) != 0) { DeleteFile(pTempDir); return 1; } return 0;}/** * Extracts the jar file from the MSI database binary field * * @param hModule Specify the handle for the MSI database */void ExtractBinaryFile (MSIHANDLE hModule, const char* pFieldName, const char* pDstFileName) { char cSelectSql[MAX_PATH_SIZE] = {0}; MSIHANDLE hRecord, hView, hDatabase; // get hView & hDatabase hDatabase = MsiGetActiveDatabase(hModule); sprintf(cSelectSql, "select * from Binary where `Name`='%s'", pFieldName); MsiDatabaseOpenView(hDatabase, cSelectSql, &hView); MsiViewExecute(hView, NULL); char szTemp[256] = {0}; DWORD dwLength = 256; if (MsiViewFetch(hView, &hRecord) != ERROR_SUCCESS) { return; } MsiRecordGetString(hRecord, 1, szTemp, &dwLength); if (strncmp(szTemp, pFieldName, strlen(pFieldName))) { return; } // write into installer.jar #define BUFFERSIZE 1024 char szBuffer[BUFFERSIZE] = {0}; DWORD cbBuf = BUFFERSIZE; DWORD countWrite = 0; FILE* fp = NULL; fp = fopen(pDstFileName, "wb+"); if (NULL == fp) { return; } do { if (MsiRecordReadStream(hRecord, 2, szBuffer, &cbBuf) !=ERROR_SUCCESS) break; // error countWrite = fwrite(szBuffer, 1, cbBuf, fp); } while (countWrite == BUFFERSIZE); fclose(fp); // close all handles MsiCloseHandle(hRecord); MsiViewClose(hView); MsiCloseHandle(hView); MsiCloseHandle(hDatabase);}void GetMsiProperty (MSIHANDLE hModule, char* propertyValue, char* propertyName) { if(!propertyName) return; MSIHANDLE hRecord, hView, hDatabase; // get hView & hDatabase hDatabase = MsiGetActiveDatabase(hModule); char pSelectSql[256] = {0}; sprintf(pSelectSql, "select * from Property where `Property`='%s'", propertyName); MsiDatabaseOpenView(hDatabase, pSelectSql, &hView); MsiViewExecute(hView, NULL); DWORD dwLength = 256; if (MsiViewFetch(hView, &hRecord) != ERROR_SUCCESS) return; MsiRecordGetString(hRecord, 2, propertyValue, &dwLength); // close all handles MsiCloseHandle(hRecord); MsiViewClose(hView); MsiCloseHandle(hView); MsiCloseHandle(hDatabase);}/** * Gets the javaws home path, e.g. c:\programe files\java\j2re1.5.0\bin */unsigned int GetJavawsHome(){ char KEY_JAVAWS[] = "SOFTWARE\\JavaSoft\\Java Web Start"; char KEY_JAVAWS_CURRENT_VERSION[1024] = {0}; char VALUE_JAVAWS_CURRNET_VERSION[] = "CurrentVersion"; char VALUE_JAVAWS_HOME[] = "Home"; char JAVAWS_VERSION[128] = {0}; HKEY hKey; DWORD dwType; DWORD cbData; //Open the key HKEY_LOCAL_MACHINE\\SOFTWAER\JavaSoft\\Java Web Start if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEY_JAVAWS, 0, KEY_READ, &hKey) == ERROR_SUCCESS) { cbData = 128; //Get the value of CurrentVersion if (RegQueryValueEx(hKey, (LPCTSTR)VALUE_JAVAWS_CURRNET_VERSION, NULL, &dwType, (LPBYTE)&JAVAWS_VERSION, &cbData) == ERROR_SUCCESS) { //Open the key HKEY_LOCAL_MACHINE\\SOFTWAER\JavaSoft\\Java Web Start\\1.5.0 sprintf(KEY_JAVAWS_CURRENT_VERSION, "%s\\%s", KEY_JAVAWS, JAVAWS_VERSION); RegCloseKey(hKey); if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEY_JAVAWS_CURRENT_VERSION, 0, KEY_READ, &hKey) == ERROR_SUCCESS) { //Get the value of the current javaws home cbData = MAX_PATH_SIZE; if (RegQueryValueEx(hKey, (LPCTSTR)VALUE_JAVAWS_HOME, NULL, &dwType, (LPBYTE)&g_JAVAWS_HOME, &cbData) == ERROR_SUCCESS) { RegCloseKey(hKey); return 1; } } } } return 0;}/** * Initialize the global static environment variables * * @param none * @return true if succeed */ BOOL InitGlobal(){ //g_TEMP_BASE_PATH: Get the system temporary path DWORD dwLenInstallerPath = MAX_PATH_SIZE; GetTempPath(dwLenInstallerPath, (LPTSTR)g_TEMP_BASE_PATH); //g_TEMP_JAVAWS_PATH: Generate a unique temporary directory GetTempDirName(g_TEMP_BASE_PATH, g_TEMP_JAVAWS_PATH); CreateDirectory(g_TEMP_JAVAWS_PATH, NULL); //Get Javaws home path if(GetJavawsHome() == 0) { return false; }; //g_TEMP_INSTALLER_JAR: Get the full path name for the jar file to be generated sprintf(g_TEMP_INSTALLER_JAR, "%s\\%s", g_TEMP_JAVAWS_PATH, "installer.jar"); //g_TEMP_JAVAWS_URL: Get the url mode of installer.jar's path char tempUrl[MAX_PATH_SIZE] = {0}; int iNumber = 0; while (g_TEMP_JAVAWS_PATH[iNumber]) { for(int i=0; i<sizeof(g_TEMP_JAVAWS_PATH); i++) { if(g_TEMP_JAVAWS_PATH[i]=='\\') { tempUrl[i] = '/'; } else { tempUrl[i] = g_TEMP_JAVAWS_PATH[i]; } } iNumber++; } sprintf(g_TEMP_JAVAWS_URL, "file:///%s", tempUrl); return true;}unsigned int __stdcall InstallAction ( MSIHANDLE hModule ){ // store the original path char ORIG_PATH[MAX_PATH_SIZE] = {0}; // c:\somewhere GetCurrentDirectory(MAX_PATH_SIZE, ORIG_PATH); if (InitGlobal() == false) { return 0; } ExtractBinaryFile(hModule, "JarPack", g_TEMP_INSTALLER_JAR); do_extract(g_TEMP_INSTALLER_JAR); // get JnlpFileName Property from msi Property table char jnlpFileName[256] = {0}; GetMsiProperty(hModule, jnlpFileName, "JnlpFileName"); char TEMP_JNLP_FILE[MAX_PATH_SIZE] = {0}; // C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\[javaws4c1.tmp]\draw.jnlp sprintf(TEMP_JNLP_FILE, "%s\\%s", g_TEMP_JAVAWS_PATH, jnlpFileName); // get Shortcut Property from msi Property table char shortcut[256] = {0}; GetMsiProperty(hModule, shortcut, "Shortcut"); if(shortcut[0] != '0') { strncpy(g_SHORTCUT, "-shortcut", strlen("-shortcut")); } // get CacheType Property from msi Property table char cachetype[256] = {0}; GetMsiProperty(hModule, cachetype, "CacheType"); if(0 == stricmp(cachetype, "system")) { strncpy(g_CACHE_TYPE, "-system", strlen("-system")); } // get Association Property from msi Property table char association[256] = {0}; GetMsiProperty(hModule, association, "Association"); if(association[0] != '0') { strncpy(g_ASSOCIATION, "-association", strlen("-association")); } // install the jnlp software STARTUPINFO stinfo = {0}; //info of the window stinfo.cb = sizeof(STARTUPINFO); PROCESS_INFORMATION procinfo; //info of the process char CREATEPROCESS[MAX_PATH_SIZE] = {0}; // javaws -silent -import -system -codebase %s sprintf(CREATEPROCESS, "%s\\javaws %s -silent -import %s %s -codebase \"%s\" %s", g_JAVAWS_HOME, g_CACHE_TYPE, g_SHORTCUT, g_ASSOCIATION, g_TEMP_JAVAWS_URL, TEMP_JNLP_FILE); if(!CreateProcess(NULL, CREATEPROCESS, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &stinfo, &procinfo)) { return 0; } // wait for the end of the process WaitForSingleObject(procinfo.hProcess, INFINITE); CloseHandle(procinfo.hProcess); CloseHandle(procinfo.hThread); // restore to original path & Remove the generated temporary directory SetCurrentDirectory(ORIG_PATH); RemoveDir(g_TEMP_JAVAWS_PATH); return ERROR_SUCCESS;}unsigned int __stdcall UninstallAction ( MSIHANDLE hModule ){ //Get Javaws home path if(GetJavawsHome() == 0) { return 0; } char CREATEPROCESS[MAX_PATH_SIZE] = {0}; // javaws -silent -uninstall -system http://java.sun.com/products/javawebstart/apps/draw.jnlp char uninstallInfo[256] = {0}; GetMsiProperty(hModule, uninstallInfo, "UninstallInfo"); // get CacheType Property from msi Property table memset(g_CACHE_TYPE, 0, sizeof(g_CACHE_TYPE)); char cachetype[256] = {0}; GetMsiProperty(hModule, cachetype, "CacheType"); if(0 == stricmp(cachetype, "system")) { strncpy(g_CACHE_TYPE, "-system", strlen("-system")); } // install the jnlp software STARTUPINFO stinfo = {0}; //info of the window stinfo.cb = sizeof(STARTUPINFO); PROCESS_INFORMATION procinfo; //info of the process sprintf(CREATEPROCESS, "%s\\javaws %s -silent -uninstall %s", g_JAVAWS_HOME, g_CACHE_TYPE, uninstallInfo); if(!CreateProcess(NULL, CREATEPROCESS, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &stinfo, &procinfo)) { return 0; } // wait for the end of the first process WaitForSingleObject (procinfo.hProcess, INFINITE); CloseHandle(procinfo.hProcess); CloseHandle(procinfo.hThread); return ERROR_SUCCESS;}bool APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -