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