1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #define UNICODE
25 
26 #ifdef _MSC_VER
27 #pragma warning(push,1) // disable warnings within system headers
28 #endif
29 #include <windows.h>
30 #ifdef _MSC_VER
31 #pragma warning(pop)
32 #endif
33 
34 #include <string.h>
35 #include <malloc.h>
36 #include <stdio.h>
37 #include "strsafe.h"
38 
39 #include <seterror.hxx>
40 
41 //----------------------------------------------------------
42 #ifdef DEBUG
OutputDebugStringFormat(LPCTSTR pFormat,...)43 inline void OutputDebugStringFormat( LPCTSTR pFormat, ... )
44 {
45 	TCHAR    buffer[1024];
46 	va_list  args;
47 
48 	va_start( args, pFormat );
49 	StringCchVPrintf( buffer, sizeof(buffer), pFormat, args );
50 	OutputDebugString( buffer );
51 }
52 #else
OutputDebugStringFormat(LPCTSTR,...)53 static inline void OutputDebugStringFormat( LPCTSTR, ... )
54 {
55 }
56 #endif
57 
58 //----------------------------------------------------------
SetMsiErrorCode(int nErrorCode)59 void SetMsiErrorCode( int nErrorCode )
60 {
61     const TCHAR sMemMapName[] = TEXT( "Global\\MsiErrorObject" );
62 
63     HANDLE hMapFile;
64     int *pBuf;
65 
66     hMapFile = OpenFileMapping(
67                     FILE_MAP_ALL_ACCESS,    // read/write access
68                     FALSE,                  // do not inherit the name
69                     sMemMapName );          // name of mapping object
70 
71     if ( hMapFile == NULL )                 // can not set error code
72     {
73         OutputDebugStringFormat( TEXT("Could not open map file (%d).\n"), GetLastError() );
74         return;
75     }
76 
77     pBuf = (int*) MapViewOfFile( hMapFile,   // handle to map object
78                         FILE_MAP_ALL_ACCESS, // read/write permission
79                         0,
80                         0,
81                         sizeof( int ) );
82     if ( pBuf )
83     {
84         *pBuf = nErrorCode;
85         UnmapViewOfFile( pBuf );
86     }
87     else
88         OutputDebugStringFormat( TEXT("Could not map view of file (%d).\n"), GetLastError() );
89 
90     CloseHandle( hMapFile );
91 }
92 
93 
94