ntdll: Make sure we don't try to attach the main exe a second time.
[wine/zf.git] / dlls / kernel32 / kernel_main.c
blobcdbb4b9a386e2d0e8df8b990454e4b2888e2af75
1 /*
2 * Kernel initialization code
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <signal.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winnls.h"
33 #include "wincon.h"
34 #include "winternl.h"
36 #include "wine/library.h"
37 #include "kernel_private.h"
38 #include "console_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(process);
43 extern int CDECL __wine_set_signal_handler(unsigned, int (*)(unsigned));
45 /***********************************************************************
46 * set_entry_point
48 static void set_entry_point( HMODULE module, const char *name, DWORD rva )
50 IMAGE_EXPORT_DIRECTORY *exports;
51 DWORD exp_size;
53 if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
54 IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
56 DWORD *functions = (DWORD *)((char *)module + exports->AddressOfFunctions);
57 const WORD *ordinals = (const WORD *)((const char *)module + exports->AddressOfNameOrdinals);
58 const DWORD *names = (const DWORD *)((const char *)module + exports->AddressOfNames);
59 int min = 0, max = exports->NumberOfNames - 1;
61 while (min <= max)
63 int res, pos = (min + max) / 2;
64 const char *ename = (const char *)module + names[pos];
65 if (!(res = strcmp( ename, name )))
67 WORD ordinal = ordinals[pos];
68 assert( ordinal < exports->NumberOfFunctions );
69 TRACE( "setting %s at %p to %08x\n", name, &functions[ordinal], rva );
70 functions[ordinal] = rva;
71 return;
73 if (res > 0) max = pos - 1;
74 else min = pos + 1;
80 /***********************************************************************
81 * KERNEL process initialisation routine
83 static BOOL process_attach( HMODULE module )
85 RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
87 kernel32_handle = module;
88 RtlSetUnhandledExceptionFilter( UnhandledExceptionFilter );
90 NtQuerySystemInformation( SystemBasicInformation, &system_info, sizeof(system_info), NULL );
92 /* Setup computer name */
93 COMPUTERNAME_Init();
95 CONSOLE_Init(params);
97 /* copy process information from ntdll */
98 ENV_CopyStartupInformation();
100 if (!(GetVersion() & 0x80000000))
102 /* Securom checks for this one when version is NT */
103 set_entry_point( module, "FT_Thunk", 0 );
105 else
107 LDR_MODULE *ldr;
109 if (LdrFindEntryForAddress( GetModuleHandleW( 0 ), &ldr ) || !(ldr->Flags & LDR_WINE_INTERNAL))
110 LoadLibraryA( "krnl386.exe16" );
113 /* finish the process initialisation for console bits, if needed */
114 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
116 if (params->ConsoleHandle == KERNEL32_CONSOLE_ALLOC)
118 HMODULE mod = GetModuleHandleA(0);
119 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
120 AllocConsole();
122 /* else TODO for DETACHED_PROCESS:
123 * 1/ inherit console + handles
124 * 2/ create std handles, if handles are not inherited
125 * TBD when not using wineserver handles for console handles
128 return TRUE;
131 /***********************************************************************
132 * KERNEL initialisation routine
134 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
136 switch(reason)
138 case DLL_PROCESS_ATTACH:
139 DisableThreadLibraryCalls( hinst );
140 return process_attach( hinst );
141 case DLL_PROCESS_DETACH:
142 WritePrivateProfileSectionW( NULL, NULL, NULL );
143 CONSOLE_Exit();
144 break;
146 return TRUE;
149 /***********************************************************************
150 * MulDiv (KERNEL32.@)
151 * RETURNS
152 * Result of multiplication and division
153 * -1: Overflow occurred or Divisor was 0
155 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
157 LONGLONG ret;
159 if (!nDivisor) return -1;
161 /* We want to deal with a positive divisor to simplify the logic. */
162 if (nDivisor < 0)
164 nMultiplicand = - nMultiplicand;
165 nDivisor = -nDivisor;
168 /* If the result is positive, we "add" to round. else, we subtract to round. */
169 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
170 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
171 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
172 else
173 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
175 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
176 return ret;
179 /******************************************************************************
180 * GetSystemRegistryQuota (KERNEL32.@)
182 BOOL WINAPI GetSystemRegistryQuota(PDWORD pdwQuotaAllowed, PDWORD pdwQuotaUsed)
184 FIXME("(%p, %p) faking reported quota values\n", pdwQuotaAllowed, pdwQuotaUsed);
186 if (pdwQuotaAllowed)
187 *pdwQuotaAllowed = 2 * 1000 * 1000 * 1000; /* 2 GB */
189 if (pdwQuotaUsed)
190 *pdwQuotaUsed = 100 * 1000 * 1000; /* 100 MB */
192 return TRUE;