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
|
/*
* MPTrackUtil.cpp
* ---------------
* Purpose: Various useful utility functions.
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "MPTrackUtil.h"
OPENMPT_NAMESPACE_BEGIN
static bool CreateShellLink(const IID &type, const mpt::PathString &path, const mpt::PathString &target, const mpt::ustring &description)
{
HRESULT hres = 0;
IShellLink *psl = nullptr;
hres = CoCreateInstance(type, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if(SUCCEEDED(hres))
{
IPersistFile *ppf = nullptr;
psl->SetPath(target.AsNative().c_str());
psl->SetDescription(mpt::ToWin(description).c_str());
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if(SUCCEEDED(hres))
{
hres = ppf->Save(path.ToWide().c_str(), TRUE);
ppf->Release();
ppf = nullptr;
}
psl->Release();
psl = nullptr;
}
return SUCCEEDED(hres);
}
bool CreateShellFolderLink(const mpt::PathString &path, const mpt::PathString &target, const mpt::ustring &description)
{
return CreateShellLink(CLSID_FolderShortcut, path, target, description);
}
bool CreateShellFileLink(const mpt::PathString &path, const mpt::PathString &target, const mpt::ustring &description)
{
return CreateShellLink(CLSID_ShellLink, path, target, description);
}
mpt::const_byte_span GetResource(LPCTSTR lpName, LPCTSTR lpType)
{
HINSTANCE hInstance = AfxGetInstanceHandle();
HRSRC hRsrc = FindResource(hInstance, lpName, lpType);
if(hRsrc == NULL)
{
return mpt::const_byte_span();
}
HGLOBAL hGlob = LoadResource(hInstance, hRsrc);
if(hGlob == NULL)
{
return mpt::const_byte_span();
}
return mpt::const_byte_span(mpt::void_cast<const std::byte *>(LockResource(hGlob)), SizeofResource(hInstance, hRsrc));
// no need to call FreeResource(hGlob) or free hRsrc, according to MSDN
}
CString LoadResourceString(UINT nID)
{
CString str;
BOOL resourceLoaded = str.LoadString(nID);
MPT_ASSERT(resourceLoaded);
if(!resourceLoaded)
{
return _T("");
}
return str;
}
OPENMPT_NAMESPACE_END
|