]> cygwin.com Git - cygwin-apps/setup.git/blob - mklink2.cc
2007-02-17 Brian Dessent <brian@dessent.net>
[cygwin-apps/setup.git] / mklink2.cc
1 #define CINTERFACE
2 #include <stdlib.h>
3 #include "win32.h"
4 #include "shlobj.h"
5 #include "mklink2.h"
6
7 #if 0
8 static const char *cvsid =
9 "\n%%% $Id$\n";
10 #endif
11
12 /* This part of the code must be in C because the C++ interface to COM
13 doesn't work. */
14
15 extern "C"
16 void
17 make_link_2 (char const *exepath, char const *args, char const *icon, char const *lname)
18 {
19 IShellLink *sl;
20 IPersistFile *pf;
21 WCHAR widepath[MAX_PATH];
22
23 CoCreateInstance (&CLSID_ShellLink, NULL,
24 CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID *) & sl);
25 sl->lpVtbl->QueryInterface (sl, &IID_IPersistFile, (void **) &pf);
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 }
37
38 #define SYMLINK_COOKIE "!<symlink>"
39
40 /* Predicate: file is not currently in existence.
41 * A file race can occur otherwise.
42 */
43 extern "C"
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 }
This page took 0.036953 seconds and 5 git commands to generate.