// MSWINSCK.OCX を登録するためのインストーラ /* 「プロジェクト」→「設定」を選び、「リンク」グループの中の 「オブジェクト/ライブラリモジュール」の欄に、 ole32.lib と version.lib と advapi32.lib を追加する必要があります。 */ #include #include #include #include int GetRegValue(char *clsid, char *filebuf) { // 0 : Error 1 : No Error PHKEY phKey; DWORD DataType; DWORD LenBuf; char clsbuf[1024]; unsigned char strbuf[MAX_PATH]; sprintf(clsbuf, "CLSID\\%s\\InprocServer32", clsid); //レジストリキーを開く if (RegOpenKeyEx(HKEY_CLASSES_ROOT, clsbuf, 0, KEY_QUERY_VALUE, (void **) &phKey) == ERROR_SUCCESS) { //バッファサイズとデータタイプを取得する if (RegQueryValueEx(phKey, "", 0, &DataType, 0, &LenBuf) == ERROR_SUCCESS) { if (DataType == REG_SZ) { //文字列キー値の取得 RegQueryValueEx(phKey, "", 0, &DataType, strbuf, &LenBuf); RegCloseKey(phKey); strcpy(filebuf, (char *) strbuf); return 1; } } //レジストリを閉じる RegCloseKey(phKey); } return 0; } int GetFileVersion(char *fn) { // 0 : Error 1 : No Error DWORD dummy, infosize, *q; UINT bufsize; char buf[1024], *textver, *p; if ((infosize = GetFileVersionInfoSize(fn, &dummy)) == 0) { return 0; } if ((p = (char *) malloc(infosize)) == NULL) return 0; GetFileVersionInfo(fn, 0, infosize, p); VerQueryValue(p, "\\VarFileInfo\\Translation", (void **) &q, &bufsize); sprintf(buf, "\\StringFileInfo\\%04X%04X\\FileVersion", LOWORD(*q), HIWORD(*q)); VerQueryValue(p, buf, (void**) &textver, &bufsize); printf(" %-12s (Version %s)\n" , fn, textver); free(p); return 1; } int RegistDll(char *pszDllName) { // 0 : Error 1 : No Error int iReturn = 0; HRESULT (STDAPICALLTYPE * lpDllEntryPoint)(void); char Message[1024]; strcpy(Message, pszDllName); if (FAILED(OleInitialize(NULL))) { puts("OLEの初期化に失敗しました。"); return 0; } SetErrorMode(SEM_FAILCRITICALERRORS); HINSTANCE hLib = LoadLibraryEx(pszDllName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); if (hLib < (HINSTANCE) HINSTANCE_ERROR) { printf("%s をロードできません。\n", Message); goto CleanupOle; } (FARPROC&)lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer"); if (lpDllEntryPoint == NULL) { printf("%s のエントリーポイントが見つかりません。\n", Message); goto CleanupLibrary; } if (FAILED((*lpDllEntryPoint)())) { printf("%s の登録に失敗しました。\n", Message); goto CleanupLibrary; } printf("%s の登録に成功しました。\n", Message); iReturn = 1; CleanupLibrary: FreeLibrary(hLib); CleanupOle: OleUninitialize(); return iReturn; } char InstallFile[] = "MSWINSCK.OCX"; char InstallFileClassID[] = "{248DD896-BB45-11CF-9ABC-0080C7E7B78D}"; int main(int argc, char *argv[]) { char DestFile[MAX_PATH], SourceFile[MAX_PATH], CurrentFile[MAX_PATH]; int i; DWORD dummy; bool OverWrite = FALSE; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; GetModuleFileName(NULL, SourceFile, MAX_PATH); _splitpath(SourceFile, drive, dir, fname, ext); strcpy(SourceFile, drive); strcat(SourceFile, dir); strcat(SourceFile, InstallFile); if (GetFileVersionInfoSize(SourceFile, &dummy) == 0) { printf("ファイル %s が見つかりません。\n", SourceFile); return 1; } if (GetRegValue(InstallFileClassID, CurrentFile)) { if (GetFileVersionInfoSize(CurrentFile, &dummy)) { printf("既に登録されているファイル\n"); GetFileVersion(CurrentFile); printf("コピー元のファイル\n"); GetFileVersion(SourceFile); printf("ファイル %s をコピーして登録しますか?(Y/N)", SourceFile); while (1) { i = getchar(); if (i == 'Y' || i == 'y') break; else if (i == 'N' || i == 'n') return 0; } OverWrite = TRUE; } } GetSystemDirectory(DestFile, MAX_PATH); _splitpath(DestFile, drive, dir, fname, ext); if (fname[0] != '\0' || ext[0] != '\0') strcat(DestFile, "\\"); strcat(DestFile, InstallFile); if (stricmp(SourceFile, DestFile) == 0) goto NoCopy; if (CopyFile(SourceFile, DestFile, false)) goto CopyOK; GetWindowsDirectory(DestFile, MAX_PATH); _splitpath(DestFile, drive, dir, fname, ext); if (fname[0] != '\0' || ext[0] != '\0') strcat(DestFile, "\\"); strcat(DestFile, InstallFile); if (stricmp(SourceFile, DestFile) == 0) goto NoCopy; if (CopyFile(SourceFile, DestFile, false)) goto CopyOK; if (OverWrite) { strcpy(DestFile, CurrentFile); if (stricmp(SourceFile, DestFile) == 0) goto NoCopy; if (CopyFile(SourceFile, DestFile, false)) goto CopyOK; } printf("%s をコピーできません。\n", SourceFile); return 1; CopyOK: ; printf("%s を %s へコピーしました。\n", SourceFile, DestFile); NoCopy: ; return (RegistDll(DestFile) == 0); }