Release 960712
[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 "kernel32.h"
13 #include "handle32.h"
14 #include "except.h"
15 #include "task.h"
16 #include "stddebug.h"
17 #define DEBUG_WIN32
18 #include "debug.h"
19   
20 /* The global error value
21  */
22 int WIN32_LastError;
23
24 /* Standard system handles for stdin, stdout, and stderr.
25  */
26 FILE_OBJECT *hstdin, *hstdout, *hstderr;
27
28 static int CreateStdHandles(void);
29
30 /*********************************************************************
31  *              CloseHandle             (KERNEL32.23)
32  */
33 BOOL CloseHandle(KERNEL_OBJECT *handle)
34 {
35     int rc;
36
37     if(ValidateKernelObject(handle) != 0)
38     {
39         SetLastError(ERROR_INVALID_HANDLE);
40         return 0;
41     }
42
43     switch(handle->magic)
44     {
45         case KERNEL_OBJECT_UNUSED:
46             SetLastError(ERROR_INVALID_HANDLE);
47             return 0;
48
49         case KERNEL_OBJECT_FILE:
50             rc = CloseFileHandle((FILE_OBJECT *)handle);
51             break;
52
53         default:
54             dprintf_win32(stddeb, "CloseHandle: type %ld not implemented yet.\n",
55                    handle->magic);
56             break;
57     }
58
59     ReleaseKernelObject(handle);
60     return 0;
61 }
62
63 /***********************************************************************
64  *              GetModuleFileNameA      (KERNEL32.235)
65  */
66 DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
67 {
68     strcpy(lpFilename, "c:\\dummy");
69     return 8;
70 }
71
72 /***********************************************************************
73  *              GetModuleHandle         (KERNEL32.237)
74  */
75 HMODULE WIN32_GetModuleHandle(char *module)
76 {
77     HMODULE hModule;
78
79     dprintf_win32(stddeb, "GetModuleHandle: %s\n", module ? module : "NULL");
80 /* Freecell uses the result of GetModuleHandleA(0) as the hInstance in
81 all calls to e.g. CreateWindowEx. */
82     if (module == NULL) {
83         TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
84         hModule = pTask->hInstance;
85     } else
86         hModule = GetModuleHandle(module);
87     dprintf_win32(stddeb, "GetModuleHandle: returning %d\n", hModule );
88     return hModule;
89 }
90
91 /***********************************************************************
92  *              GetStartupInfoA         (KERNEL32.273)
93  */
94 VOID GetStartupInfoA(LPSTARTUPINFO lpStartupInfo)
95 {
96     lpStartupInfo->cb = sizeof(STARTUPINFO);
97     lpStartupInfo->lpReserved = NULL;
98     lpStartupInfo->lpDesktop = "Desktop";
99     lpStartupInfo->lpTitle = "Title";
100
101     lpStartupInfo->cbReserved2 = 0;
102     lpStartupInfo->lpReserved2 = NULL; /* must be NULL for VC runtime */
103     lpStartupInfo->hStdInput  = (HANDLE)0;
104     lpStartupInfo->hStdOutput = (HANDLE)1;
105     lpStartupInfo->hStdError  = (HANDLE)2;
106 }
107
108 /* Initialize whatever internal data structures we need.
109  *
110  * Returns 1 on success, 0 on failure.
111  */
112 int KERN32_Init(void)
113 {
114 #ifndef WINELIB
115     /* Initialize exception handling */
116     EXC_Init();
117 #endif
118
119     /* Create the standard system handles
120      */
121     if(CreateStdHandles() != 0)
122         return 0;
123
124     return 1;
125 }
126
127 /* CreateStdHandles creates the standard input, output, and error handles.
128  * These handles aren't likely to be used since they're generally used for
129  * console output, but startup code still likes to mess with them.  They're
130  * also useful for debugging since apps and runtime libraries might write
131  * errors to stderr.
132  *
133  * Returns 0 on success, nonzero on failure.
134  */
135 static int CreateStdHandles(void)
136 {
137     /* Create the standard input handle.
138      */
139     hstdin = (FILE_OBJECT *)CreateKernelObject(sizeof(FILE_OBJECT));
140     if(hstdin == NULL)
141         return 1;
142     hstdin->common.magic = KERNEL_OBJECT_FILE;
143     hstdin->fd = 0;
144     hstdin->type = FILE_TYPE_CHAR;
145     hstdin->misc_flags = 0;
146
147     /* Create the standard output handle
148      */
149     hstdout = (FILE_OBJECT *)CreateKernelObject(sizeof(FILE_OBJECT));
150     if(hstdout == NULL)
151         return 1;
152     hstdout->common.magic = KERNEL_OBJECT_FILE;
153     hstdout->fd = 1;
154     hstdout->type = FILE_TYPE_CHAR;
155     hstdout->misc_flags = 0;
156
157     /* Create the standard error handle
158      */
159     hstderr = (FILE_OBJECT *)CreateKernelObject(sizeof(FILE_OBJECT));
160     if(hstderr == NULL)
161         return 1;
162     hstderr->common.magic = KERNEL_OBJECT_FILE;
163     hstderr->fd = 2;
164     hstderr->type = FILE_TYPE_CHAR;
165     hstderr->misc_flags = 0;
166
167     return 0;
168 }