ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / extras / windowsShortcut.ec
1 #define COBJMACROS
2 #define WIN32_LEAN_AND_MEAN
3 #define UNICODE
4 #define Method _Method
5 #define Array _Array
6 #define byte _byte
7 #define int64 _int64
8 #include <shlobj.h>
9 #undef Method
10 #undef Array
11 #undef byte
12 #undef int64
13
14 #ifdef ECERE_STATIC
15 import static "ecere"
16 #else
17 import "ecere"
18 #endif
19
20
21 bool CreateLink(char * lpszPathObj, char * lpszPathLink, char * lpszDesc)
22 {
23     HRESULT hres;
24     IShellLink* psl;
25
26     CoInitialize(NULL);
27     hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void *)&psl);
28     if (SUCCEEDED(hres))
29     {
30         IPersistFile * ppf;
31         uint16 pathObj[2048] = { 0 };
32         uint16 desc[2048] = { 0 };
33
34         UTF8toUTF16Buffer(lpszPathObj, pathObj, sizeof(pathObj) / sizeof(uint16));
35         UTF8toUTF16Buffer(lpszDesc, desc, sizeof(desc) / sizeof(uint16));
36         IShellLinkW_SetPath(psl, pathObj);
37         IShellLinkW_SetDescription(psl, desc);
38
39         //hres = IShellLinkA_QueryInterface(psl, &IID_IPersistFile, (void *)&ppf);
40         hres = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (void *)&ppf);
41
42         if(SUCCEEDED(hres))
43         {
44             WCHAR wsz[MAX_PATH];
45             MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
46             hres = IPersistFile_Save(ppf, wsz, TRUE);
47             IPersistFile_Release(ppf);
48         }
49         IShellLinkW_Release(psl);
50     }
51     return hres == 0;
52 }
53
54 bool GetLinkTarget(char * lpszPathObj, char ** lpszPathLink)
55 {
56     HRESULT hres;
57     IShellLink* psl;
58
59     CoInitialize(NULL);
60     hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void *)&psl);
61     if (SUCCEEDED(hres))
62     {
63         IPersistFile * ppf;
64         uint16 pathObj[2048] = { 0 };
65
66         UTF8toUTF16Buffer(lpszPathObj, pathObj, sizeof(pathObj) / sizeof(uint16));
67
68         hres = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (void *)&ppf);
69
70         if(SUCCEEDED(hres))
71         {
72             WCHAR wsz[MAX_PATH];
73             if(MultiByteToWideChar(CP_ACP, 0, lpszPathObj, -1, wsz, MAX_PATH))
74             {
75                if(SUCCEEDED(IPersistFile_Load(ppf, wsz, STGM_READ)))
76                {
77                   WCHAR szTarget[MAX_PATH];
78                   if(NOERROR == IShellLinkW_GetPath(psl, szTarget, MAX_PATH, NULL, 0))
79                   {
80                      char pathTarget[2048] = { 0 };
81                      UTF16toUTF8Buffer(szTarget, pathTarget, sizeof(pathTarget) / sizeof(uint16));
82                      *lpszPathLink = CopyString(pathTarget);
83                   }
84                }
85             }
86             IPersistFile_Release(ppf);
87         }
88         IShellLinkW_Release(psl);
89     }
90     CoUninitialize();
91     return hres == 0;
92 }