]>
Commit | Line | Data |
---|---|---|
51ebb760 | 1 | #define CINTERFACE |
b24c88b3 | 2 | #include <stdlib.h> |
904d24fe DD |
3 | #include "win32.h" |
4 | #include "shlobj.h" | |
b24c88b3 | 5 | #include "mklink2.h" |
904d24fe | 6 | |
b24c88b3 RC |
7 | #if 0 |
8 | static const char *cvsid = | |
9 | "\n%%% $Id$\n"; | |
10 | #endif | |
8507f105 | 11 | |
904d24fe DD |
12 | /* This part of the code must be in C because the C++ interface to COM |
13 | doesn't work. */ | |
14 | ||
51ebb760 | 15 | extern "C" |
904d24fe | 16 | void |
3c054baf | 17 | make_link_2 (char const *exepath, char const *args, char const *icon, char const *lname) |
904d24fe DD |
18 | { |
19 | IShellLink *sl; | |
20 | IPersistFile *pf; | |
b24c88b3 | 21 | WCHAR widepath[_MAX_PATH]; |
904d24fe | 22 | |
931f2755 RC |
23 | CoCreateInstance (&CLSID_ShellLink, NULL, |
24 | CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID *) & sl); | |
25 | sl->lpVtbl->QueryInterface (sl, &IID_IPersistFile, (void **) &pf); | |
904d24fe DD |
26 | |
27 | sl->lpVtbl->SetPath (sl, exepath); | |
28 | sl->lpVtbl->SetArguments (sl, args); | |
29 | sl->lpVtbl->SetIconLocation (sl, icon, 0); | |
30 | ||
31 | MultiByteToWideChar (CP_ACP, 0, lname, -1, widepath, _MAX_PATH); | |
32 | pf->lpVtbl->Save (pf, widepath, TRUE); | |
33 | ||
34 | pf->lpVtbl->Release (pf); | |
35 | sl->lpVtbl->Release (sl); | |
36 | } | |
b24c88b3 RC |
37 | |
38 | #define SYMLINK_COOKIE "!<symlink>" | |
39 | ||
40 | /* Predicate: file is not currently in existence. | |
41 | * A file race can occur otherwise. | |
42 | */ | |
51ebb760 | 43 | extern "C" |
b24c88b3 RC |
44 | int |
45 | mkcygsymlink (const char *from, const char *to) | |
46 | { | |
47 | char buf[512]; | |
48 | unsigned long w; | |
49 | HANDLE h = CreateFileA (from, GENERIC_WRITE, 0, 0, CREATE_NEW, | |
50 | FILE_ATTRIBUTE_NORMAL, 0); | |
51 | if (h == INVALID_HANDLE_VALUE) | |
52 | return 1; | |
53 | strcpy (buf, SYMLINK_COOKIE); | |
54 | strcat (buf, to); | |
55 | if (WriteFile (h, buf, strlen (buf) + 1, &w, NULL)) | |
56 | { | |
57 | CloseHandle (h); | |
58 | SetFileAttributesA (from, FILE_ATTRIBUTE_SYSTEM); | |
59 | return 0; | |
60 | } | |
61 | CloseHandle (h); | |
62 | DeleteFileA (from); | |
63 | return 1; | |
64 | } |