shortcuts.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 1983-2013 Martin Atkins, Richard Dobson and Composers Desktop Project Ltd
  3. * http://people.bath.ac.uk/masrwd
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /***********************************************************
  22. How to Obtain Filename and Path from a Shell Link or Shortcut
  23. Last reviewed: July 19, 1996
  24. Article ID: Q130698
  25. The information in this article applies to:
  26. * Microsoft Win32 Software Development Kit (SDK), versions 3.51
  27. and 4.0
  28. SUMMARY
  29. The shortcuts used in Microsoft Windows 95 provide applications and
  30. users a way to create shortcuts or links to objects in the shell's
  31. namespace. The IShellLink OLE Interface can be used to obtain the
  32. path and filename from the shortcut, among other things.
  33. MORE INFORMATION
  34. A shortcut allows the user or an application to access an object
  35. from anywhere in the namespace. Shortcuts to objects are stored as
  36. binary files. These files contain information such as the path to
  37. the object, working directory, the path of the icon used to display
  38. the object, the description string, and so on.
  39. Given a shortcut, applications can use the IShellLink interface and
  40. its functions to obtain all the pertinent information about that
  41. object. The IShellLink interface supports functions such as
  42. GetPath(), GetDescription(), Resolve(), GetWorkingDirectory(), and
  43. so on.
  44. Sample Code
  45. The following code shows how to obtain the filename or path and
  46. description of a given link file:
  47. ************************************************/
  48. #include <windows.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <shlobj.h>
  52. #include <io.h>
  53. #include "alias.h" //RWD: for wrapper funcs
  54. /********* CONTENTS OF ALIAS.H :
  55. int COMinit(void);
  56. void COMclose(void);
  57. int getAliasInfo(const char *linkfilename, char *path, char *desc);
  58. int fileExists(const char *name);
  59. *********** END OF CONTENTS OF ALIAS.H ************/
  60. static void ErrorMessage(LPCTSTR str, HRESULT hr);
  61. /* RWD: for encapsulation, this should be declared static
  62. * GetLinkInfo() fills the filename and path buffer
  63. * with relevant information.
  64. * hWnd - calling application's window handle.
  65. *
  66. * lpszLinkName - name of the link file passed into the function.
  67. *
  68. * lpszPath - the buffer that receives the file's path name.
  69. *
  70. * lpszDescription - the buffer that receives the file's
  71. * description.
  72. */
  73. HRESULT
  74. GetLinkInfo( HWND hWnd,
  75. LPCTSTR lpszLinkName,
  76. LPSTR lpszPath,
  77. LPSTR lpszDescription)
  78. {
  79. HRESULT hres;
  80. IShellLink *pShLink;
  81. WIN32_FIND_DATA wfd;
  82. /* Initialize the return parameters to null strings.*/
  83. *lpszPath = '\0';
  84. *lpszDescription = '\0';
  85. /* Call CoCreateInstance to obtain the IShellLink
  86. * Interface pointer. This call fails if
  87. * CoInitialize is not called, so it is assumed that
  88. * CoInitialize has been called.
  89. */
  90. hres = CoCreateInstance( &CLSID_ShellLink,
  91. NULL,
  92. CLSCTX_INPROC_SERVER,
  93. &IID_IShellLink,
  94. (LPVOID *)&pShLink );
  95. if (SUCCEEDED(hres))
  96. {
  97. IPersistFile *ppf;
  98. /* The IShellLink Interface supports the IPersistFile
  99. * interface. Get an interface pointer to it.
  100. */
  101. hres = pShLink->lpVtbl->QueryInterface(pShLink,
  102. &IID_IPersistFile,
  103. (LPVOID *)&ppf );
  104. if (SUCCEEDED(hres))
  105. {
  106. WORD wsz[MAX_PATH];
  107. /* Convert the given link name string to a wide character string. */
  108. MultiByteToWideChar( CP_ACP, 0,
  109. lpszLinkName,
  110. -1, wsz, MAX_PATH );
  111. /* Load the file. */
  112. hres = ppf->lpVtbl->Load(ppf, wsz, STGM_READ );
  113. if (SUCCEEDED(hres))
  114. {
  115. /* Resolve the link by calling the Resolve() interface function.
  116. * This enables us to find the file the link points to even if
  117. * it has been moved or renamed.
  118. */
  119. hres = pShLink->lpVtbl->Resolve(pShLink, hWnd,
  120. SLR_ANY_MATCH | SLR_NO_UI);
  121. if (SUCCEEDED(hres))
  122. {
  123. /* Get the path of the file the link points to. */
  124. hres = pShLink->lpVtbl->GetPath( pShLink, lpszPath,
  125. MAX_PATH,
  126. &wfd,
  127. SLGP_SHORTPATH );
  128. /* Only get the description if we successfully got the path
  129. * (We can't return immediately because we need to release ppf &
  130. * pShLink.)
  131. */
  132. if(SUCCEEDED(hres))
  133. {
  134. /* Get the description of the link. ** RWD: probably empty most times! */
  135. hres = pShLink->lpVtbl->GetDescription(pShLink,
  136. lpszDescription,
  137. MAX_PATH );
  138. }
  139. }
  140. }
  141. ppf->lpVtbl->Release(ppf);
  142. }
  143. pShLink->lpVtbl->Release(pShLink);
  144. }
  145. else
  146. ErrorMessage("Failure: CoCreateInstance",hres); /*RWD: eg of error message usage*/
  147. return hres;
  148. }
  149. /********* RWD utility functions *********/
  150. int COMinit(void)
  151. {
  152. HRESULT res = CoInitialize(NULL);
  153. if(res==S_OK)
  154. return 1;
  155. else
  156. return 0;
  157. }
  158. void COMclose(void)
  159. {
  160. CoUninitialize();
  161. }
  162. int fileExists(const char *name)
  163. {
  164. struct _finddata_t file;
  165. long hFile;
  166. hFile = _findfirst((char *)name,&file);
  167. return (int)(hFile != -1L);
  168. }
  169. /*the wrapper function itself, for a console application (no window handle) */
  170. /*requires COM to be initialized*/
  171. int getAliasInfo(const char *linkfilename, char *path, char *desc)
  172. {
  173. HRESULT res;
  174. res = GetLinkInfo(NULL, (LPCTSTR) linkfilename, (LPSTR) path, (LPSTR) desc);
  175. return (int) SUCCEEDED(res);
  176. }
  177. /*from Inside COM: DaleRogerson, changed from C++ to plain C*/
  178. void ErrorMessage(LPCTSTR str, HRESULT hr)
  179. {
  180. void *pMsgBuf;
  181. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  182. NULL,hr,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  183. (LPTSTR) &pMsgBuf, 0, NULL);
  184. printf("\n%s\n%s\n",str,(const char *)pMsgBuf);
  185. LocalFree(pMsgBuf);
  186. }