1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifndef __cplusplus
28cdf0e10cSrcweir #error Need C++ to compile
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #define UNICODE
32cdf0e10cSrcweir #define _UNICODE
33cdf0e10cSrcweir #include <tchar.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #ifdef _MSC_VER
36cdf0e10cSrcweir #pragma warning(push, 1)
37cdf0e10cSrcweir #pragma warning(disable:4005)
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <windows.h>
41cdf0e10cSrcweir #include <shellapi.h>
42cdf0e10cSrcweir #include <sqlext.h>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #ifdef _MSC_VER
45cdf0e10cSrcweir #pragma warning(pop)
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // the name of the library which contains the SQLManageDataSources function
49cdf0e10cSrcweir #define ODBC_UI_LIB_NAME    L"ODBCCP32.DLL"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir // the signature of the SQLManageDataSources function
52cdf0e10cSrcweir typedef SQLRETURN (SQL_API* TSQLManageDataSource) (SQLHWND hwndParent);
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // displays the error text for the last error (GetLastError), and returns this error value
displayLastError()55cdf0e10cSrcweir int displayLastError()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	DWORD	dwError = GetLastError();
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	LPVOID lpMsgBuf;
60cdf0e10cSrcweir 	FormatMessage(
61cdf0e10cSrcweir 		FORMAT_MESSAGE_ALLOCATE_BUFFER |
62cdf0e10cSrcweir 		FORMAT_MESSAGE_FROM_SYSTEM,
63cdf0e10cSrcweir 		NULL,
64cdf0e10cSrcweir 		dwError,
65cdf0e10cSrcweir 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
66cdf0e10cSrcweir 		(LPTSTR)&lpMsgBuf,
67cdf0e10cSrcweir 		0,
68cdf0e10cSrcweir 		NULL
69cdf0e10cSrcweir 	);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	// Display the string.
72cdf0e10cSrcweir 	MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	// Free the buffer.
75cdf0e10cSrcweir 	LocalFree( lpMsgBuf );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     return dwError;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir /** registers the window class for our application's main window
81cdf0e10cSrcweir */
registerWindowClass(HINSTANCE _hAppInstance)82cdf0e10cSrcweir BOOL registerWindowClass( HINSTANCE _hAppInstance )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     WNDCLASSEX wcx;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     wcx.cbSize = sizeof(wcx);                   // size of structure
87cdf0e10cSrcweir     wcx.style = CS_HREDRAW | CS_VREDRAW;        // redraw if size changes
88cdf0e10cSrcweir     wcx.lpfnWndProc = DefWindowProc;            // points to window procedure
89cdf0e10cSrcweir     wcx.cbClsExtra = 0;                         // no extra class memory
90cdf0e10cSrcweir     wcx.cbWndExtra = 0;                         // no extra window memory
91cdf0e10cSrcweir     wcx.hInstance = _hAppInstance;              // handle to instance
92cdf0e10cSrcweir     wcx.hIcon = NULL;                           // predefined app. icon
93cdf0e10cSrcweir     wcx.hCursor = NULL;                         // predefined arrow
94cdf0e10cSrcweir     wcx.hbrBackground = NULL;                   // no background brush
95cdf0e10cSrcweir     wcx.lpszMenuName =  NULL;                   // name of menu resource
96cdf0e10cSrcweir     wcx.lpszClassName = L"ODBCConfigMainClass"; // name of window class
97cdf0e10cSrcweir     wcx.hIconSm = NULL;                         // small class icon
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     return ( NULL != RegisterClassEx( &wcx ) );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir /// initializes the application instances
initInstance(HINSTANCE _hAppInstance)103cdf0e10cSrcweir HWND initInstance( HINSTANCE _hAppInstance )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     HWND hWindow = CreateWindow(
106cdf0e10cSrcweir         L"ODBCConfigMainClass", // name of window class
107cdf0e10cSrcweir         L"ODBC Config Wrapper", // title-bar string
108cdf0e10cSrcweir         WS_OVERLAPPEDWINDOW,    // top-level window
109cdf0e10cSrcweir         CW_USEDEFAULT,          // default horizontal position
110cdf0e10cSrcweir         CW_USEDEFAULT,          // default vertical position
111cdf0e10cSrcweir         CW_USEDEFAULT,          // default width
112cdf0e10cSrcweir         CW_USEDEFAULT,          // default height
113cdf0e10cSrcweir         (HWND) NULL,            // no owner window
114cdf0e10cSrcweir         (HMENU) NULL,           // use class menu
115cdf0e10cSrcweir         _hAppInstance,          // handle to application instance
116cdf0e10cSrcweir         (LPVOID) NULL);         // no window-creation data
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     // don't show the window, we only need it as parent handle for the
119cdf0e10cSrcweir     // SQLManageDataSources function
120cdf0e10cSrcweir     return hWindow;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir // main window function
124cdf0e10cSrcweir #ifdef __MINGW32__
WinMain(HINSTANCE _hAppInstance,HINSTANCE,LPSTR,int)125cdf0e10cSrcweir extern "C" int APIENTRY WinMain( HINSTANCE _hAppInstance, HINSTANCE, LPSTR, int )
126cdf0e10cSrcweir #else
127cdf0e10cSrcweir extern "C" int APIENTRY _tWinMain( HINSTANCE _hAppInstance, HINSTANCE, LPTSTR, int )
128cdf0e10cSrcweir #endif
129cdf0e10cSrcweir {
130cdf0e10cSrcweir     if ( !registerWindowClass( _hAppInstance ) )
131cdf0e10cSrcweir         return FALSE;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     HWND hAppWindow = initInstance( _hAppInstance );
134cdf0e10cSrcweir     if ( !IsWindow( hAppWindow ) )
135cdf0e10cSrcweir         return displayLastError();
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     HMODULE hModule = LoadLibraryW( ODBC_UI_LIB_NAME );
138cdf0e10cSrcweir     if ( hModule == NULL )
139cdf0e10cSrcweir         hModule = LoadLibraryExW( ODBC_UI_LIB_NAME, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
140cdf0e10cSrcweir     if ( hModule == NULL )
141cdf0e10cSrcweir         return displayLastError();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     FARPROC pManageDSProc = GetProcAddress( hModule, "SQLManageDataSources" );
144cdf0e10cSrcweir     if ( pManageDSProc == NULL )
145cdf0e10cSrcweir         return displayLastError();
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     TSQLManageDataSource pManageDS = (TSQLManageDataSource)pManageDSProc;
148cdf0e10cSrcweir     if ( !( (*pManageDS)( hAppWindow ) ) )
149cdf0e10cSrcweir         return displayLastError();
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     FreeLibrary( hModule );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	return 0;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156