MPTrackUtil.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * MPTrackUtil.cpp
  3. * ---------------
  4. * Purpose: Various useful utility functions.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "MPTrackUtil.h"
  11. OPENMPT_NAMESPACE_BEGIN
  12. static bool CreateShellLink(const IID &type, const mpt::PathString &path, const mpt::PathString &target, const mpt::ustring &description)
  13. {
  14. HRESULT hres = 0;
  15. IShellLink *psl = nullptr;
  16. hres = CoCreateInstance(type, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
  17. if(SUCCEEDED(hres))
  18. {
  19. IPersistFile *ppf = nullptr;
  20. psl->SetPath(target.AsNative().c_str());
  21. psl->SetDescription(mpt::ToWin(description).c_str());
  22. hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
  23. if(SUCCEEDED(hres))
  24. {
  25. hres = ppf->Save(path.ToWide().c_str(), TRUE);
  26. ppf->Release();
  27. ppf = nullptr;
  28. }
  29. psl->Release();
  30. psl = nullptr;
  31. }
  32. return SUCCEEDED(hres);
  33. }
  34. bool CreateShellFolderLink(const mpt::PathString &path, const mpt::PathString &target, const mpt::ustring &description)
  35. {
  36. return CreateShellLink(CLSID_FolderShortcut, path, target, description);
  37. }
  38. bool CreateShellFileLink(const mpt::PathString &path, const mpt::PathString &target, const mpt::ustring &description)
  39. {
  40. return CreateShellLink(CLSID_ShellLink, path, target, description);
  41. }
  42. mpt::const_byte_span GetResource(LPCTSTR lpName, LPCTSTR lpType)
  43. {
  44. HINSTANCE hInstance = AfxGetInstanceHandle();
  45. HRSRC hRsrc = FindResource(hInstance, lpName, lpType);
  46. if(hRsrc == NULL)
  47. {
  48. return mpt::const_byte_span();
  49. }
  50. HGLOBAL hGlob = LoadResource(hInstance, hRsrc);
  51. if(hGlob == NULL)
  52. {
  53. return mpt::const_byte_span();
  54. }
  55. return mpt::const_byte_span(mpt::void_cast<const std::byte *>(LockResource(hGlob)), SizeofResource(hInstance, hRsrc));
  56. // no need to call FreeResource(hGlob) or free hRsrc, according to MSDN
  57. }
  58. CString LoadResourceString(UINT nID)
  59. {
  60. CString str;
  61. BOOL resourceLoaded = str.LoadString(nID);
  62. MPT_ASSERT(resourceLoaded);
  63. if(!resourceLoaded)
  64. {
  65. return _T("");
  66. }
  67. return str;
  68. }
  69. OPENMPT_NAMESPACE_END