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

Last change on this file since 9889 was 9889, checked in by sandervl, 22 years ago

Move OSLibWinCreateObject to KERNEL32

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