1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 
31 #ifndef __cplusplus
32 #error Need C++ to compile
33 #endif
34 
35 #define UNICODE
36 #define _UNICODE
37 #include <tchar.h>
38 
39 #ifdef _MSC_VER
40 #pragma warning(push, 1)
41 #pragma warning(disable:4005)
42 #endif
43 
44 #include <windows.h>
45 #include <shellapi.h>
46 #include <sqlext.h>
47 
48 #ifdef _MSC_VER
49 #pragma warning(pop)
50 #endif
51 
52 // the name of the library which contains the SQLManageDataSources function
53 #define ODBC_UI_LIB_NAME    L"ODBCCP32.DLL"
54 
55 // the signature of the SQLManageDataSources function
56 typedef SQLRETURN (SQL_API* TSQLManageDataSource) (SQLHWND hwndParent);
57 
58 // displays the error text for the last error (GetLastError), and returns this error value
59 int displayLastError()
60 {
61 	DWORD	dwError = GetLastError();
62 
63 	LPVOID lpMsgBuf;
64 	FormatMessage(
65 		FORMAT_MESSAGE_ALLOCATE_BUFFER |
66 		FORMAT_MESSAGE_FROM_SYSTEM,
67 		NULL,
68 		dwError,
69 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
70 		(LPTSTR)&lpMsgBuf,
71 		0,
72 		NULL
73 	);
74 
75 	// Display the string.
76 	MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
77 
78 	// Free the buffer.
79 	LocalFree( lpMsgBuf );
80 
81     return dwError;
82 }
83 
84 /** registers the window class for our application's main window
85 */
86 BOOL registerWindowClass( HINSTANCE _hAppInstance )
87 {
88     WNDCLASSEX wcx;
89 
90     wcx.cbSize = sizeof(wcx);                   // size of structure
91     wcx.style = CS_HREDRAW | CS_VREDRAW;        // redraw if size changes
92     wcx.lpfnWndProc = DefWindowProc;            // points to window procedure
93     wcx.cbClsExtra = 0;                         // no extra class memory
94     wcx.cbWndExtra = 0;                         // no extra window memory
95     wcx.hInstance = _hAppInstance;              // handle to instance
96     wcx.hIcon = NULL;                           // predefined app. icon
97     wcx.hCursor = NULL;                         // predefined arrow
98     wcx.hbrBackground = NULL;                   // no background brush
99     wcx.lpszMenuName =  NULL;                   // name of menu resource
100     wcx.lpszClassName = L"ODBCConfigMainClass"; // name of window class
101     wcx.hIconSm = NULL;                         // small class icon
102 
103     return ( NULL != RegisterClassEx( &wcx ) );
104 }
105 
106 /// initializes the application instances
107 HWND initInstance( HINSTANCE _hAppInstance )
108 {
109     HWND hWindow = CreateWindow(
110         L"ODBCConfigMainClass", // name of window class
111         L"ODBC Config Wrapper", // title-bar string
112         WS_OVERLAPPEDWINDOW,    // top-level window
113         CW_USEDEFAULT,          // default horizontal position
114         CW_USEDEFAULT,          // default vertical position
115         CW_USEDEFAULT,          // default width
116         CW_USEDEFAULT,          // default height
117         (HWND) NULL,            // no owner window
118         (HMENU) NULL,           // use class menu
119         _hAppInstance,          // handle to application instance
120         (LPVOID) NULL);         // no window-creation data
121 
122     // don't show the window, we only need it as parent handle for the
123     // SQLManageDataSources function
124     return hWindow;
125 }
126 
127 // main window function
128 #ifdef __MINGW32__
129 extern "C" int APIENTRY WinMain( HINSTANCE _hAppInstance, HINSTANCE, LPSTR, int )
130 #else
131 extern "C" int APIENTRY _tWinMain( HINSTANCE _hAppInstance, HINSTANCE, LPTSTR, int )
132 #endif
133 {
134     if ( !registerWindowClass( _hAppInstance ) )
135         return FALSE;
136 
137     HWND hAppWindow = initInstance( _hAppInstance );
138     if ( !IsWindow( hAppWindow ) )
139         return displayLastError();
140 
141     HMODULE hModule = LoadLibraryW( ODBC_UI_LIB_NAME );
142     if ( hModule == NULL )
143         hModule = LoadLibraryExW( ODBC_UI_LIB_NAME, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
144     if ( hModule == NULL )
145         return displayLastError();
146 
147     FARPROC pManageDSProc = GetProcAddress( hModule, "SQLManageDataSources" );
148     if ( pManageDSProc == NULL )
149         return displayLastError();
150 
151     TSQLManageDataSource pManageDS = (TSQLManageDataSource)pManageDSProc;
152     if ( !( (*pManageDS)( hAppWindow ) ) )
153         return displayLastError();
154 
155     FreeLibrary( hModule );
156 
157 	return 0;
158 }
159 
160