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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef __cplusplus
28 #error Need C++ to compile
29 #endif
30 
31 #define UNICODE
32 #define _UNICODE
33 #include <tchar.h>
34 
35 #ifdef _MSC_VER
36 #pragma warning(push, 1)
37 #pragma warning(disable:4005)
38 #endif
39 
40 #include <windows.h>
41 #include <shellapi.h>
42 #include <sqlext.h>
43 
44 #ifdef _MSC_VER
45 #pragma warning(pop)
46 #endif
47 
48 // the name of the library which contains the SQLManageDataSources function
49 #define ODBC_UI_LIB_NAME    L"ODBCCP32.DLL"
50 
51 // the signature of the SQLManageDataSources function
52 typedef SQLRETURN (SQL_API* TSQLManageDataSource) (SQLHWND hwndParent);
53 
54 // displays the error text for the last error (GetLastError), and returns this error value
displayLastError()55 int displayLastError()
56 {
57 	DWORD	dwError = GetLastError();
58 
59 	LPVOID lpMsgBuf;
60 	FormatMessage(
61 		FORMAT_MESSAGE_ALLOCATE_BUFFER |
62 		FORMAT_MESSAGE_FROM_SYSTEM,
63 		NULL,
64 		dwError,
65 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
66 		(LPTSTR)&lpMsgBuf,
67 		0,
68 		NULL
69 	);
70 
71 	// Display the string.
72 	MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
73 
74 	// Free the buffer.
75 	LocalFree( lpMsgBuf );
76 
77     return dwError;
78 }
79 
80 /** registers the window class for our application's main window
81 */
registerWindowClass(HINSTANCE _hAppInstance)82 BOOL registerWindowClass( HINSTANCE _hAppInstance )
83 {
84     WNDCLASSEX wcx;
85 
86     wcx.cbSize = sizeof(wcx);                   // size of structure
87     wcx.style = CS_HREDRAW | CS_VREDRAW;        // redraw if size changes
88     wcx.lpfnWndProc = DefWindowProc;            // points to window procedure
89     wcx.cbClsExtra = 0;                         // no extra class memory
90     wcx.cbWndExtra = 0;                         // no extra window memory
91     wcx.hInstance = _hAppInstance;              // handle to instance
92     wcx.hIcon = NULL;                           // predefined app. icon
93     wcx.hCursor = NULL;                         // predefined arrow
94     wcx.hbrBackground = NULL;                   // no background brush
95     wcx.lpszMenuName =  NULL;                   // name of menu resource
96     wcx.lpszClassName = L"ODBCConfigMainClass"; // name of window class
97     wcx.hIconSm = NULL;                         // small class icon
98 
99     return ( NULL != RegisterClassEx( &wcx ) );
100 }
101 
102 /// initializes the application instances
initInstance(HINSTANCE _hAppInstance)103 HWND initInstance( HINSTANCE _hAppInstance )
104 {
105     HWND hWindow = CreateWindow(
106         L"ODBCConfigMainClass", // name of window class
107         L"ODBC Config Wrapper", // title-bar string
108         WS_OVERLAPPEDWINDOW,    // top-level window
109         CW_USEDEFAULT,          // default horizontal position
110         CW_USEDEFAULT,          // default vertical position
111         CW_USEDEFAULT,          // default width
112         CW_USEDEFAULT,          // default height
113         (HWND) NULL,            // no owner window
114         (HMENU) NULL,           // use class menu
115         _hAppInstance,          // handle to application instance
116         (LPVOID) NULL);         // no window-creation data
117 
118     // don't show the window, we only need it as parent handle for the
119     // SQLManageDataSources function
120     return hWindow;
121 }
122 
123 // main window function
124 #ifdef __MINGW32__
WinMain(HINSTANCE _hAppInstance,HINSTANCE,LPSTR,int)125 extern "C" int APIENTRY WinMain( HINSTANCE _hAppInstance, HINSTANCE, LPSTR, int )
126 #else
127 extern "C" int APIENTRY _tWinMain( HINSTANCE _hAppInstance, HINSTANCE, LPTSTR, int )
128 #endif
129 {
130     if ( !registerWindowClass( _hAppInstance ) )
131         return FALSE;
132 
133     HWND hAppWindow = initInstance( _hAppInstance );
134     if ( !IsWindow( hAppWindow ) )
135         return displayLastError();
136 
137     HMODULE hModule = LoadLibraryW( ODBC_UI_LIB_NAME );
138     if ( hModule == NULL )
139         hModule = LoadLibraryExW( ODBC_UI_LIB_NAME, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
140     if ( hModule == NULL )
141         return displayLastError();
142 
143     FARPROC pManageDSProc = GetProcAddress( hModule, "SQLManageDataSources" );
144     if ( pManageDSProc == NULL )
145         return displayLastError();
146 
147     TSQLManageDataSource pManageDS = (TSQLManageDataSource)pManageDSProc;
148     if ( !( (*pManageDS)( hAppWindow ) ) )
149         return displayLastError();
150 
151     FreeLibrary( hModule );
152 
153 	return 0;
154 }
155 
156