minor corrections by M.Taguchi
[reactos.git] / reactos / apps / tests / create-links / create-links.c
1 /*
2
3 compile via:
4 gcc -o create-links -D_WIN32_IE=0x400 create-links.c -lole32 -luuid -lshell32 -lshlwapi
5
6 Martin Fuchs, 27.12.2003
7
8 */
9
10 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
12
13 #include <shlobj.h>
14 #include <objidl.h>
15 #include <shlwapi.h>
16
17 #include <stdio.h>
18
19 HRESULT CreateShellLink(LPCSTR linkPath, LPCSTR cmd, LPCSTR arg, LPCSTR dir, LPCSTR iconPath, int icon_nr, LPCSTR comment)
20 {
21 IShellLinkA* psl;
22 IPersistFile* ppf;
23 WCHAR buffer[MAX_PATH];
24
25 HRESULT hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID*)&psl);
26
27 printf("creating shortcut file '%s' to %s...\n", linkPath, cmd);
28
29 if (SUCCEEDED(hr)) {
30 hr = psl->lpVtbl->SetPath(psl, cmd);
31
32 if (arg)
33 hr = psl->lpVtbl->SetArguments(psl, arg);
34
35 if (dir)
36 hr = psl->lpVtbl->SetWorkingDirectory(psl, dir);
37
38 if (iconPath)
39 hr = psl->lpVtbl->SetIconLocation(psl, iconPath, icon_nr);
40
41 if (comment)
42 hr = psl->lpVtbl->SetDescription(psl, comment);
43
44 hr = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
45
46 if (SUCCEEDED(hr)) {
47 MultiByteToWideChar(CP_ACP, 0, linkPath, -1, buffer, MAX_PATH);
48
49 hr = ppf->lpVtbl->Save(ppf, buffer, TRUE);
50
51 ppf->lpVtbl->Release(ppf);
52 }
53
54 psl->lpVtbl->Release(psl);
55 }
56
57 if (SUCCEEDED(hr))
58 printf("OK\n\n");
59 else
60 printf("error %08x\n\n", (int) hr);
61
62 return hr;
63 }
64
65
66 int main()
67 {
68 char path[MAX_PATH];
69 LPSTR p;
70
71 CoInitialize(NULL);
72
73 /* create some shortcuts in the start menu "programs" folder */
74 SHGetSpecialFolderPathA(0, path, CSIDL_PROGRAMS, TRUE);
75 p = PathAddBackslash(path);
76
77 strcpy(p, "start-cmd.lnk");
78 CreateShellLink(path, "cmd.exe", "", NULL, NULL, 0, "open console window");
79
80 strcpy(p, "start-winhello.lnk");
81 CreateShellLink(path, "winhello.exe", "", NULL, NULL, 0, "launch winhello");
82
83
84 /* create some shortcuts on the desktop */
85 SHGetSpecialFolderPathA(0, path, CSIDL_DESKTOP, TRUE);
86 p = PathAddBackslash(path);
87
88 strcpy(p, "start-wcmd.lnk");
89 CreateShellLink(path, "cmd.exe", "", NULL, NULL, 0, "open console window");
90
91 strcpy(p, "start-winemine.lnk");
92 CreateShellLink(path, "winemine.exe", "", NULL, NULL, 0, "launch winemine");
93
94 CoUninitialize();
95
96 return 0;
97 }