Release 961215
[wine.git] / win32 / init.c
blobb61ac47acbc89abf33d36089af08f1848998cc71
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 */
7 #include <string.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include "windows.h"
11 #include "winerror.h"
12 #include "handle32.h"
13 #include "except.h"
14 #include "task.h"
15 #include "stddebug.h"
16 #include "string32.h"
17 #define DEBUG_WIN32
18 #include "debug.h"
19 #include "string32.h"
20 #include "xmalloc.h"
22 /* The global error value
24 int WIN32_LastError;
26 /*********************************************************************
27 * CloseHandle (KERNEL32.23)
29 BOOL CloseHandle(KERNEL_OBJECT *handle)
31 if (handle<0x1000) /* FIXME: hack */
32 return CloseFileHandle(handle);
33 if (handle==0xFFFFFFFF)
34 return FALSE;
35 switch(handle->magic)
37 case KERNEL_OBJECT_UNUSED:
38 SetLastError(ERROR_INVALID_HANDLE);
39 return 0;
40 /* FIXME
41 case KERNEL_OBJECT_FILE:
42 rc = CloseFileHandle((FILE_OBJECT *)handle);
43 break;
46 default:
47 dprintf_win32(stddeb, "CloseHandle: type %ld not implemented yet.\n",
48 handle->magic);
49 break;
52 ReleaseKernelObject(handle);
53 return 0;
56 /***********************************************************************
57 * GetModuleHandle (KERNEL32.237)
59 HMODULE32 WIN32_GetModuleHandleA(char *module)
61 HMODULE32 hModule;
63 dprintf_win32(stddeb, "GetModuleHandleA: %s\n", module ? module : "NULL");
64 /* Freecell uses the result of GetModuleHandleA(0) as the hInstance in
65 all calls to e.g. CreateWindowEx. */
66 if (module == NULL) {
67 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
68 hModule = pTask->hInstance;
69 } else
70 hModule = GetModuleHandle(module);
71 dprintf_win32(stddeb, "GetModuleHandleA: returning %d\n", hModule );
72 return hModule;
75 HMODULE32 WIN32_GetModuleHandleW(LPCWSTR module)
77 HMODULE32 hModule;
78 LPSTR modulea;
79 if(module==NULL) return WIN32_GetModuleHandleA(NULL);
80 modulea=STRING32_DupUniToAnsi(module);
81 hModule = WIN32_GetModuleHandleA(modulea);
82 free(modulea);
83 return hModule;
87 /***********************************************************************
88 * GetStartupInfoA (KERNEL32.273)
90 VOID GetStartupInfo32A(LPSTARTUPINFO32A lpStartupInfo)
92 lpStartupInfo->cb = sizeof(STARTUPINFO32A);
93 lpStartupInfo->lpReserved = "<Reserved>";
94 lpStartupInfo->lpDesktop = "Desktop";
95 lpStartupInfo->lpTitle = "Title";
97 lpStartupInfo->cbReserved2 = 0;
98 lpStartupInfo->lpReserved2 = NULL; /* must be NULL for VC runtime */
99 lpStartupInfo->hStdInput = (HANDLE32)0;
100 lpStartupInfo->hStdOutput = (HANDLE32)1;
101 lpStartupInfo->hStdError = (HANDLE32)2;
104 /***********************************************************************
105 * GetStartupInfoW (KERNEL32.274)
107 VOID GetStartupInfo32W(LPSTARTUPINFO32W lpStartupInfo)
109 lpStartupInfo->cb = sizeof(STARTUPINFO32W);
110 lpStartupInfo->lpReserved = STRING32_DupAnsiToUni("<Reserved>");
111 lpStartupInfo->lpDesktop = STRING32_DupAnsiToUni("Desktop");
112 lpStartupInfo->lpTitle = STRING32_DupAnsiToUni("Title");
114 lpStartupInfo->cbReserved2 = 0;
115 lpStartupInfo->lpReserved2 = NULL; /* must be NULL for VC runtime */
116 lpStartupInfo->hStdInput = (HANDLE32)0;
117 lpStartupInfo->hStdOutput = (HANDLE32)1;
118 lpStartupInfo->hStdError = (HANDLE32)2;
121 /***********************************************************************
122 * GetStartupInfoA (KERNEL32.284)
123 * FIXME: perhaps supply better values.
124 * add other architectures for WINELIB.
126 VOID
127 GetSystemInfo(LPSYSTEM_INFO si) {
128 WORD cpu;
130 si->u.x.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
132 si->dwPageSize = 4096; /* 4K */
133 si->lpMinimumApplicationAddress = 0x40000000;
134 si->lpMaximumApplicationAddress = 0x80000000;
135 si->dwActiveProcessorMask = 1;
136 si->dwNumberOfProcessors = 1;
137 #ifdef WINELIB
138 /* FIXME: perhaps check compilation defines ... */
139 si->dwProcessorType = PROCESSOR_INTEL_386;
140 cpu = 3;
141 #else
142 cpu = runtime_cpu();
143 switch (cpu) {
144 case 4: si->dwProcessorType = PROCESSOR_INTEL_486;
145 break;
146 case 5: si->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
147 break;
148 case 3:
149 default: si->dwProcessorType = PROCESSOR_INTEL_386;
150 break;
152 #endif
153 si->dwAllocationGranularity = 8; /* hmm? */
154 si->wProcessorLevel = cpu;
155 si->wProcessorRevision = 0; /* FIXME, see SDK */
158 /* Initialize whatever internal data structures we need.
160 * Returns 1 on success, 0 on failure.
162 int KERN32_Init(void)
164 #ifndef WINELIB
165 /* Initialize exception handling */
166 EXC_Init();
167 #endif
168 return 1;
171 /***********************************************************************
172 * GetComputerNameA (KERNEL32.165)
174 BOOL32
175 GetComputerName32A(LPSTR name,LPDWORD size) {
176 if (-1==gethostname(name,*size))
177 return FALSE;
178 *size = lstrlen32A(name);
179 return TRUE;
182 /***********************************************************************
183 * GetComputerNameW (KERNEL32.166)
185 BOOL32
186 GetComputerName32W(LPWSTR name,LPDWORD size) {
187 LPSTR nameA = (LPSTR)xmalloc(*size);
189 if (!GetComputerName32A(nameA,size)) {
190 free(nameA);
191 return FALSE;
193 lstrcpynAtoW(name,nameA,*size);
194 free(nameA);
195 /* FIXME : size correct? */
196 return TRUE;
199 /***********************************************************************
200 * GetUserNameA [ADVAPI32.67]
202 BOOL32 GetUserName32A(LPSTR lpszName, LPDWORD lpSize)
204 size_t len;
205 char *name;
207 name=getlogin();
208 len = name ? strlen(name) : 0;
209 if (!len || !lpSize || len > *lpSize) {
210 if (lpszName) *lpszName = 0;
211 return 0;
213 *lpSize=len;
214 strcpy(lpszName, name);
215 return 1;
218 /***********************************************************************
219 * GetUserNameW [ADVAPI32.68]
221 BOOL32 GetUserName32W(LPWSTR lpszName, LPDWORD lpSize)
223 LPSTR name = (LPSTR)xmalloc(*lpSize);
224 DWORD size = *lpSize;
225 BOOL32 res = GetUserName32A(name,lpSize);
227 lstrcpynAtoW(lpszName,name,size);
228 return res;