1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*

	compile via:
	gcc -o create-links -D_WIN32_IE=0x400 create-links.c -lole32 -luuid -lshell32 -lshlwapi

	Martin Fuchs, 27.12.2003

*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <shlobj.h>
#include <objidl.h>
#include <shlwapi.h>

#include <stdio.h>

HRESULT CreateShellLink(LPCSTR linkPath, LPCSTR cmd, LPCSTR arg, LPCSTR dir, LPCSTR iconPath, int icon_nr, LPCSTR comment)
{
	IShellLinkA* psl;
	IPersistFile* ppf;
	WCHAR buffer[MAX_PATH];

	HRESULT hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID*)&psl);

	printf("creating shortcut file '%s' to %s...\n", linkPath, cmd);

	if (SUCCEEDED(hr)) {
		hr = psl->lpVtbl->SetPath(psl, cmd);<--- hr is assigned

		if (arg)
			hr = psl->lpVtbl->SetArguments(psl, arg);<--- hr is assigned

		if (dir)
			hr = psl->lpVtbl->SetWorkingDirectory(psl, dir);<--- hr is assigned

		if (iconPath)
			hr = psl->lpVtbl->SetIconLocation(psl, iconPath, icon_nr);<--- hr is assigned

		if (comment)
			hr = psl->lpVtbl->SetDescription(psl, comment);<--- hr is assigned

		hr = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);<--- hr is overwritten<--- hr is overwritten<--- hr is overwritten<--- hr is overwritten<--- hr is overwritten

		if (SUCCEEDED(hr)) {
			MultiByteToWideChar(CP_ACP, 0, linkPath, -1, buffer, MAX_PATH);

			hr = ppf->lpVtbl->Save(ppf, buffer, TRUE);

			ppf->lpVtbl->Release(ppf);
		}

		psl->lpVtbl->Release(psl);
	}

	if (SUCCEEDED(hr))
		printf("OK\n\n");
	else
		printf("error %08x\n\n", (int) hr);

	return hr;
}


int main()
{
	char path[MAX_PATH];
	LPSTR p;

	CoInitialize(NULL);

	/* create some shortcuts in the start menu "programs" folder */
	SHGetSpecialFolderPathA(0, path, CSIDL_PROGRAMS, TRUE);
	p = PathAddBackslash(path);

	strcpy(p, "start-cmd.lnk");
	CreateShellLink(path, "cmd.exe", "", NULL, NULL, 0, "open console window");

	strcpy(p, "start-winhello.lnk");
	CreateShellLink(path, "winhello.exe", "", NULL, NULL, 0, "launch winhello");


	/* create some shortcuts on the desktop */
	SHGetSpecialFolderPathA(0, path, CSIDL_DESKTOP, TRUE);
	p = PathAddBackslash(path);

	strcpy(p, "start-wcmd.lnk");
	CreateShellLink(path, "cmd.exe", "", NULL, NULL, 0, "open console window");

	strcpy(p, "start-winemine.lnk");
	CreateShellLink(path, "winemine.exe", "", NULL, NULL, 0, "launch winemine");

	CoUninitialize();

	return 0;
}