This commit was manufactured by cvs2svn to create branch 'captive'.
[reactos.git] / subsys / system / explorer / utility / utility.cpp
1 /*
2  * Copyright 2003 Martin Fuchs
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20  //
21  // Explorer clone
22  // 
23  // utility.cpp
24  //
25  // Martin Fuchs, 23.07.2003
26  //
27
28
29 #include "utility.h"
30 #include <shellapi.h>
31
32 #include <time.h>
33
34
35 void display_error(HWND hwnd, DWORD error)
36 {
37         PTSTR msg;
38
39         if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
40                 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
41                 MessageBox(hwnd, msg, TEXT("Winefile"), MB_OK);
42         else
43                 MessageBox(hwnd, TEXT("Error"), TEXT("Winefile"), MB_OK);
44
45         LocalFree(msg);
46 }
47
48
49 BOOL time_to_filetime(const time_t* t, FILETIME* ftime)
50 {
51         struct tm* tm = gmtime(t);
52         SYSTEMTIME stime;
53
54         if (!tm)
55                 return FALSE;
56
57         stime.wYear = tm->tm_year+1900;
58         stime.wMonth = tm->tm_mon+1;
59         /*      stime.wDayOfWeek */
60         stime.wDay = tm->tm_mday;
61         stime.wHour = tm->tm_hour;
62         stime.wMinute = tm->tm_min;
63         stime.wSecond = tm->tm_sec;
64
65         return SystemTimeToFileTime(&stime, ftime);
66 }
67
68
69 BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow)
70 {
71         HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
72
73         if ((int)hinst <= 32) {
74                 display_error(hwnd, GetLastError());
75                 return FALSE;
76         }
77
78         return TRUE;
79 }
80
81 #ifdef UNICODE
82 BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow)
83 {
84         HINSTANCE hinst = ShellExecuteA(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
85
86         if ((int)hinst <= 32) {
87                 display_error(hwnd, GetLastError());
88                 return FALSE;
89         }
90
91         return TRUE;
92 }
93 #endif
94
95
96 /* search for already running win[e]files */
97
98 static int g_foundPrevInstance = 0;
99
100 static BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lparam)
101 {
102         TCHAR cls[128];
103
104         GetClassName(hwnd, cls, 128);
105
106         if (!lstrcmp(cls, (LPCTSTR)lparam)) {
107                 g_foundPrevInstance++;
108                 return FALSE;
109         }
110
111         return TRUE;
112 }
113
114 /* search for window of given class name to allow only one running instance */
115 int find_window_class(LPCTSTR classname)
116 {
117         EnumWindows(EnumWndProc, (LPARAM)classname);
118
119         if (g_foundPrevInstance)
120                 return 1;
121
122         return 0;
123 }
124
125
126 typedef void (WINAPI*RUNDLLPROC)(HWND hwnd, HINSTANCE hinst, LPCTSTR cmdline, DWORD nCmdShow);
127
128 BOOL RunDLL(HWND hwnd, LPCTSTR dllname, LPCSTR procname, LPCTSTR cmdline, UINT nCmdShow)
129 {
130         HMODULE hmod = LoadLibrary(dllname);
131         if (!hmod)
132                 return FALSE;
133
134 /*TODO
135         <Windows NT/2000>
136         It is possible to create a Unicode version of the function.
137         Rundll32 first tries to find a function named EntryPointW.
138         If it cannot find this function, it tries EntryPointA, then EntryPoint.
139         To create a DLL that supports ANSI on Windows 95/98/Me and Unicode otherwise,
140         export two functions: EntryPointW and EntryPoint.
141 */
142         RUNDLLPROC proc = (RUNDLLPROC)GetProcAddress(hmod, procname);
143         if (!proc) {
144                 FreeLibrary(hmod);
145                 return FALSE;
146         }
147
148         proc(hwnd, hmod, cmdline, nCmdShow);
149
150         FreeLibrary(hmod);
151
152         return TRUE;
153 }