创建快捷方式

注册表的方式会有权限问题,直接在启动目录下面创建快捷方式比较方便。

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

string appPath = w2s(getAppPath()) + "videoHelper.exe";
CreatLink2startupFolder(appPath,"videoHelper");

deleteLinkFromStarupFolder("videoHelper.lnk");


If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero (0).
*/
BOOL (string linkName)
{
CHAR szStartPath[MAX_PATH] = { 0 };
SHGetSpecialFolderPathA(NULL, szStartPath, CSIDL_STARTUP, 0);
string path = &szStartPath[0];
path += "\" + linkName;
return DeleteFileA(path.c_str());
}

bool CreatLink2startupFolder(string appPath, string linkName)
{
string appName = "\" + linkName + ".lnk";
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
IShellLink *pisl;
hr = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl);
if (SUCCEEDED(hr))
{
IPersistFile* pIPF;

//这里是我们要创建快捷方式的原始文件地址
pisl->SetPath(s2w(appPath).c_str());
hr = pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF);
if (SUCCEEDED(hr))
{
//这里是我们要创建快捷方式的目标地址
CHAR szStartPath[MAX_PATH] = { 0 };
SHGetSpecialFolderPathA(NULL, szStartPath, CSIDL_STARTUP, 0);
strcat_s(szStartPath,MAX_PATH, appName.c_str());

USES_CONVERSION;
LPCOLESTR lpOleStr = A2COLE(szStartPath);
pIPF->Save(lpOleStr, FALSE);
pIPF->Release();
}
pisl->Release();
}
CoUninitialize();
}

return true;
}