123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- #define _WIN32_IE 0x400
- #define COBJMACROS 1
- #include <windows.h>
- #include <shlobj.h>
- #include <ddeml.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <malloc.h>
- HDDEDATA CALLBACK
- DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
- HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
- DWORD dwData1, DWORD dwData2)
- {
- return ((HDDEDATA) NULL);
- }
- #define DdeCommand(str) \
- DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
- CF_TEXT, XTYP_EXECUTE, 30000, NULL)
- #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
- #define REG_GTK "SOFTWARE\\GTK\\2.0"
- #define REG_APP_PATH \
- "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
- #define REG_RUNEMACS_PATH \
- "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\runemacs.exe"
- static struct entry
- {
- const char *name;
- const char *value;
- }
- env_vars[] =
- {
- {"emacs_dir", NULL},
- {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
- {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
- {"EMACSDATA", "%emacs_dir%/etc"},
- {"EMACSPATH", "%emacs_dir%/bin"},
-
-
- {"EMACSDOC", "%emacs_dir%/etc"},
- {"TERM", "cmd"}
- };
- BOOL
- add_registry (const char *path)
- {
- HKEY hrootkey = NULL;
- int i;
- BOOL ok = TRUE;
- DWORD size;
-
- if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
- REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
- &hrootkey, NULL) == ERROR_SUCCESS)
- {
- int len;
- char *emacs_path;
- HKEY gtk_key = NULL;
- len = strlen (path) + 15;
- emacs_path = (char *) alloca (len);
- sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
- RegSetValueEx (hrootkey, NULL, 0, REG_EXPAND_SZ, emacs_path, len);
-
- if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_GTK, REG_OPTION_NON_VOLATILE,
- KEY_READ, >k_key) == ERROR_SUCCESS)
- {
- if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
- NULL, &size) == ERROR_SUCCESS)
- {
- char *gtk_path = (char *) alloca (size);
- if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
- gtk_path, &size) == ERROR_SUCCESS)
- {
-
- char *dll_paths;
- HKEY runemacs_key = NULL;
- len = strlen (path) + 5 + size;
- dll_paths = (char *) alloca (size + strlen (path) + 1);
- sprintf (dll_paths, "%s\\bin;%s", path, gtk_path);
- RegSetValueEx (hrootkey, "Path", 0, REG_EXPAND_SZ,
- dll_paths, len);
-
- if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_RUNEMACS_PATH,
- 0, "", REG_OPTION_NON_VOLATILE,
- KEY_WRITE, NULL, &runemacs_key, NULL)
- == ERROR_SUCCESS)
- {
- RegSetValueEx (runemacs_key, "Path", 0, REG_EXPAND_SZ,
- dll_paths, len);
- RegCloseKey (runemacs_key);
- }
- }
- }
- RegCloseKey (gtk_key);
- }
- RegCloseKey (hrootkey);
- }
-
-
- if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT,
- REG_OPTION_NON_VOLATILE,
- KEY_WRITE, &hrootkey) != ERROR_SUCCESS
- && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT,
- REG_OPTION_NON_VOLATILE,
- KEY_WRITE, &hrootkey) != ERROR_SUCCESS)
- {
- return FALSE;
- }
- for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
- {
- const char * value = env_vars[i].value ? env_vars[i].value : path;
- if (RegSetValueEx (hrootkey, env_vars[i].name,
- 0, REG_EXPAND_SZ,
- value, lstrlen (value) + 1) != ERROR_SUCCESS)
- ok = FALSE;
- }
- RegCloseKey (hrootkey);
- return (ok);
- }
- int
- main (int argc, char *argv[])
- {
- char start_folder[MAX_PATH + 1];
- int shortcuts_created = 0;
- int com_available = 1;
- char modname[MAX_PATH];
- const char *prog_name;
- const char *emacs_path;
- char *p;
- int quiet = 0;
- HRESULT result;
- IShellLinkA *shortcut;
-
- if (argc > 1
- && (argv[1][0] == '/' || argv[1][0] == '-')
- && argv[1][1] == 'q')
- {
- quiet = 1;
- --argc;
- ++argv;
- }
- if (argc > 1)
- emacs_path = argv[1];
- else
- {
- if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
- (p = strrchr (modname, '\\')) == NULL)
- {
- fprintf (stderr, "fatal error");
- exit (1);
- }
- *p = 0;
-
- if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
- {
- *p = 0;
- emacs_path = modname;
- }
- else
- {
- fprintf (stderr, "usage: addpm emacs_path\n");
- exit (1);
- }
-
- if (!quiet)
- {
- int result;
- char msg[ MAX_PATH ];
- sprintf (msg, "Install Emacs at %s?\n", emacs_path);
- result = MessageBox (NULL, msg, "Install Emacs",
- MB_OKCANCEL | MB_ICONQUESTION);
- if (result != IDOK)
- {
- fprintf (stderr, "Install canceled\n");
- exit (1);
- }
- }
- }
- add_registry (emacs_path);
- prog_name = "runemacs.exe";
-
- if (!SUCCEEDED (CoInitialize (NULL))
- || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
- CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
- (void **) &shortcut)))
- {
- com_available = 0;
- }
- if (com_available
- && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
- {
- if (strlen (start_folder) < (MAX_PATH - 20))
- {
- strcat (start_folder, "\\Gnu Emacs");
- if (CreateDirectory (start_folder, NULL)
- || GetLastError () == ERROR_ALREADY_EXISTS)
- {
- char full_emacs_path[MAX_PATH + 1];
- IPersistFile *lnk;
- strcat (start_folder, "\\Emacs.lnk");
- sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
- IShellLinkA_SetPath (shortcut, full_emacs_path);
- IShellLinkA_SetDescription (shortcut, "GNU Emacs");
- result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
- (void **) &lnk);
- if (SUCCEEDED (result))
- {
- wchar_t unicode_path[MAX_PATH];
- MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
- unicode_path, MAX_PATH);
- if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
- shortcuts_created = 1;
- IPersistFile_Release (lnk);
- }
- }
- }
- }
- if (!shortcuts_created && com_available
- && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
- {
-
- if (strlen (start_folder) < (MAX_PATH - 20))
- {
- strcat (start_folder, "\\Gnu Emacs");
- if (CreateDirectory (start_folder, NULL)
- || GetLastError () == ERROR_ALREADY_EXISTS)
- {
- char full_emacs_path[MAX_PATH + 1];
- IPersistFile *lnk;
- strcat (start_folder, "\\Emacs.lnk");
- sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
- IShellLinkA_SetPath (shortcut, full_emacs_path);
- IShellLinkA_SetDescription (shortcut, "GNU Emacs");
- result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
- (void **) &lnk);
- if (SUCCEEDED (result))
- {
- wchar_t unicode_path[MAX_PATH];
- MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
- unicode_path, MAX_PATH);
- if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
- shortcuts_created = 1;
- IPersistFile_Release (lnk);
- }
- }
- }
- }
- if (com_available)
- IShellLinkA_Release (shortcut);
-
- CoUninitialize ();
-
- if (!shortcuts_created)
- {
- DWORD dde = 0;
- HCONV conversation;
- HSZ progman;
- char add_item[MAX_PATH*2 + 100];
- DdeInitialize (&dde, (PFNCALLBACK) DdeCallback, APPCMD_CLIENTONLY, 0);
- progman = DdeCreateStringHandle (dde, "PROGMAN", CP_WINANSI);
- conversation = DdeConnect (dde, progman, progman, NULL);
- if (conversation)
- {
- DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
- DdeCommand ("[ReplaceItem (Emacs)]");
- sprintf (add_item, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
- emacs_path, prog_name);
- DdeCommand (add_item);
- DdeDisconnect (conversation);
- }
- DdeFreeStringHandle (dde, progman);
- DdeUninitialize (dde);
- }
- return 0;
- }
|