Main Page | Modules | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages | Examples

apr_app.c

Go to the documentation of this file.
00001 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
00002  * applicable.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /* Usage Notes:
00018  *
00019  *   this module, and the misc/win32/utf8.c modules must be 
00020  *   compiled APR_EXPORT_STATIC and linked to an application with
00021  *   the /entry:wmainCRTStartup flag.  This module becomes the true
00022  *   wmain entry point, and passes utf-8 reformatted argv and env
00023  *   arrays to the application's main function.
00024  *
00025  *   This module is only compatible with Unicode-only executables.
00026  *   Mixed (Win9x backwards compatible) binaries should refer instead
00027  *   to the apr_startup.c module.
00028  *
00029  *   _dbg_malloc/realloc is used in place of the usual API, in order
00030  *   to convince the MSVCRT that they created these entities.  If we
00031  *   do not create them as _CRT_BLOCK entities, the crt will fault
00032  *   on an assert.  We are not worrying about the crt's locks here, 
00033  *   since we are single threaded [so far].
00034  */
00035 
00036 #include "apr_general.h"
00037 #include "ShellAPI.h"
00038 #include "crtdbg.h"
00039 #include "wchar.h"
00040 #include "apr_arch_file_io.h"
00041 #include "assert.h"
00042 #include "apr_private.h"
00043 #include "apr_arch_misc.h"
00044 
00045 /* This symbol is _private_, although it must be exported.
00046  */
00047 
00048 extern int main(int argc, const char **argv, const char **env);
00049 
00050 int wmain(int argc, const wchar_t **wargv, const wchar_t **wenv)
00051 {
00052     char **argv;
00053     char **env;
00054     int dupenv;
00055 
00056     (void)apr_wastrtoastr(&argv, wargv, argc);
00057 
00058     dupenv = apr_wastrtoastr(&env, wenv, -1);
00059 
00060     _environ = _malloc_dbg((dupenv + 1) * sizeof (char *), 
00061                            _CRT_BLOCK, __FILE__, __LINE__ );
00062     memcpy(_environ, env, (dupenv + 1) * sizeof (char *));
00063 
00064     /* MSVCRT will attempt to maintain the wide environment calls
00065      * on _putenv(), which is bogus if we've passed a non-ascii
00066      * string to _putenv(), since they use MultiByteToWideChar
00067      * and breaking the implicit utf-8 assumption we've built.
00068      *
00069      * Reset _wenviron for good measure.
00070      */
00071     if (_wenviron) {
00072         wenv = _wenviron;
00073         _wenviron = NULL;
00074         free((wchar_t **)wenv);
00075     }
00076 
00077     apr_app_init_complete = 1;
00078 
00079     return main(argc, argv, env);
00080 }