1 /* realpath - return the canonicalized absolute pathname
2 * Copyright (C) 2001 Steven Barker
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation in version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 /* Modified 2002-01-26 Charles Wilson:
19 * -- use popt for option handling
20 * -- remove gettext dependency
21 */
22
23 /* Modified 2010-01-27 Jeffrey Pohlmeyer:
24 * -- Butchered for use with the FXiTe text editor
25 */
26
27 #ifdef WIN32
28 #include <stdio.h>
29 #include <windows.h>
30 #include <shlobj.h>
31 #include <olectlid.h>
32 #include "intl.h"
33
34 /*
35 Attempt to read a MS-Windows shortcut (*.lnk) file.
36 Returns 1 on success and places the target path in *dst,
37 on error it returns 0 and places an error message in *dst.
38 *dst should be disposed of by the caller by using free()!
39 */
40
41 #define debug(s) fprintf(stderr, "DEBUG: "s"\n"); fflush(stderr);
42
43 static int ole_is_init = 0;
44
ReadShortcut(char ** dst,const char * src)45 int ReadShortcut(char**dst, const char*src)
46 {
47 HRESULT hres;
48 IShellLink *sh_lnk;
49 IPersistFile *persist_file;
50 int result = 1;
51 *dst=(char*)calloc(MAX_PATH,1);
52 if (!ole_is_init) {
53 hres = OleInitialize(NULL);
54 if (hres != S_FALSE && hres != S_OK) {
55 strncpy(*dst, _("ReadShortcut: OleInitialize failed"), MAX_PATH-1);
56 return 0;
57 } else {
58 ole_is_init=1;
59 }
60 }
61 hres=CoCreateInstance(&CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,&IID_IShellLink,(void*)&sh_lnk);
62 if (!SUCCEEDED(hres)) {
63 strncpy(*dst, _("ReadShortcut: CoCreateInstance failed"), MAX_PATH-1);
64 return 0;
65 }
66 hres = sh_lnk->lpVtbl->QueryInterface(sh_lnk, &IID_IPersistFile,(void*)&persist_file);
67 if (SUCCEEDED(hres)) {
68 WCHAR wsz[MAX_PATH];
69 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, wsz, MAX_PATH);
70 hres = persist_file->lpVtbl->Load(persist_file, wsz, STGM_READ);
71 if (SUCCEEDED(hres)) {
72 sh_lnk->lpVtbl->GetPath(sh_lnk, *dst, MAX_PATH, NULL, SLGP_RAWPATH);
73 } else {
74 strncpy(*dst, _("ReadShortcut: Load failed"), MAX_PATH-1);
75 result = 0;
76 }
77 persist_file->lpVtbl->Release(persist_file);
78 } else {
79 strncpy(*dst, _("ReadShortcut: QueryInterface failed"), MAX_PATH-1);
80 result = 0;
81 }
82 sh_lnk->lpVtbl->Release(sh_lnk);
83 return result;
84 }
85
86 #endif
87
88