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