File: winshell.c

package info (click to toggle)
k2pdfopt 2.53%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,668 kB
  • sloc: ansic: 81,507; cpp: 6,400; makefile: 5
file content (179 lines) | stat: -rw-r--r-- 5,165 bytes parent folder | download | duplicates (2)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
** winshell.c    Windows specific calls related to Windows Shell
**
** Part of willus.com general purpose C code library.
**
** Copyright (C) 2015  http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*/

#if (!defined(__GNUC__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
#include "willus.h"

#ifdef HAVE_WIN32_API
#include <windows.h>
#include <shlobj.h>
/* #include <process.h> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <conio.h>

/*
** Resolve a windows shortcut into it's full target name.
** If wide, shortcut and target are treated as wide ptrs, otherwise char.
*/
int win_resolve_shortcut(void *shortcut0,void *target0,int maxlen,int wide)

    {
    WCHAR *shortcutw,*targetw;
    char *shortcut,*target;
	IShellLinkW *psl = NULL;
    static char *funcname="win_resolve_shortcut";

	if (target0 == NULL)
		return(0);
    shortcutw=(WCHAR *)shortcut0;
    shortcut=(char *)shortcut0;
    targetw=(WCHAR *)target0;
    target=(char *)target0;
    if (wide)
    	target[0]='\0';
    else
        targetw[0]=0;
    /*
	** Get a pointer to the IShellLink interface. It is assumed that CoInitialize
	** has already been called.
    */
	HRESULT hres = CoCreateInstance(&CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,
                                    &IID_IShellLinkW,(LPVOID*)&psl);
	if (SUCCEEDED(hres))
        {
		/* Get a pointer to the IPersistFile interface. */
		IPersistFile *ppf = NULL;

		hres = psl->lpVtbl->QueryInterface(psl,&IID_IPersistFile,(void**)&ppf);
		if (SUCCEEDED(hres))
            {
			/* Add code here to check return value from MultiByteWideChar for success. */
			/* Load the shortcut. */
/*
** MinGW doesn't appear to define this...
#ifdef _UNICODE
			hres = ppf->lpVtbl->Load(ppf,shortcut,STGM_READ);
#else
*/
            if (!wide)
                {
    			WCHAR widestr[512];

                widestr[0]='\0';
	    		/* Ensure that the string is Unicode. */
	    		MultiByteToWideChar(CP_ACP,0,shortcut,-1, widestr,511);
	    		hres = ppf->lpVtbl->Load(ppf,widestr,STGM_READ);
                }
            else
                hres = ppf->lpVtbl->Load(ppf,shortcutw,STGM_READ);
/*
#endif
*/
			if (SUCCEEDED(hres))
                {
				/* Resolve the link. */
				hres = psl->lpVtbl->Resolve(psl,GetDesktopWindow(),0);
				if (SUCCEEDED(hres))
                    {
                    if (!wide)
                        {
                        short *buf;

                        willus_mem_alloc_warn((void **)&buf,maxlen*sizeof(short),funcname,10);
                        hres = psl->lpVtbl->GetPath(psl,(WCHAR *)buf,maxlen,NULL,SLGP_RAWPATH);
                        wide_to_char(target,buf);
                        }
                    else
                        hres = psl->lpVtbl->GetPath(psl,targetw,maxlen,NULL,SLGP_RAWPATH);
                    }
			    }
			ppf->lpVtbl->Release(ppf);
		    }
		psl->lpVtbl->Release(psl);
	    }
	return(SUCCEEDED(hres) ? 1 : 0);
    }


/*
** Returns 0 if user cancels, 1 otherwise
** foldername[] must have MAX_PATH (260) characters allocated
*/
int winshell_get_foldername(char *foldername,char *title)

    {
    BROWSEINFO *bi,_bi;
    PCIDLIST_ABSOLUTE pidl;

    bi=&_bi;
    bi->hwndOwner = NULL;
    bi->pidlRoot = NULL;  /* Start folder */
    bi->pszDisplayName = foldername;
    bi->lpszTitle = title;
    bi->ulFlags = BIF_EDITBOX | BIF_VALIDATE | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;
    bi->lpfn = NULL;
    bi->lParam = 0;
    bi->iImage = 0;
    pidl=SHBrowseForFolder(bi);
    if (pidl==NULL)
        return(0);
    if (SHGetPathFromIDList(pidl,foldername))
        return(1);
    return(0);
    }

/*
** Returns 0 if user cancels, 1 otherwise
** foldername[] must have MAX_PATH (260) characters allocated
*/
int winshell_get_foldernamew(short *foldername,char *title)

    {
    BROWSEINFOW *bi,_bi;
    PCIDLIST_ABSOLUTE pidl;
    short *wtitle;

    wtitle=char_to_wide(NULL,title);
    bi=&_bi;
    bi->hwndOwner = NULL;
    bi->pidlRoot = NULL;  /* Start folder */
    bi->pszDisplayName = (LPWSTR)foldername;
    bi->lpszTitle = (LPWSTR)wtitle;
    bi->ulFlags = BIF_EDITBOX | BIF_VALIDATE | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;
    bi->lpfn = NULL;
    bi->lParam = 0;
    bi->iImage = 0;
    pidl=SHBrowseForFolderW(bi);
    if (pidl==NULL)
        return(0);
    if (SHGetPathFromIDListW(pidl,(LPWSTR)foldername))
        return(1);
    return(0);
    }

#endif /* HAVE_WIN32_API */
#endif /* GNU C version test */