ntdll: Rename local variables in heap_reallocate.
[wine.git] / dlls / kernel32 / kernel_main.c
blob7b45db732577eb824766f89280d3fd86785d784c
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 <ctype.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <signal.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wincon.h"
30 #include "winternl.h"
32 #include "kernel_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(process);
37 static STARTUPINFOA startup_infoA;
39 /***********************************************************************
40 * set_entry_point
42 #ifdef __i386__
43 static void set_entry_point( HMODULE module, const char *name, DWORD rva )
45 IMAGE_EXPORT_DIRECTORY *exports;
46 DWORD exp_size;
48 if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
49 IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
51 DWORD *functions = (DWORD *)((char *)module + exports->AddressOfFunctions);
52 const WORD *ordinals = (const WORD *)((const char *)module + exports->AddressOfNameOrdinals);
53 const DWORD *names = (const DWORD *)((const char *)module + exports->AddressOfNames);
54 int min = 0, max = exports->NumberOfNames - 1;
56 while (min <= max)
58 int res, pos = (min + max) / 2;
59 const char *ename = (const char *)module + names[pos];
60 if (!(res = strcmp( ename, name )))
62 WORD ordinal = ordinals[pos];
63 DWORD oldprot;
65 TRACE( "setting %s at %p to %08lx\n", name, &functions[ordinal], rva );
66 VirtualProtect( functions + ordinal, sizeof(*functions), PAGE_READWRITE, &oldprot );
67 functions[ordinal] = rva;
68 VirtualProtect( functions + ordinal, sizeof(*functions), oldprot, &oldprot );
69 return;
71 if (res > 0) max = pos - 1;
72 else min = pos + 1;
76 #endif
79 /***********************************************************************
80 * GetStartupInfoA (KERNEL32.@)
82 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
84 *info = startup_infoA;
87 static void copy_startup_info(void)
89 RTL_USER_PROCESS_PARAMETERS* rupp;
90 ANSI_STRING ansi;
92 RtlAcquirePebLock();
94 rupp = NtCurrentTeb()->Peb->ProcessParameters;
96 startup_infoA.cb = sizeof(startup_infoA);
97 startup_infoA.lpReserved = NULL;
98 startup_infoA.lpDesktop = !RtlUnicodeStringToAnsiString( &ansi, &rupp->Desktop, TRUE ) ? ansi.Buffer : NULL;
99 startup_infoA.lpTitle = !RtlUnicodeStringToAnsiString( &ansi, &rupp->WindowTitle, TRUE ) ? ansi.Buffer : NULL;
100 startup_infoA.dwX = rupp->dwX;
101 startup_infoA.dwY = rupp->dwY;
102 startup_infoA.dwXSize = rupp->dwXSize;
103 startup_infoA.dwYSize = rupp->dwYSize;
104 startup_infoA.dwXCountChars = rupp->dwXCountChars;
105 startup_infoA.dwYCountChars = rupp->dwYCountChars;
106 startup_infoA.dwFillAttribute = rupp->dwFillAttribute;
107 startup_infoA.dwFlags = rupp->dwFlags;
108 startup_infoA.wShowWindow = rupp->wShowWindow;
109 startup_infoA.cbReserved2 = rupp->RuntimeInfo.MaximumLength;
110 startup_infoA.lpReserved2 = rupp->RuntimeInfo.MaximumLength ? (void*)rupp->RuntimeInfo.Buffer : NULL;
111 startup_infoA.hStdInput = rupp->hStdInput ? rupp->hStdInput : INVALID_HANDLE_VALUE;
112 startup_infoA.hStdOutput = rupp->hStdOutput ? rupp->hStdOutput : INVALID_HANDLE_VALUE;
113 startup_infoA.hStdError = rupp->hStdError ? rupp->hStdError : INVALID_HANDLE_VALUE;
115 RtlReleasePebLock();
118 /***********************************************************************
119 * KERNEL process initialisation routine
121 static BOOL process_attach( HMODULE module )
123 RtlSetUnhandledExceptionFilter( UnhandledExceptionFilter );
125 NtQuerySystemInformation( SystemBasicInformation, &system_info, sizeof(system_info), NULL );
126 kernelbase_global_data = KernelBaseGetGlobalData();
128 copy_startup_info();
130 #ifdef __i386__
131 if (!(GetVersion() & 0x80000000))
133 /* Securom checks for this one when version is NT */
134 set_entry_point( module, "FT_Thunk", 0 );
136 else
138 LDR_DATA_TABLE_ENTRY *ldr;
140 if (LdrFindEntryForAddress( GetModuleHandleW( 0 ), &ldr ) || !(ldr->Flags & LDR_WINE_INTERNAL))
141 LoadLibraryA( "krnl386.exe16" );
143 #endif
144 return TRUE;
147 /***********************************************************************
148 * KERNEL initialisation routine
150 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
152 switch(reason)
154 case DLL_PROCESS_ATTACH:
155 DisableThreadLibraryCalls( hinst );
156 return process_attach( hinst );
157 case DLL_PROCESS_DETACH:
158 WritePrivateProfileSectionW( NULL, NULL, NULL );
159 break;
161 return TRUE;
164 /***********************************************************************
165 * MulDiv (KERNEL32.@)
166 * RETURNS
167 * Result of multiplication and division
168 * -1: Overflow occurred or Divisor was 0
170 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
172 LONGLONG ret;
174 if (!nDivisor) return -1;
176 /* We want to deal with a positive divisor to simplify the logic. */
177 if (nDivisor < 0)
179 nMultiplicand = - nMultiplicand;
180 nDivisor = -nDivisor;
183 /* If the result is positive, we "add" to round. else, we subtract to round. */
184 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
185 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
186 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
187 else
188 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
190 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
191 return ret;
194 /******************************************************************************
195 * GetSystemRegistryQuota (KERNEL32.@)
197 BOOL WINAPI GetSystemRegistryQuota(PDWORD pdwQuotaAllowed, PDWORD pdwQuotaUsed)
199 FIXME("(%p, %p) faking reported quota values\n", pdwQuotaAllowed, pdwQuotaUsed);
201 if (pdwQuotaAllowed)
202 *pdwQuotaAllowed = 2 * 1000 * 1000 * 1000; /* 2 GB */
204 if (pdwQuotaUsed)
205 *pdwQuotaUsed = 100 * 1000 * 1000; /* 100 MB */
207 return TRUE;