xref: /AOO41X/main/setup_native/source/win32/customactions/tools/checkversion.cxx (revision 32b1fd08cf0851da51c0ed68f50bc63c4ee660e0)
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 #include <msiquery.h>
31 #ifdef _MSC_VER
32 #pragma warning(pop)
33 #endif
34 
35 #include <string.h>
36 #include <malloc.h>
37 #include <stdio.h>
38 #include "strsafe.h"
39 
40 #include <seterror.hxx>
41 
42 //----------------------------------------------------------
GetMsiProp(MSIHANDLE hMSI,const wchar_t * pPropName,wchar_t ** ppValue)43 BOOL GetMsiProp( MSIHANDLE hMSI, const wchar_t* pPropName, wchar_t** ppValue )
44 {
45     DWORD sz = 0;
46     if ( MsiGetProperty( hMSI, pPropName, L"", &sz ) == ERROR_MORE_DATA )
47     {
48         sz++;
49         DWORD nbytes = sz * sizeof( wchar_t );
50         wchar_t* buff = reinterpret_cast<wchar_t*>( malloc( nbytes ) );
51         ZeroMemory( buff, nbytes );
52         MsiGetProperty( hMSI, pPropName, buff, &sz );
53         *ppValue = buff;
54 
55         return TRUE;
56     }
57 
58     return FALSE;
59 }
60 
61 //----------------------------------------------------------
62 #ifdef DEBUG
OutputDebugStringFormat(LPCTSTR pFormat,...)63 inline void OutputDebugStringFormat( LPCTSTR pFormat, ... )
64 {
65     TCHAR    buffer[1024];
66     va_list  args;
67 
68     va_start( args, pFormat );
69     StringCchVPrintf( buffer, sizeof(buffer), pFormat, args );
70     OutputDebugString( buffer );
71 }
72 #else
OutputDebugStringFormat(LPCTSTR,...)73 static inline void OutputDebugStringFormat( LPCTSTR, ... )
74 {
75 }
76 #endif
77 
78 //----------------------------------------------------------
CheckVersions(MSIHANDLE hMSI)79 extern "C" UINT __stdcall CheckVersions( MSIHANDLE hMSI )
80 {
81     // MessageBox(NULL, L"CheckVersions", L"Information", MB_OK | MB_ICONINFORMATION);
82 
83     wchar_t* pVal = NULL;
84 
85     if ( GetMsiProp( hMSI, L"NEWPRODUCTS", &pVal ) && pVal )
86     {
87         OutputDebugStringFormat( TEXT("DEBUG: NEWPRODUCTS found [%s]"), pVal );
88         if ( *pVal != 0 )
89             SetMsiErrorCode( MSI_ERROR_NEW_VERSION_FOUND );
90         free( pVal );
91     }
92     pVal = NULL;
93     if ( GetMsiProp( hMSI, L"SAMEPRODUCTS", &pVal ) && pVal )
94     {
95         OutputDebugStringFormat( TEXT("DEBUG: SAMEPRODUCTS found [%s]"), pVal );
96         if ( *pVal != 0 )
97             SetMsiErrorCode( MSI_ERROR_SAME_VERSION_FOUND );
98         free( pVal );
99     }
100     pVal = NULL;
101     if ( GetMsiProp( hMSI, L"OLDPRODUCTS", &pVal ) && pVal )
102     {
103         OutputDebugStringFormat( TEXT("DEBUG: OLDPRODUCTS found [%s]"), pVal );
104         if ( *pVal != 0 )
105             SetMsiErrorCode( MSI_ERROR_OLD_VERSION_FOUND );
106         free( pVal );
107     }
108     pVal = NULL;
109     if ( GetMsiProp( hMSI, L"BETAPRODUCTS", &pVal ) && pVal )
110     {
111         OutputDebugStringFormat( TEXT("DEBUG: BETAPRODUCTS found [%s]"), pVal );
112         if ( *pVal != 0 )
113             SetMsiErrorCode( MSI_ERROR_OLD_VERSION_FOUND );
114         free( pVal );
115     }
116 
117     pVal = NULL;
118     if ( GetMsiProp( hMSI, L"NEWPRODUCTSPATCH", &pVal ) && pVal )
119     {
120         OutputDebugStringFormat( TEXT("DEBUG: NEWPRODUCTSPATCH found [%s]"), pVal );
121         if ( *pVal != 0 )
122             SetMsiErrorCode( MSI_ERROR_NEW_PATCH_FOUND );
123         free( pVal );
124     }
125     pVal = NULL;
126     if ( GetMsiProp( hMSI, L"SAMEPRODUCTSPATCH", &pVal ) && pVal )
127     {
128         OutputDebugStringFormat( TEXT("DEBUG: SAMEPRODUCTSPATCH found [%s]"), pVal );
129         if ( *pVal != 0 )
130             SetMsiErrorCode( MSI_ERROR_SAME_PATCH_FOUND );
131         free( pVal );
132     }
133     pVal = NULL;
134     if ( GetMsiProp( hMSI, L"OLDPRODUCTSPATCH", &pVal ) && pVal )
135     {
136         OutputDebugStringFormat( TEXT("DEBUG: OLDPRODUCTSPATCH found [%s]"), pVal );
137         if ( *pVal != 0 )
138             SetMsiErrorCode( MSI_ERROR_OLD_PATCH_FOUND );
139         free( pVal );
140     }
141 
142     return ERROR_SUCCESS;
143 }
144 
145 
146