source: trunk/src/kernel32/oslibwps.cpp@ 22018

Last change on this file since 22018 was 21916, checked in by dmik, 14 years ago

Merge branch gcc-kmk to trunk.

File size: 8.0 KB
Line 
1#define INCL_WIN
2#define INCL_PM
3#define INCL_WPCLASS
4#include <os2wrap.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <malloc.h>
9
10#include <dbglog.h>
11#include <winconst.h>
12#include <win32api.h>
13#include <wprocess.h>
14#include <custombuild.h>
15
16#include "oslibmisc.h"
17
18#define WPS_SHELLLINK_MAGIC "WPS_SHELLLINK:"
19#define WPS_SHELLLINK_DESKTOP "WPS_SHELLLINK_DESKTOP_"
20
21
22static BOOL fDisableFolderShellLink = FALSE;
23
24//******************************************************************************
25//******************************************************************************
26void OSLibStripFile(char *path)
27{
28 char *pszFilename;
29 char *pszFilename1;
30
31 pszFilename = strrchr(path, '\\'); /* find rightmost backslash */
32 pszFilename1 = strrchr(path, '/'); /* find rightmost slash */
33 if(pszFilename > pszFilename1 && pszFilename != NULL)
34 *pszFilename = 0;
35 else
36 if (pszFilename1 != NULL)
37 *pszFilename1 = 0;
38}
39
40extern "C" {
41
42//******************************************************************************
43// ODIN_DisableFolderShellLink
44//
45// Disable object creation in Odin folder. Desktop shortcuts will still be
46// created as WPS objects on the desktop.
47//
48//******************************************************************************
49void WIN32API ODIN_DisableFolderShellLink()
50{
51 fDisableFolderShellLink = TRUE;
52}
53//******************************************************************************
54//******************************************************************************
55BOOL WIN32API OSLibWinCreateObject(LPSTR pszPath, LPSTR pszArgs,
56 LPSTR pszWorkDir, LPSTR pszLink,
57 LPSTR pszDescription, LPSTR pszIcoPath,
58 INT iIcoNdx, BOOL fDesktop)
59{
60 HOBJECT hObject = 0;
61 LPSTR pszName;
62 LPSTR pszSetupString;
63 LPSTR pszFolder;
64 char szSystemDir[256];
65 char temp[128];
66 char szWorkDir[256];
67 char szPEGUILoaderPath[256];
68 BOOL fWin32App;
69 FILE *lnkfile = NULL;
70
71 if(fDisableFolderShellLink && !fDesktop) {
72 return TRUE; //pretend success
73 }
74
75 if(pszLink) {
76 lnkfile = fopen(pszLink, "wb");
77
78 char *tmp;
79 pszName = OSLibStripPath(pszLink);
80 tmp = pszName;
81 while(*tmp) {
82 if(*tmp == '.') {
83 *tmp = 0;
84 break;
85 }
86 tmp++;
87 }
88 }
89 else {
90 dprintf(("OSLibWinCreateObject: pszLink == NULL!!"));
91 goto fail;
92 }
93 dprintf(("OSLibWinCreateObject %s %s %s\n %s %s %s %d %d", pszPath, pszArgs,
94 pszWorkDir, pszName, pszDescription, pszIcoPath, iIcoNdx, fDesktop));
95 dprintf(("Link path %s", pszLink));
96
97 GetSystemDirectoryA(szSystemDir, sizeof(szSystemDir));
98 if(pszWorkDir && *pszWorkDir) {
99 strcpy(szWorkDir, pszWorkDir);
100 }
101 else {
102 strcpy(szWorkDir, pszPath);
103 OSLibStripFile(szWorkDir);
104 }
105
106 ODIN_QueryLoaders(NULL, 0, szPEGUILoaderPath, sizeof(szPEGUILoaderPath), NULL, 0);
107
108 pszSetupString = (LPSTR)malloc(128 + strlen(pszPath) + strlen(pszName) +
109 strlen(pszLink) + strlen(szSystemDir) +
110 strlen(szWorkDir) + strlen(pszIcoPath) +
111 strlen(szPEGUILoaderPath) +
112 ((pszArgs) ? strlen(pszArgs) : 0) +
113 ((pszWorkDir) ? strlen(pszWorkDir) : 0));
114 if(pszSetupString == NULL) {
115 DebugInt3();
116 goto fail;
117 }
118
119 fWin32App = ODIN_IsWin32App(pszPath);
120 if(!fWin32App)
121 {//don't use the PE loader; use the program path directly
122 sprintf(pszSetupString, "PROGTYPE=PM;OBJECTID=<%s%s>;EXENAME=%s;SET BEGINLIBPATH=%s;STARTUPDIR=%s;ICONFILE=%s;PARAMETERS=", (fDesktop) ? WPS_SHELLLINK_DESKTOP : "", pszName, pszPath, szSystemDir, szWorkDir, pszIcoPath);
123 }
124 else sprintf(pszSetupString, "PROGTYPE=PM;OBJECTID=<%s%s>;EXENAME=%s;SET BEGINLIBPATH=%s;STARTUPDIR=%s;ICONFILE=%s;PARAMETERS=\"%s\"", (fDesktop) ? WPS_SHELLLINK_DESKTOP : "", pszName, szPEGUILoaderPath, szSystemDir, szWorkDir, pszIcoPath, pszPath);
125 if(pszArgs && *pszArgs) {
126 strcat(pszSetupString, " ");
127 strcat(pszSetupString, pszArgs);
128 }
129 strcat(pszSetupString, ";");
130
131 if(fDesktop) {
132 dprintf(("Name = %s", pszName));
133 dprintf(("Setup string = %s", pszSetupString));
134
135 //Use a different name for desktop objects
136 hObject = WinCreateObject("WPProgram", pszName, pszSetupString,
137 "<WP_DESKTOP>", CO_REPLACEIFEXISTS);
138 }
139 else {
140 //e.g.: Link path k:\source\odin32\bin\win\Start Menu\Programs\Winamp\Winamp
141 OSLibStripFile(pszLink);
142 pszFolder = OSLibStripPath(pszLink);
143 sprintf(temp, "<FOLDER_%s>", pszFolder);
144 sprintf(szWorkDir, "OBJECTID=%s;", temp);
145 hObject = WinCreateObject("WPFolder", pszFolder, szWorkDir,
146 "<ODINFOLDER>", CO_UPDATEIFEXISTS);
147 if(hObject) {
148 hObject = WinCreateObject("WPProgram", pszName, pszSetupString,
149 temp, CO_REPLACEIFEXISTS);
150 }
151 else {
152 hObject = 1; //force silent failure
153 }
154 }
155
156 if(!hObject) {
157 dprintf(("ERROR: WinCreateObject failed!!"));
158 }
159 else {
160 //write WPS object info to lnk file
161
162 //write magic string
163 fwrite(WPS_SHELLLINK_MAGIC, 1, sizeof(WPS_SHELLLINK_MAGIC)-1, lnkfile);
164 //and WPS object id
165 if(fDesktop) {
166 fwrite(WPS_SHELLLINK_DESKTOP, 1, sizeof(WPS_SHELLLINK_DESKTOP)-1, lnkfile);
167 }
168 fwrite(pszName, 1, strlen(pszName), lnkfile);
169 }
170 free(pszSetupString);
171 if(lnkfile) fclose(lnkfile);
172 return hObject != 0;
173
174fail:
175 if(lnkfile) fclose(lnkfile);
176 return FALSE;
177}
178//******************************************************************************
179//******************************************************************************
180BOOL WIN32API OSLibIsShellLink(LPSTR lpszLink)
181{
182 char szLinkFile[CCHMAXPATH];
183 BOOL fIsShellLink = FALSE;
184
185 strcpy(szLinkFile, lpszLink);
186 strupr(szLinkFile);
187 if(strstr(szLinkFile, ".LNK"))
188 {//could be a shelllink file, check for magic string at start of the file
189 FILE *lnkfile;
190
191 lnkfile = fopen(lpszLink, "r");
192 if(lnkfile == NULL) return FALSE;
193
194 char szMagic[sizeof(WPS_SHELLLINK_MAGIC)];
195
196 memset(szMagic, 0, sizeof(szMagic));
197 fread(szMagic, sizeof(szMagic)-1, 1, lnkfile);
198
199 if(!strcmp(szMagic, WPS_SHELLLINK_MAGIC)) {
200 fIsShellLink = TRUE;
201 }
202 fclose(lnkfile);
203 }
204 return fIsShellLink;
205}
206//******************************************************************************
207// OSLibWinDeleteObject
208//
209// Delete object with object id stored in the file
210//
211// Parameters:
212//
213// LPSTR lpszLink - shelllink file
214//
215// Returns:
216// FALSE - failure
217// TRUE - success
218//
219//******************************************************************************
220BOOL WIN32API OSLibWinDeleteObject(LPSTR lpszLink)
221{
222 char szObjectId[CCHMAXPATH];
223 HOBJECT hObject;
224 int wpsobjectidsize;
225 FILE *lnkfile;
226
227 lnkfile = fopen(lpszLink, "r");
228 if(lnkfile == NULL) return FALSE;
229
230 char szMagic[sizeof(WPS_SHELLLINK_MAGIC)];
231
232 memset(szMagic, 0, sizeof(szMagic));
233 fread(szMagic, sizeof(szMagic)-1, 1, lnkfile);
234
235 if(!strcmp(szMagic, WPS_SHELLLINK_MAGIC))
236 {
237 fseek(lnkfile, 0, SEEK_END);
238 wpsobjectidsize = ftell(lnkfile) - sizeof(WPS_SHELLLINK_MAGIC) + 2;
239 fseek(lnkfile, sizeof(WPS_SHELLLINK_MAGIC)-1, SEEK_SET);
240
241 memset(szObjectId, 0, sizeof(szObjectId));
242 szObjectId[0] = '<';
243 fread(&szObjectId[1], min(wpsobjectidsize, sizeof(szObjectId)-1), 1, lnkfile);
244 strcat(szObjectId, ">");
245
246 hObject = WinQueryObject(szObjectId);
247 if(hObject) {
248 WinDestroyObject(hObject);
249 }
250 }
251 fclose(lnkfile);
252 return TRUE;
253}
254//******************************************************************************
255//******************************************************************************
256
257} // extern "C"
Note: See TracBrowser for help on using the repository browser.