Release 970101
[wine] / win32 / init.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
5  */
6
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 "heap.h"
15 #include "task.h"
16 #include "stddebug.h"
17 #include "debug.h"
18 #include "xmalloc.h"
19   
20 /* The global error value
21  */
22 int WIN32_LastError;
23
24 /***********************************************************************
25  *              GetModuleHandle         (KERNEL32.237)
26  */
27 HMODULE32 WIN32_GetModuleHandleA(char *module)
28 {
29     HMODULE32 hModule;
30
31     dprintf_win32(stddeb, "GetModuleHandleA: %s\n", module ? module : "NULL");
32 /* Freecell uses the result of GetModuleHandleA(0) as the hInstance in
33 all calls to e.g. CreateWindowEx. */
34     if (module == NULL) {
35         TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
36         hModule = pTask->hInstance;
37     } else
38         hModule = GetModuleHandle(module);
39     dprintf_win32(stddeb, "GetModuleHandleA: returning %d\n", hModule );
40     return hModule;
41 }
42
43 HMODULE32 WIN32_GetModuleHandleW(LPCWSTR module)
44 {
45     HMODULE32 hModule;
46     LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
47     hModule = WIN32_GetModuleHandleA( modulea );
48     HeapFree( GetProcessHeap(), 0, modulea );
49     return hModule;
50 }
51
52
53 /***********************************************************************
54  *              GetStartupInfoA         (KERNEL32.273)
55  */
56 VOID GetStartupInfo32A(LPSTARTUPINFO32A lpStartupInfo)
57 {
58     lpStartupInfo->cb = sizeof(STARTUPINFO32A);
59     lpStartupInfo->lpReserved = "<Reserved>";
60     lpStartupInfo->lpDesktop = "Desktop";
61     lpStartupInfo->lpTitle = "Title";
62
63     lpStartupInfo->cbReserved2 = 0;
64     lpStartupInfo->lpReserved2 = NULL; /* must be NULL for VC runtime */
65     lpStartupInfo->hStdInput  = (HANDLE32)0;
66     lpStartupInfo->hStdOutput = (HANDLE32)1;
67     lpStartupInfo->hStdError  = (HANDLE32)2;
68 }
69
70 /***********************************************************************
71  *              GetStartupInfoW         (KERNEL32.274)
72  */
73 VOID GetStartupInfo32W(LPSTARTUPINFO32W lpStartupInfo)
74 {
75     lpStartupInfo->cb = sizeof(STARTUPINFO32W);
76     lpStartupInfo->lpReserved = HEAP_strdupAtoW(GetProcessHeap(),0,"<Reserved>");
77     lpStartupInfo->lpDesktop = HEAP_strdupAtoW(GetProcessHeap(), 0, "Desktop");
78     lpStartupInfo->lpTitle = HEAP_strdupAtoW(GetProcessHeap(), 0, "Title");
79
80     lpStartupInfo->cbReserved2 = 0;
81     lpStartupInfo->lpReserved2 = NULL; /* must be NULL for VC runtime */
82     lpStartupInfo->hStdInput  = (HANDLE32)0;
83     lpStartupInfo->hStdOutput = (HANDLE32)1;
84     lpStartupInfo->hStdError  = (HANDLE32)2;
85 }
86
87 /***********************************************************************
88  *              GetStartupInfoA         (KERNEL32.284)
89  * FIXME: perhaps supply better values. 
90  *        add other architectures for WINELIB.
91  */
92 VOID
93 GetSystemInfo(LPSYSTEM_INFO si) {
94     WORD cpu;
95     
96     si->u.x.wProcessorArchitecture      = PROCESSOR_ARCHITECTURE_INTEL;
97
98     si->dwPageSize                      = 4096; /* 4K */
99     si->lpMinimumApplicationAddress     = (void *)0x40000000;
100     si->lpMaximumApplicationAddress     = (void *)0x80000000;
101     si->dwActiveProcessorMask           = 1;
102     si->dwNumberOfProcessors            = 1;
103 #ifdef WINELIB
104     /* FIXME: perhaps check compilation defines ... */
105     si->dwProcessorType                 = PROCESSOR_INTEL_386;
106     cpu = 3;
107 #else
108     cpu = runtime_cpu();
109     switch (cpu) {
110     case 4: si->dwProcessorType         = PROCESSOR_INTEL_486;
111             break;
112     case 5: si->dwProcessorType         = PROCESSOR_INTEL_PENTIUM;
113             break;
114     case 3:
115     default: si->dwProcessorType        = PROCESSOR_INTEL_386;
116             break;
117     }
118 #endif
119     si->dwAllocationGranularity         = 8; /* hmm? */
120     si->wProcessorLevel                 = cpu;
121     si->wProcessorRevision              = 0; /* FIXME, see SDK */
122 }
123
124 /* Initialize whatever internal data structures we need.
125  *
126  * Returns 1 on success, 0 on failure.
127  */
128 int KERN32_Init(void)
129 {
130 #ifndef WINELIB
131     /* Initialize exception handling */
132     EXC_Init();
133 #endif
134     return 1;
135 }
136
137 /***********************************************************************
138  *              GetComputerNameA         (KERNEL32.165)
139  */
140 BOOL32
141 GetComputerName32A(LPSTR name,LPDWORD size) {
142         if (-1==gethostname(name,*size))
143                 return FALSE;
144         *size = lstrlen32A(name);
145         return TRUE;
146 }
147
148 /***********************************************************************
149  *              GetComputerNameW         (KERNEL32.166)
150  */
151 BOOL32
152 GetComputerName32W(LPWSTR name,LPDWORD size) {
153         LPSTR   nameA = (LPSTR)xmalloc(*size);
154
155         if (!GetComputerName32A(nameA,size)) {
156                 free(nameA);
157                 return FALSE;
158         }
159         lstrcpynAtoW(name,nameA,*size);
160         free(nameA);
161         /* FIXME : size correct? */
162         return TRUE;
163 }
164
165 /***********************************************************************
166  *           GetUserNameA   [ADVAPI32.67]
167  */
168 BOOL32 GetUserName32A(LPSTR lpszName, LPDWORD lpSize)
169 {
170   size_t len;
171   char *name;
172
173   name=getlogin();
174   len = name ? strlen(name) : 0;
175   if (!len || !lpSize || len > *lpSize) {
176     if (lpszName) *lpszName = 0;
177     return 0;
178   }
179   *lpSize=len;
180   strcpy(lpszName, name);
181   return 1;
182 }
183
184 /***********************************************************************
185  *           GetUserNameW   [ADVAPI32.68]
186  */
187 BOOL32 GetUserName32W(LPWSTR lpszName, LPDWORD lpSize)
188 {
189         LPSTR name = (LPSTR)xmalloc(*lpSize);
190         DWORD   size = *lpSize;
191         BOOL32 res = GetUserName32A(name,lpSize);
192
193         lstrcpynAtoW(lpszName,name,size);
194         return res;
195 }