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