1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #if !defined WIN32_LEAN_AND_MEAN
22 # define WIN32_LEAN_AND_MEAN
23 #endif
24 #include <windows.h>
25 #include <odbcinst.h>
26 
27 // displays the error text for the last error (GetLastError), and returns this error value
displayLastError()28 static int displayLastError()
29 {
30     DWORD   dwError = GetLastError();
31 
32     LPVOID lpMsgBuf = nullptr;
33     FormatMessageW(
34         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
35         nullptr,
36         dwError,
37         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
38         reinterpret_cast<LPWSTR>(&lpMsgBuf),
39         0,
40         nullptr
41     );
42 
43     // Display the string.
44     MessageBoxW( nullptr, static_cast<LPCWSTR>(lpMsgBuf), nullptr, MB_OK | MB_ICONERROR );
45 
46     // Free the buffer.
47     HeapFree( GetProcessHeap(), 0, lpMsgBuf );
48 
49     return dwError;
50 }
51 
52 /** registers the window class for our application's main window
53 */
registerWindowClass(HINSTANCE _hAppInstance)54 static bool registerWindowClass( HINSTANCE _hAppInstance )
55 {
56     WNDCLASSEXW wcx;
57 
58     wcx.cbSize = sizeof(wcx);                   // size of structure
59     wcx.style = CS_HREDRAW | CS_VREDRAW;        // redraw if size changes
60     wcx.lpfnWndProc = DefWindowProcW;           // points to window procedure
61     wcx.cbClsExtra = 0;                         // no extra class memory
62     wcx.cbWndExtra = 0;                         // no extra window memory
63     wcx.hInstance = _hAppInstance;              // handle to instance
64     wcx.hIcon = nullptr;                        // predefined app. icon
65     wcx.hCursor = nullptr;                      // predefined arrow
66     wcx.hbrBackground = nullptr;                // no background brush
67     wcx.lpszMenuName =  nullptr;                // name of menu resource
68     wcx.lpszClassName = L"ODBCConfigMainClass"; // name of window class
69     wcx.hIconSm = nullptr;                      // small class icon
70 
71     return ( !!RegisterClassExW( &wcx ) );
72 }
73 
74 /// initializes the application instances
initInstance(HINSTANCE _hAppInstance)75 static HWND initInstance( HINSTANCE _hAppInstance )
76 {
77     HWND hWindow = CreateWindowW(
78         L"ODBCConfigMainClass", // name of window class
79         L"ODBC Config Wrapper", // title-bar string
80         WS_OVERLAPPEDWINDOW,    // top-level window
81         CW_USEDEFAULT,          // default horizontal position
82         CW_USEDEFAULT,          // default vertical position
83         CW_USEDEFAULT,          // default width
84         CW_USEDEFAULT,          // default height
85         nullptr,                // no owner window
86         nullptr,                // use class menu
87         _hAppInstance,          // handle to application instance
88         nullptr);               // no window-creation data
89 
90     // don't show the window, we only need it as parent handle for the
91     // SQLManageDataSources function
92     return hWindow;
93 }
94 
95 // main window function
wWinMain(HINSTANCE _hAppInstance,HINSTANCE,LPWSTR,int)96 extern "C" int APIENTRY wWinMain( HINSTANCE _hAppInstance, HINSTANCE, LPWSTR, int )
97 {
98     if ( !registerWindowClass( _hAppInstance ) )
99         return FALSE;
100 
101     HWND hAppWindow = initInstance( _hAppInstance );
102     if ( !IsWindow( hAppWindow ) )
103         return displayLastError();
104 
105     if (!SQLManageDataSources(hAppWindow))
106         return displayLastError();
107 
108     return 0;
109 }
110 
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
112