home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////////////
- //
- // FILE: genwin.sqc
- //
- // Generic Embedded SQL for C Win16/Win32 program
- //
- // FUNCTIONS:
- //
- // WinMain() - Main program and message loop
- // MainWndProc() - Main window procedure
- // ConnectToSQLServer() - Connect to SQL Server using CONNECT TO
- // QuerySQLServer() - Query a table in SQL Server
- // ConnectDlgProc() - Connect box dialog procedure
- // QueryDlgProc() - Query box dialog procedure
- // AboutDlgProc() - About box dialog procedure
- // ErrorHandler() - Embedded SQL for C error handler
- //
- // COMMENTS:
- //
- // Copyright (C) 1992 - 1994 Microsoft Corporation
- //
- //////////////////////////////////////////////////////////////////////////////
-
- #define STRICT
- #define WIN32_LEAN_AND_MEAN //excludes unused windows.h headers
- #include <windows.h> // standard Windows include file
- #include <windowsx.h> // extended Windows include file
- #include "gwutil.h" // utility header
- #include "genwin.h" // specific to this program
-
- // GLOBAL VARIABLES
- char szAppName[] = "GenWin"; // application name
- HINSTANCE hinstMain; // current instance
- HWND hwndMain; // main window handle
- HWND hwndEdit; // edit control handle
-
- char szLogin[SQLID_MAX+1] = ""; // SQL Server login ID
- char szPassword[SQLID_MAX+1] = ""; // SQL Server password
- char szServer[SQLID_MAX+1] = ""; // SQL Server name
- char szDatabase[SQLID_MAX+1] = ""; // SQL Server database to use
- BOOL bConnected = FALSE; // are we connected to SQL Server?
-
- EXEC SQL BEGIN DECLARE SECTION;
- char szLastName[50] = ""; // author's last name
- EXEC SQL END DECLARE SECTION;
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: WinMain()
- //
- // Creates class and window, message processing loop
- //
- // PARAMETERS:
- //
- // hinst - handle of current instance
- // hinstPrev - handle of previous instance
- // lpstrCmdLine - command line string
- // nCmdShow - show window type
- //
- // RETURNS: the value from PostQuitMessage()
- //
- // COMMENTS:
- //
- //////////////////////////////////////////////////////////////////////////////
-
- int WINAPI WinMain (
- HINSTANCE hinst,
- HINSTANCE hinstPrev,
- LPSTR lpstrCmdLine,
- int nCmdShow)
- {
- MSG msg; // message
- WNDCLASS wcMain; // windows class
- HFONT hfontFixed; // fixed font
- char chOpt; // gotten option character
- char* pszParam; // gotten parameter
- char szScratch[200] = ""; // scratch area for wsprintf()
-
- BOOL bLogin = FALSE, // bools for tracking options
- bPassword = FALSE,
- bServer = FALSE,
- bDatabase = FALSE;
-
- // install Embedded SQL for C error handler
- EXEC SQL WHENEVER SQLERROR CALL ErrorHandler();
- // set Embedded SQL for C options
- EXEC SQL SET OPTION LOGINTIME 10;
- EXEC SQL SET OPTION QUERYTIME 100;
-
- // Save the instance handle in global variable
- hinstMain = hinst;
-
- if (!hinstPrev)
- {
- // Fill in window class structure
- wcMain.style = 0; // class style(s)
- wcMain.lpfnWndProc = (WNDPROC) MainWndProc; // window procedure
- wcMain.cbClsExtra = 0; // no per-class extra data
- wcMain.cbWndExtra = 0; // no per-window extra data
- wcMain.hInstance = hinst; // app that owns this class
- wcMain.hIcon = LoadIcon(hinst, "AppIcon"); // icon from .RC file
- wcMain.hCursor = LoadCursor(NULL, IDC_ARROW); // arrow cursor
- wcMain.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); // white background
- wcMain.lpszMenuName = "AppMenu"; // menu from .RC file
- wcMain.lpszClassName = szAppName; // name used in call to CreateWindow
-
- // Register the window class
- if (!RegisterClass(&wcMain))
- return (FALSE);
- }
-
- // Create a main window for this application instance
- hwndMain = CreateWindow(szAppName, // class name
- "GenWin", // window name
- WS_OVERLAPPEDWINDOW, // window style(s)
- CW_USEDEFAULT, // init horizontal position
- CW_USEDEFAULT, // init vertical position
- CW_USEDEFAULT, // init width
- CW_USEDEFAULT, // init height
- NULL, // parent window handle
- NULL, // menu or child window ID
- hinst, // instance
- NULL); // used for MDI windows only
-
- // If main window could not be created, return failure
- if (!hwndMain)
- return (FALSE);
-
- // Create edit window
- hwndEdit = CreateWindow("edit",
- NULL,
- WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
- ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
- 0, 0, 0, 0, // initial dimensions all 0
- hwndMain, // parent handle
- (HMENU)IDE_EDIT, // unique child-window identifier
- hinst, // instance handle
- NULL);
-
- // If edit window could not be created, return failure
- if (!hwndEdit)
- return (FALSE);
-
- // get fixed font for edit control
- hfontFixed = GetStockObject(ANSI_FIXED_FONT);
- SendMessage(hwndEdit,
- WM_SETFONT,
- (WPARAM)hfontFixed,
- FALSE);
-
- SetWindowText(hwndEdit, "");
-
- // display logo
- PrintLine(hwndEdit,"Generic Embedded SQL for C Win16/Win32 application");
-
- // get options and parameters
- while (TRUE)
- {
- // all these command-line options are valid
- // {/s|/S} [sql_server]
- // {/u|/U} login_id
- // {/p|/P} [password]
- // {/d|/D} [database]
-
- chOpt = GetOption(__argc, __argv, "u:U:p:P:s:S:d:D:", &pszParam);
- if (chOpt > 1)
- {
- // chOpt is valid argument
- switch (chOpt)
- {
- case 'u': // login ID
- case 'U':
- bLogin = TRUE;
- if (pszParam != NULL)
- {
- lstrcpy(szLogin, pszParam);
- }
- break;
- case 'p': // password
- case 'P':
- bPassword = TRUE;
- if (pszParam != NULL)
- {
- lstrcpy(szPassword, pszParam);
- }
- break;
- case 's': // SQL Server
- case 'S':
- bServer = TRUE;
- if (pszParam != NULL)
- {
- lstrcpy(szServer, pszParam);
- }
- break;
- case 'd': // database
- case 'D':
- bDatabase = TRUE;
- if (pszParam != NULL)
- {
- lstrcpy(szDatabase, pszParam);
- }
- break;
- }
-
- }
- if (chOpt == 0)
- {
- // end of argument list
- break;
- }
- if ((chOpt == 1) || (chOpt == -1))
- {
- // standalone param or error
- wsprintf(szScratch, "ERROR: Argument '%s' not recognized", (LPSTR)pszParam);
- PrintLine(hwndEdit, szScratch);
- break;
- }
- }
-
- if ((chOpt == 1) || (chOpt == -1))
- {
- // error
- }
-
- ShowWindow(hwndMain, nCmdShow); // Show the window
- UpdateWindow(hwndMain); // Sends WM_PAINT message
-
- if (bServer && bLogin && bPassword && bDatabase)
- {
- // all command-line options specified, so attempt connection
- PostMessage(hwndMain, WM_CONNECTTOSQLSERVER, 0, 0);
- }
- else
- {
- // prompt for unspecified options
- Post_WM_COMMAND(hwndMain,
- IDM_FILE_CONNECT,
- 0,
- 0);
- }
-
- // Acquire and dispatch messages until a WM_QUIT message is received
- while (GetMessage(&msg, // message structure
- NULL, // handle of window receiving the message
- 0, // lowest message to examine
- 0)) // highest message to examine
- {
- TranslateMessage(&msg); // translate virtual key codes
- DispatchMessage(&msg); // dispatch message to window
- }
- return (msg.wParam); // Returns the value from PostQuitMessage
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: MainWndProc()
- //
- // Main window procedure, processes messages
- //
- // MESSAGES:
- //
- // WM_COMMAND application menu command
- // WM_CREATE create window
- // WM_DESTROY destroy window
- // WM_SETFOCUS window got focus
- // WM_SIZE window has been resized
- // WM_DESTROY window is being destroyed
- // WM_CONNECTTOSQLSERVER connect to SQL Server
- // WM_QUERYSQLSERVER query a table in SQL Server
- //
- // COMMENTS:
- //
- //////////////////////////////////////////////////////////////////////////////
-
- LRESULT CALLBACK MainWndProc (
- HWND hwnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
- {
- WORD cmdNotify, idItem;
- HWND hwndControl;
- DLGPROC lpDlgProc;
- int nWidth, nHeight;
-
- switch (msg)
- {
- case WM_COMMAND: // message: command from application menu
-
- cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
- idItem = GET_WM_COMMAND_ID(wParam, lParam);
- hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
-
- switch (idItem)
- {
- case IDM_FILE_CONNECT:
- lpDlgProc = (DLGPROC) MakeProcInstance((FARPROC)ConnectDlgProc, hinstMain);
-
- DialogBox(hinstMain,
- "ConnectBox",
- hwnd,
- lpDlgProc);
-
- FreeProcInstance((FARPROC)lpDlgProc);
- break;
-
- case IDM_FILE_QUERY:
- if (bConnected)
- {
- lpDlgProc = (DLGPROC) MakeProcInstance((FARPROC)QueryDlgProc, hinstMain);
-
- DialogBox(hinstMain,
- "QueryBox",
- hwnd,
- lpDlgProc);
-
- FreeProcInstance((FARPROC)lpDlgProc);
- }
- else
- {
- PrintLine(hwndEdit, "ERROR: Not connected to SQL Server");
- }
- break;
-
- case IDM_FILE_EXIT:
- DestroyWindow(hwnd);
- break;
-
- case IDM_HELP_ABOUT:
- lpDlgProc = (DLGPROC) MakeProcInstance((FARPROC)AboutDlgProc, hinstMain);
-
- DialogBox(hinstMain,
- "AboutBox",
- hwnd,
- lpDlgProc);
-
- FreeProcInstance((FARPROC)lpDlgProc);
- break;
-
- default:
- // pass it on if unproccessed
- return (DefWindowProc(hwnd, msg, wParam, lParam));
- break;
-
- }
- break; // case WM_COMMAND
-
- case WM_SETFOCUS: // message: window focus
- SetFocus(hwndEdit);
- break; // case WM_SETFOCUS
-
- case WM_SIZE: // message: window focus
- nWidth = LOWORD(lParam);
- nHeight = HIWORD(lParam);
- MoveWindow(hwndEdit, // window to move
- 0, // x coord
- 0, // y coord
- nWidth, // width
- nHeight, // height
- TRUE); // repaint
- break; // case WM_SIZE
-
- case WM_DESTROY: // message: window being destroyed
- EXEC SQL DISCONNECT ALL;
- PostQuitMessage (0);
- break; // case WM_DESTORY
-
- case WM_CONNECTTOSQLSERVER: // private message: connect to SQL Server
- ConnectToSQLServer(szServer,
- szLogin,
- szPassword,
- szDatabase);
- break; // case WM_CONNECTTOSQLSERVER
-
- case WM_QUERYSQLSERVER: // private message: query SQL Server
- QuerySQLServer();
- break; // case WM_CONNECTTOSQLSERVER
-
- default:
- // pass it on if unproccessed
- return (DefWindowProc(hwnd, msg, wParam, lParam));
- break;
- }
- return (0);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: ConnectToSQLServer()
- //
- // Establish connection to SQL Server
- //
- // PARAMETERS:
- //
- // szServer - connect to this SQL Server
- // szLogin - login ID
- // szPassword - password
- // szDatabase - database to use
- //
- // RETURNS: 0
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- BOOL ConnectToSQLServer (
- LPSTR szServer,
- LPSTR szLogin,
- LPSTR szPassword,
- LPSTR szDatabase)
- {
- EXEC SQL BEGIN DECLARE SECTION;
- // for CONNECT TO statement
- char szConnectToServerDatabase[(SQLID_MAX * 2)+2] = "";
- char szConnectToLoginPassword[(SQLID_MAX * 2)+2] = "";
- EXEC SQL END DECLARE SECTION;
-
- // set defaults
- if (lstrlen(szDatabase) == 0)
- {
- lstrcpy(szDatabase, "pubs");
- }
-
- PrintLine(hwndEdit, "Using:");
- PrintStr(hwndEdit, " SQL Server: ");
- PrintLine(hwndEdit, szServer);
- PrintStr(hwndEdit, " Database: ");
- PrintLine(hwndEdit, szDatabase);
- PrintStr(hwndEdit, " Login ID: ");
- PrintLine(hwndEdit, szLogin);
- PrintStr(hwndEdit, " Password: ");
- PrintLine(hwndEdit, szPassword);
-
- // build host variables for CONNECT TO
- if (lstrlen(szServer) != 0)
- {
- lstrcat(szConnectToServerDatabase, szServer);
- lstrcat(szConnectToServerDatabase, ".");
- }
- if (lstrlen(szDatabase) != 0)
- {
- lstrcat(szConnectToServerDatabase, szDatabase);
- }
-
- if (lstrlen(szLogin) != 0)
- {
- lstrcat(szConnectToLoginPassword, szLogin);
- }
- if (lstrlen(szPassword) != 0)
- {
- lstrcat(szConnectToLoginPassword, ".");
- lstrcat(szConnectToLoginPassword, szPassword);
- }
-
- // attempt connection to SQL Server
- EXEC SQL CONNECT TO :szConnectToServerDatabase
- USER :szConnectToLoginPassword;
-
- PrintStr(hwndEdit, "Connection to SQL Server '");
- PrintStr(hwndEdit, szServer);
- if (SQLCODE == 0)
- {
- bConnected = TRUE;
- PrintLine(hwndEdit, "' established");
- PrintLine(hwndEdit, "From the File menu choose Query to execute a query");
- return (TRUE);
- }
- else
- {
- // problem connecting to SQL Server
- bConnected = FALSE;
- PrintLine(hwndEdit, "' failed");
- return (FALSE);
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: QuerySQLServer()
- //
- // Query SQL Server
- //
- // PARAMETERS: none
- //
- // RETURNS: 0
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- BOOL QuerySQLServer ()
- {
- EXEC SQL BEGIN DECLARE SECTION;
- char szFirstName[50] = "";
- EXEC SQL END DECLARE SECTION;
-
- // display what user specified
- PrintStr(hwndEdit, "Author's last name: ");
- PrintLine(hwndEdit, szLastName);
-
- // query the authors table
- EXEC SQL SELECT au_fname INTO :szFirstName
- from authors where au_lname = :szLastName;
-
- if (SQLCODE == 0)
- {
- PrintStr(hwndEdit, "That author's first name is: ");
- PrintLine(hwndEdit, szFirstName);
- return (TRUE);
- }
- else
- {
- if (SQLCODE == 100)
- {
- // no rows match the given criteria
- PrintLine(hwndEdit, "That author was not found");
- }
- else
- {
- // problem executing query
- PrintLine(hwndEdit, "ERROR: Executing SELECT query");
- return (FALSE);
- }
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: ConnectDlgProc()
- //
- // Connect box dialog procedure
- //
- // MESSAGES:
- //
- // WM_INITDIALOG init dialog
- // WM_COMMAND button command
- //
- // COMMENTS:
- //
- //////////////////////////////////////////////////////////////////////////////
-
- BOOL CALLBACK ConnectDlgProc (
- HWND hwndDlg,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
- {
- WORD cmdNotify, idItem;
- HWND hwndControl;
-
- switch (msg)
- {
- case WM_INITDIALOG: // message: initialize dialog box
- SetDlgItemText(hwndDlg,
- IDC_SERVER,
- szServer);
- SetDlgItemText(hwndDlg,
- IDC_LOGIN,
- szLogin);
- SetDlgItemText(hwndDlg,
- IDC_PASSWORD,
- szPassword);
- SetDlgItemText(hwndDlg,
- IDC_DATABASE,
- szDatabase);
- return (TRUE);
- break;
-
- case WM_COMMAND: // message: received a command
-
- cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
- idItem = GET_WM_COMMAND_ID(wParam, lParam);
- hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
-
- switch (idItem)
- {
- case IDOK:
- // get server name, login, and password
- GetDlgItemText(hwndDlg,
- IDC_SERVER,
- szServer,
- SQLID_MAX);
- GetDlgItemText(hwndDlg,
- IDC_LOGIN,
- szLogin,
- SQLID_MAX);
- GetDlgItemText(hwndDlg,
- IDC_PASSWORD,
- szPassword,
- SQLID_MAX);
- GetDlgItemText(hwndDlg,
- IDC_DATABASE,
- szDatabase,
- SQLID_MAX);
-
- PostMessage(hwndMain, WM_CONNECTTOSQLSERVER, 0, 0);
-
- EndDialog(hwndDlg, TRUE);
- return (TRUE);
- break;
- case IDCANCEL:
- EndDialog(hwndDlg, TRUE);
- return (TRUE);
- break;
- }
- break;
- }
- return (FALSE); // didn't process a message
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: QueryDlgProc()
- //
- // Query box dialog procedure
- //
- // MESSAGES:
- //
- // WM_INITDIALOG init dialog
- // WM_COMMAND button command
- //
- // COMMENTS:
- //
- //////////////////////////////////////////////////////////////////////////////
-
- BOOL CALLBACK QueryDlgProc (
- HWND hwndDlg,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
- {
- WORD cmdNotify, idItem;
- HWND hwndControl;
-
- switch (msg)
- {
- case WM_INITDIALOG: // message: initialize dialog box
- return (TRUE);
- break;
-
- case WM_COMMAND: // message: received a command
-
- cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
- idItem = GET_WM_COMMAND_ID(wParam, lParam);
- hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
-
- switch (idItem)
- {
- case IDOK:
- // get author's last name for query
- GetDlgItemText(hwndDlg,
- IDC_LASTNAME,
- szLastName,
- 49);
-
- PostMessage(hwndMain, WM_QUERYSQLSERVER, 0, 0);
-
- EndDialog(hwndDlg, TRUE);
- return (TRUE);
- break;
- case IDCANCEL:
- EndDialog(hwndDlg, TRUE);
- return (TRUE);
- break;
- }
- break;
- }
- return (FALSE); // didn't process a message
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: AboutDlgProc()
- //
- // About box dialog procedure
- //
- // MESSAGES:
- //
- // WM_INITDIALOG init dialog
- // WM_COMMAND button command
- //
- // COMMENTS:
- //
- //////////////////////////////////////////////////////////////////////////////
-
- BOOL CALLBACK AboutDlgProc (
- HWND hwndDlg,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
- {
- WORD cmdNotify, idItem;
- HWND hwndControl;
-
- switch (msg)
- {
- case WM_INITDIALOG: // message: initialize dialog box
- SetDlgItemText(hwndDlg,
- IDC_PLATFORM,
- OS_STRING);
- return (TRUE);
- break;
-
- case WM_COMMAND: // message: received a command
-
- cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
- idItem = GET_WM_COMMAND_ID(wParam, lParam);
- hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
-
- switch (idItem)
- {
- case IDOK:
- case IDCANCEL:
- EndDialog(hwndDlg, TRUE);
- return (TRUE);
- break;
- }
- break;
- }
- return (FALSE); // didn't process a message
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: ErrorHandler()
- //
- // Called on Embedded SQL for C error, displays fields from SQLCA
- //
- // PARAMETERS: none
- //
- // RETURNS: none
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- void ErrorHandler (void)
- {
- char szScratch[200] = "";
-
- PrintLine(hwndEdit, "Error Handler called:");
- wsprintf(szScratch, " SQL Code = %li", SQLCODE);
- PrintLine(hwndEdit, szScratch);
- wsprintf(szScratch, " SQL Server Message %li: '%s'", SQLERRD1, (LPSTR)SQLERRMC);
- PrintLine(hwndEdit, szScratch);
- }
-