merge the formfield patch from ooo-build
[ooovba.git] / desktop / win32 / source / rebase / rebase.cxx
blob633e74723c5e9d0671d2b833343594cf98036f86
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: md5.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_desktop.hxx"
32 #define UNICODE
33 #define _UNICODE
35 #define WIN32_LEAN_AND_MEAN
36 #if defined _MSC_VER
37 #pragma warning(push, 1)
38 #endif
39 #include <windows.h>
40 #include <shellapi.h>
41 #include <imagehlp.h>
42 #include <wchar.h>
43 #if defined _MSC_VER
44 #pragma warning(pop)
45 #endif
47 #include <time.h>
48 #include "sal/config.h"
49 #include "tools/pathutils.hxx"
51 #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
52 #define MY_STRING(s) (s), MY_LENGTH(s)
54 const int FORMAT_MESSAGE_SIZE = 4096;
55 const DWORD PE_Signature = 0x00004550;
56 const DWORD BASEVIRTUALADDRESS = 0x10000000;
58 namespace
61 bool IsValidHandle( HANDLE handle )
63 return ((NULL != handle) && (INVALID_HANDLE_VALUE != handle));
66 void fail()
68 LPWSTR buf = NULL;
69 FormatMessageW(
70 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
71 GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL);
72 MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR);
73 LocalFree(buf);
74 TerminateProcess(GetCurrentProcess(), 255);
77 bool rebaseImage( wchar_t* pszFilePath, ULONG nNewImageBase)
79 ULONG ulOldImageSize;
80 ULONG_PTR lpOldImageBase;
81 ULONG ulNewImageSize;
82 ULONG_PTR lpNewImageBase = nNewImageBase;
83 ULONG ulDateTimeStamp = 0;
84 bool bResult(false);
86 char cszFilePath[_MAX_PATH+1] = {0};
87 int nResult = WideCharToMultiByte(CP_ACP, 0, pszFilePath, -1, cszFilePath, _MAX_PATH, NULL, NULL);
89 if (nResult != 0)
91 BOOL bResult = ReBaseImage(
92 cszFilePath,
93 "",
94 TRUE,
95 FALSE,
96 FALSE,
98 &ulOldImageSize,
99 &lpOldImageBase,
100 &ulNewImageSize,
101 &lpNewImageBase,
102 ulDateTimeStamp );
105 return bResult;
108 wchar_t* getBrandPath(wchar_t * path)
110 DWORD n = GetModuleFileNameW(NULL, path, MAX_PATH);
111 if (n == 0 || n >= MAX_PATH) {
112 exit(EXIT_FAILURE);
114 return tools::filename(path);
117 void rebaseImagesInFolder( wchar_t* pszFolder, DWORD nNewImageBase )
119 wchar_t szPattern[MAX_PATH];
120 wchar_t *lpLastSlash = wcsrchr( pszFolder, '\\' );
121 if ( lpLastSlash )
123 size_t len = lpLastSlash - pszFolder + 1;
124 wcsncpy( szPattern, pszFolder, len );
125 wcsncpy( szPattern + len, TEXT("*.dll"), sizeof(szPattern)/sizeof(szPattern[0]) - len );
128 WIN32_FIND_DATA aFindFileData;
129 HANDLE hFind = FindFirstFile( szPattern, &aFindFileData );
131 if ( IsValidHandle(hFind) )
133 BOOL fSuccess = false;
137 wchar_t szLibFilePath[MAX_PATH];
138 wchar_t *lpLastSlash = wcsrchr( pszFolder, '\\' );
139 if ( lpLastSlash )
141 size_t len = lpLastSlash - pszFolder + 1;
142 wcsncpy( szLibFilePath, pszFolder, len );
143 wcsncpy( szLibFilePath + len, aFindFileData.cFileName, sizeof(szLibFilePath)/sizeof(szLibFilePath[0]) - len );
146 rebaseImage( szLibFilePath, nNewImageBase );
147 fSuccess = FindNextFile( hFind, &aFindFileData );
149 while ( fSuccess );
151 FindClose( hFind );
157 extern "C" int APIENTRY WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
159 wchar_t path[MAX_PATH];
161 wchar_t * pathEnd = getBrandPath(path);
163 if (tools::buildPath(path, path, pathEnd, MY_STRING(L"")) == NULL)
164 fail();
165 rebaseImagesInFolder(path, BASEVIRTUALADDRESS);
167 if (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\basis-link")) == NULL)
168 fail();
169 pathEnd = tools::resolveLink(path);
171 if ( pathEnd == NULL )
172 return 0;
174 if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\program\\")) == NULL)
175 fail();
176 rebaseImagesInFolder(path, BASEVIRTUALADDRESS);
178 if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\ure-link")) == NULL)
179 fail();
180 pathEnd = tools::resolveLink(path);
182 if ( pathEnd == NULL )
183 return 0;
185 if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\bin\\")) == NULL)
186 fail();
187 rebaseImagesInFolder(path, BASEVIRTUALADDRESS);
189 return 0;