2 * msvcrt.dll dll data items
4 * Copyright 2000 Jon Griffiths
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
22 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 unsigned int MSVCRT_basemajor;/* FIXME: */
34 unsigned int MSVCRT_baseminor;/* FIXME: */
35 unsigned int MSVCRT_baseversion; /* FIXME: */
36 unsigned int MSVCRT__commode;
37 unsigned int MSVCRT__fmode;
38 unsigned int MSVCRT_osmajor;/* FIXME: */
39 unsigned int MSVCRT_osminor;/* FIXME: */
40 unsigned int MSVCRT_osmode;/* FIXME: */
41 unsigned int MSVCRT__osver;
42 unsigned int MSVCRT_osversion; /* FIXME: */
43 unsigned int MSVCRT__winmajor;
44 unsigned int MSVCRT__winminor;
45 unsigned int MSVCRT__winver;
46 unsigned int MSVCRT___setlc_active;
47 unsigned int MSVCRT___unguarded_readlc_active;
50 MSVCRT_wchar_t **MSVCRT___wargv;
52 MSVCRT_wchar_t *MSVCRT__wcmdln;
54 MSVCRT_wchar_t **_wenviron = 0;
55 char **MSVCRT___initenv = 0;
56 MSVCRT_wchar_t **MSVCRT___winitenv = 0;
58 char* MSVCRT__pgmptr = 0;
59 WCHAR* MSVCRT__wpgmptr = 0;
61 /* Get a snapshot of the current environment
62 * and construct the __p__environ array
64 * The pointer returned from GetEnvironmentStrings may get invalid when
65 * some other module cause a reallocation of the env-variable block
67 * blk is an array of pointers to environment strings, ending with a NULL
68 * and after that the actual copy of the environment strings, ending in a \0
70 char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
72 char* environ_strings = GetEnvironmentStringsA();
73 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
76 for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
79 len += strlen(ptr) + 1;
82 blk = HeapReAlloc( GetProcessHeap(), 0, blk, count* sizeof(char*) + len );
84 blk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(char*) + len );
90 memcpy(&blk[count],environ_strings,len);
91 for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
98 FreeEnvironmentStringsA(environ_strings);
102 MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
104 MSVCRT_wchar_t* wenviron_strings = GetEnvironmentStringsW();
105 int count = 1, len = 1, i = 0; /* keep space for the trailing NULLS */
106 MSVCRT_wchar_t *wptr;
108 for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
111 len += strlenW(wptr) + 1;
114 wblk = HeapReAlloc( GetProcessHeap(), 0, wblk, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
116 wblk = HeapAlloc(GetProcessHeap(), 0, count* sizeof(MSVCRT_wchar_t*) + len * sizeof(MSVCRT_wchar_t));
121 memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
122 for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
129 FreeEnvironmentStringsW(wenviron_strings);
133 typedef void (*_INITTERMFUN)(void);
135 /***********************************************************************
136 * __p___argc (MSVCRT.@)
138 int* CDECL __p___argc(void) { return &MSVCRT___argc; }
140 /***********************************************************************
141 * __p__commode (MSVCRT.@)
143 unsigned int* CDECL __p__commode(void) { return &MSVCRT__commode; }
146 /***********************************************************************
147 * __p__pgmptr (MSVCRT.@)
149 char** CDECL __p__pgmptr(void) { return &MSVCRT__pgmptr; }
151 /***********************************************************************
152 * __p__wpgmptr (MSVCRT.@)
154 WCHAR** CDECL __p__wpgmptr(void) { return &MSVCRT__wpgmptr; }
156 /***********************************************************************
157 * __p__fmode (MSVCRT.@)
159 unsigned int* CDECL __p__fmode(void) { return &MSVCRT__fmode; }
161 /***********************************************************************
162 * __p__osver (MSVCRT.@)
164 unsigned int* CDECL __p__osver(void) { return &MSVCRT__osver; }
166 /***********************************************************************
167 * __p__winmajor (MSVCRT.@)
169 unsigned int* CDECL __p__winmajor(void) { return &MSVCRT__winmajor; }
171 /***********************************************************************
172 * __p__winminor (MSVCRT.@)
174 unsigned int* CDECL __p__winminor(void) { return &MSVCRT__winminor; }
176 /***********************************************************************
177 * __p__winver (MSVCRT.@)
179 unsigned int* CDECL __p__winver(void) { return &MSVCRT__winver; }
181 /*********************************************************************
182 * __p__acmdln (MSVCRT.@)
184 char** CDECL __p__acmdln(void) { return &MSVCRT__acmdln; }
186 /*********************************************************************
187 * __p__wcmdln (MSVCRT.@)
189 MSVCRT_wchar_t** CDECL __p__wcmdln(void) { return &MSVCRT__wcmdln; }
191 /*********************************************************************
192 * __p___argv (MSVCRT.@)
194 char*** CDECL __p___argv(void) { return &MSVCRT___argv; }
196 /*********************************************************************
197 * __p___wargv (MSVCRT.@)
199 MSVCRT_wchar_t*** CDECL __p___wargv(void) { return &MSVCRT___wargv; }
201 /*********************************************************************
202 * __p__environ (MSVCRT.@)
204 char*** CDECL __p__environ(void)
207 _environ = msvcrt_SnapshotOfEnvironmentA(NULL);
211 /*********************************************************************
212 * __p__wenviron (MSVCRT.@)
214 MSVCRT_wchar_t*** CDECL __p__wenviron(void)
217 _wenviron = msvcrt_SnapshotOfEnvironmentW(NULL);
221 /*********************************************************************
222 * __p___initenv (MSVCRT.@)
224 char*** CDECL __p___initenv(void) { return &MSVCRT___initenv; }
226 /*********************************************************************
227 * __p___winitenv (MSVCRT.@)
229 MSVCRT_wchar_t*** CDECL __p___winitenv(void) { return &MSVCRT___winitenv; }
231 /* INTERNAL: Create a wide string from an ascii string */
232 static MSVCRT_wchar_t *wstrdupa(const char *str)
234 const size_t len = strlen(str) + 1 ;
235 MSVCRT_wchar_t *wstr = MSVCRT_malloc(len* sizeof (MSVCRT_wchar_t));
238 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,str,len,wstr,len);
242 /* INTERNAL: Since we can't rely on Winelib startup code calling w/getmainargs,
243 * we initialise data values during DLL loading. When called by a native
244 * program we simply return the data we've already initialised. This also means
245 * you can call multiple times without leaking
247 void msvcrt_init_args(void)
251 MSVCRT__acmdln = _strdup( GetCommandLineA() );
252 MSVCRT__wcmdln = wstrdupa(MSVCRT__acmdln);
253 MSVCRT___argc = __wine_main_argc;
254 MSVCRT___argv = __wine_main_argv;
255 MSVCRT___wargv = __wine_main_wargv;
257 TRACE("got %s, wide = %s argc=%d\n", debugstr_a(MSVCRT__acmdln),
258 debugstr_w(MSVCRT__wcmdln),MSVCRT___argc);
260 version = GetVersion();
261 MSVCRT__osver = version >> 16;
262 MSVCRT__winminor = version & 0xFF;
263 MSVCRT__winmajor = (version>>8) & 0xFF;
264 MSVCRT_baseversion = version >> 16;
265 MSVCRT__winver = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
266 MSVCRT_baseminor = (version >> 16) & 0xFF;
267 MSVCRT_basemajor = (version >> 24) & 0xFF;
268 MSVCRT_osversion = version & 0xFFFF;
269 MSVCRT_osminor = version & 0xFF;
270 MSVCRT_osmajor = (version>>8) & 0xFF;
271 MSVCRT__HUGE = HUGE_VAL;
272 MSVCRT___setlc_active = 0;
273 MSVCRT___unguarded_readlc_active = 0;
274 MSVCRT__fmode = MSVCRT__O_TEXT;
276 MSVCRT___initenv= msvcrt_SnapshotOfEnvironmentA(NULL);
277 MSVCRT___winitenv= msvcrt_SnapshotOfEnvironmentW(NULL);
279 MSVCRT__pgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
282 if (!GetModuleFileNameA(0, MSVCRT__pgmptr, MAX_PATH))
283 MSVCRT__pgmptr[0] = '\0';
285 MSVCRT__pgmptr[MAX_PATH - 1] = '\0';
288 MSVCRT__wpgmptr = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
291 if (!GetModuleFileNameW(0, MSVCRT__wpgmptr, MAX_PATH))
292 MSVCRT__wpgmptr[0] = '\0';
294 MSVCRT__wpgmptr[MAX_PATH - 1] = '\0';
299 /* INTERNAL: free memory used by args */
300 void msvcrt_free_args(void)
302 /* FIXME: more things to free */
303 HeapFree(GetProcessHeap(), 0, MSVCRT___initenv);
304 HeapFree(GetProcessHeap(), 0, MSVCRT___winitenv);
305 HeapFree(GetProcessHeap(), 0, _environ);
306 HeapFree(GetProcessHeap(), 0, _wenviron);
307 HeapFree(GetProcessHeap(), 0, MSVCRT__pgmptr);
308 HeapFree(GetProcessHeap(), 0, MSVCRT__wpgmptr);
311 /*********************************************************************
312 * __getmainargs (MSVCRT.@)
314 void CDECL __getmainargs(int *argc, char** *argv, char** *envp,
315 int expand_wildcards, int *new_mode)
317 TRACE("(%p,%p,%p,%d,%p).\n", argc, argv, envp, expand_wildcards, new_mode);
318 *argc = MSVCRT___argc;
319 *argv = MSVCRT___argv;
320 *envp = MSVCRT___initenv;
322 MSVCRT__set_new_mode( *new_mode );
325 /*********************************************************************
326 * __wgetmainargs (MSVCRT.@)
328 void CDECL __wgetmainargs(int *argc, MSVCRT_wchar_t** *wargv, MSVCRT_wchar_t** *wenvp,
329 int expand_wildcards, int *new_mode)
331 TRACE("(%p,%p,%p,%d,%p).\n", argc, wargv, wenvp, expand_wildcards, new_mode);
332 *argc = MSVCRT___argc;
333 *wargv = MSVCRT___wargv;
334 *wenvp = MSVCRT___winitenv;
336 MSVCRT__set_new_mode( *new_mode );
339 /*********************************************************************
340 * _initterm (MSVCRT.@)
342 unsigned int CDECL _initterm(_INITTERMFUN *start,_INITTERMFUN *end)
344 _INITTERMFUN* current = start;
345 unsigned int count=0;
347 TRACE("(%p,%p)\n",start,end);
352 TRACE("Call init function %p\n",*current);
362 /*********************************************************************
363 * __set_app_type (MSVCRT.@)
365 void CDECL MSVCRT___set_app_type(int app_type)
367 TRACE("(%d) %s application\n", app_type, app_type == 2 ? "Gui" : "Console");
368 MSVCRT_app_type = app_type;