home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sysmgmt / sms / smsview / nt / smsview.c < prev    next >
C/C++ Source or Header  |  1996-10-15  |  14KB  |  455 lines

  1. /*************************************************************************
  2.  
  3. SMSVIEW.EXE - This Module creates Views based on the current Group Map in
  4. SMS
  5.  
  6. Copyright (c), 1994 by Microsoft Corp.
  7.  
  8. Modified: 7/11/95 a-cvadai  Added error message messageboxes.
  9.  
  10. *************************************************************************/
  11.  
  12. #define DBNTWIN32       // must identify operating system environment
  13. #include "windows.h"
  14.  
  15. #include <ctl3d.h>
  16.  
  17.  
  18. #include <sqlfront.h>
  19. #include <sqldb.h>     // DB-LIB header file (should always be included)
  20. #include <stdio.h>
  21. #include <winnt.h>
  22. #include "smsview.h"
  23. #include "smsviews.h"
  24. #include "resource.h"
  25.  
  26. typedef int (*PFNDLL)();
  27. struct DBLOGIN {
  28.  char ServerName[MAX_NAME];
  29.  char LoginId   [MAX_NAME];
  30.  char Password  [MAX_NAME];
  31.  char DBName    [MAX_NAME];
  32.  int DropOnly;
  33.  } dbLoginInfo;
  34. int CmdComplete;
  35.  
  36. void ErrorMsg(int ,int);
  37.  
  38. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  39. HINSTANCE hInstance;                 /* current instance         */
  40. HINSTANCE hPrevInstance;                 /* previous instance        */
  41. LPSTR lpCmdLine;                 /* command line             */
  42. int nCmdShow;                    /* show-window type (open/icon) */
  43. {
  44.  
  45.     PFNDLL GenViews = NULL;
  46.     static HANDLE hLib = NULL;
  47.  
  48.  
  49.  
  50.     // Forward declarations of the error handler and message handler.
  51.  
  52.     if (!hPrevInstance)         /* Has application been initialized? */
  53.       if (!SqlTestInit(hInstance))
  54.         return (0);             /* Exits if unable to initialize     */
  55.  
  56.     hInst = hInstance;          /* Saves the current instance        */
  57.  
  58.     Ctl3dRegister( hInst );
  59.     Ctl3dAutoSubclass( hInst );
  60.  
  61.     CmdComplete = CheckCommandLine(lpCmdLine);
  62.     if ( ! GetConnectionInfo(CmdComplete))
  63.      return(0);
  64.  
  65.    Ctl3dUnregister( hInst );
  66.  
  67.    return(1);
  68. }
  69.  
  70.  int CheckCommandLine(char * lpCmdLine)
  71.  {
  72.   int Len;
  73.   char * pSlash1 = lpCmdLine;
  74.  
  75.   memset (&dbLoginInfo,'\0',sizeof(dbLoginInfo));
  76.  
  77.   if ( strlen(lpCmdLine) == 0)
  78.    return(0);
  79.   while ( (pSlash1 = (strchr(pSlash1,'/'))) ) { // Find First Param
  80.  
  81.        // Find Next Param
  82.   //  pSlash2 = (strchr(pSlash1+1,'/');
  83.  
  84.    if ( (Len = (strchr(pSlash1,' ') - pSlash1)) < 0 )
  85.      Len = MAX_NAME;
  86.    if ( Len > MAX_NAME){
  87.     ErrorMsg(IDS_ERR_CMD_TOO_LONG,DISP);
  88.     return(0);
  89.    }
  90.  
  91.     Len-=(strchr(pSlash1,':')-pSlash1)+1;    //Subtract the /?: off the string Length
  92.     if ( Len < 0)  //Invalid switch
  93.      return(0);
  94.  
  95.     pSlash1++; //Point to First char of the keyword
  96.     switch (toupper(pSlash1[0])) {
  97.      case  SERVERNAMEPARAM :
  98.            strncpy(dbLoginInfo.ServerName,strchr(pSlash1,':')+1,Len);
  99.            break;
  100.      case  LOGINIDPARAM :
  101.            strncpy(dbLoginInfo.LoginId,strchr(pSlash1,':')+1,Len);
  102.            break;
  103.      case  PASSWORDPARAM:
  104.            strncpy(dbLoginInfo.Password,strchr(pSlash1,':')+1,Len);
  105.            break;
  106.      case  DBNAMEPARAM:
  107.            strncpy(dbLoginInfo.DBName,strchr(pSlash1,':')+1,Len);
  108.            break;
  109.     }
  110.   } // End While
  111.  
  112.    return(1);
  113.  }
  114. //GetConnectionInfo
  115. GetConnectionInfo(CmdComplete)
  116.  int CmdComplete;
  117. {
  118.     DWORD Er;
  119.     if(  !CmdComplete ) {
  120.          if( DialogBoxParam(hInst, TEXT("IDD_ConnectionDlg"), NULL,
  121.                               (DLGPROC)dbConnect, 2 )== -1){
  122.                     Er = GetLastError();
  123.                     return(0);
  124.          }
  125.      }
  126.      else
  127.         if ( SetupDbConnection() == FAIL)
  128.              return(FAIL);
  129.  
  130.      return(1);
  131. }
  132. /***************************************************************************\
  133. * dbConnect()
  134. *
  135. * GetConnection Details from DLG Box
  136. *
  137. * History:
  138. * 6/5/92  2am
  139. *   Created.
  140. \***************************************************************************/
  141. LRESULT WINAPI dbConnect(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  142. {
  143.  
  144.     static char ServerName[MAX_NAME]  ;
  145.     static char * lpszHold = ServerName;
  146.     HCURSOR CurArrow,CurWait;
  147.  
  148.     switch (message){
  149.       case WM_INITDIALOG:{
  150.  
  151.              // Set Default Server Name
  152.         SendDlgItemMessage( hDlg, IDC_SERVERNAME, MAX_NAME,
  153.                             (WPARAM)MAX_NAME, (LPARAM)0);
  154.  
  155.         if( SendDlgItemMessage( hDlg, IDC_SERVERNAME, WM_SETTEXT, 0,
  156.                                 (LPARAM)lpszHold) == LB_ERR ){
  157.             //ErrorMsg(TEXT("Rename: Add Source String failure."),DISP);
  158.             return(1);
  159.         }
  160.         //Set default Login ID
  161.         lstrcpy(lpszHold,"sa");
  162.  
  163.          SendDlgItemMessage( hDlg, IDC_LOGINID, MAX_NAME,
  164.                             (WPARAM)MAX_NAME, (LPARAM)0);
  165.  
  166.         if( SendDlgItemMessage( hDlg, IDC_LOGINID, WM_SETTEXT, 0,
  167.                                 (LPARAM)lpszHold) == LB_ERR ){
  168.             //ErrorMsg(TEXT("Rename: Add Source String failure."),DISP);
  169.             return(1);
  170.         }
  171.          // Set deafult Data Base Name
  172.          lstrcpy (lpszHold,"SMS");
  173.  
  174.          SendDlgItemMessage( hDlg, IDC_DBNAME, MAX_NAME,
  175.                             (WPARAM)MAX_NAME, (LPARAM)0);
  176.  
  177.         if( SendDlgItemMessage( hDlg, IDC_DBNAME ,WM_SETTEXT, 0,
  178.                                 (LPARAM)lpszHold) == LB_ERR ){
  179.             //ErrorMsg(TEXT("Rename: Add Source String failure."),DISP);
  180.             return(1);
  181.         }
  182.         //Set Deafult password length
  183.          SendDlgItemMessage( hDlg, IDC_PASSWORD, MAX_NAME,
  184.                             (WPARAM)MAX_NAME, (LPARAM)0);
  185.  
  186.         return(1);
  187.       }
  188.       case WM_COMMAND:{
  189.         switch(LOWORD(wParam)){
  190.             case IDOK:{
  191.  
  192.                 if( SendDlgItemMessage( hDlg, IDC_SERVERNAME, WM_GETTEXT,
  193.                                         MAX_NAME,
  194.                                         (LPARAM)lpszHold) == LB_ERR ){
  195.                     //ErrorMsg(TEXT("Rename: Get Source String failure."),DISP);
  196.                     EndDialog(hDlg, wParam);
  197.                     return(1);
  198.                 }
  199.                 lstrcpy(dbLoginInfo.ServerName,lpszHold);
  200.                 if( SendDlgItemMessage( hDlg, IDC_LOGINID, WM_GETTEXT,
  201.                                         MAX_NAME,
  202.                                         (LPARAM)lpszHold) == LB_ERR ){
  203.                     //ErrorMsg(TEXT("Rename: Get Source String failure."),DISP);
  204.                     EndDialog(hDlg, wParam);
  205.                     return(1);
  206.                 }
  207.                 lstrcpy(dbLoginInfo.LoginId,lpszHold);
  208.                 if( SendDlgItemMessage( hDlg, IDC_DBNAME, WM_GETTEXT,
  209.                                         MAX_NAME,
  210.                                         (LPARAM)lpszHold) == LB_ERR ){
  211.                     //ErrorMsg(TEXT("Rename: Get Source String failure."),DISP);
  212.                     EndDialog(hDlg, wParam);
  213.                     return(1);
  214.                 }
  215.                 lstrcpy(dbLoginInfo.DBName,lpszHold);
  216.                 if( SendDlgItemMessage( hDlg, IDC_PASSWORD, WM_GETTEXT,
  217.                                         MAX_NAME,
  218.                                         (LPARAM)lpszHold) == LB_ERR ){
  219.                     //ErrorMsg(TEXT("Rename: Get Source String failure."),DISP);
  220.                     EndDialog(hDlg, wParam);
  221.                     return(1);
  222.                 }
  223.                 lstrcpy(dbLoginInfo.Password,lpszHold);
  224.  
  225.                 if ( (dbLoginInfo.DropOnly = SendDlgItemMessage( hDlg, IDC_DROPONLY, BM_GETCHECK,
  226.                                         MAX_NAME,
  227.                                         (LPARAM)0)) == LB_ERR ){
  228.                     //ErrorMsg(TEXT("Rename: Get Source String failure."),DISP);
  229.                     EndDialog(hDlg, wParam);
  230.                     return(1);
  231.                 }
  232.                 CurWait = LoadCursor(NULL, IDC_WAIT);
  233.                 CurArrow = SetCursor(CurWait);
  234.                 if ( SetupDbConnection() == FAIL){
  235.                     SetCursor(CurArrow);
  236.                  break;
  237.                  }
  238.               SetCursor(CurArrow);
  239.             }
  240.             case IDCANCEL:{
  241.  
  242.                 EndDialog(hDlg, wParam);
  243.                 return(1);
  244.             }
  245.         }
  246.         return(1);
  247.       } // WM_COMMAND
  248.     }
  249.  
  250.     return(0);
  251. }
  252.  
  253. /*********************************************************************
  254. * Connect to the DataBase with Indo supplied                         *
  255. **********************************************************************/
  256.  
  257. int SetupDbConnection()
  258. {
  259.     DBPROCESS *dbproc;      // allocate a DB-LIB process structure
  260.     DBPROCESS *dbprocView;  // Allocate a proc to create the Views
  261.     LOGINREC  *login;       // allocate a DB-LIB login structure
  262.     static HANDLE hLib = NULL;
  263.     PFNDLL GenViews = NULL;
  264.     char CreateGroupName[MAX_COLUMN_NAME];
  265.     int RetCode;
  266.  
  267.     CreateGroupName[0] = '\0';
  268.  
  269.     if (dbinit() == (char *)NULL)
  270.     {
  271.         ErrorMsg(IDS_ERR_COM_NOT_LOADED, DISP);
  272.         return(1);
  273.     }
  274.  
  275.     // Install the user-supplied error-handling and message-handling
  276.     // routines. They are defined at the bottom of this source file.
  277.  
  278.     dberrhandle((void *)err_handler);
  279.     dbmsghandle((void *)err_handler);
  280.  
  281.     login = dblogin();                   // get login record from DB-LIB
  282.     DBSETLUSER (login, (char *)dbLoginInfo.LoginId); // set the username
  283.     DBSETLAPP (login, (char *)"smsview");              // set the application name
  284.     DBSETLPWD (login, (char *)dbLoginInfo.Password); // set the SQL Server password
  285.  
  286.     // Now attempt to create and initialize a DBPROCESS structure
  287.     if ((dbproc = dbopen (login, dbLoginInfo.ServerName)) == NULL)
  288.         return (FAIL);
  289.  
  290.     // and another one for The Create View Process
  291.     if ((dbprocView = dbopen (login, dbLoginInfo.ServerName)) == NULL)
  292.         return (FAIL);
  293.  
  294.  
  295.     if ( dbuse (dbproc, dbLoginInfo.DBName) == FAIL) {     // use the "SMS" database
  296.         dbexit();
  297.       return(FAIL);
  298.     }
  299.     if ( dbuse (dbprocView,dbLoginInfo.DBName) == FAIL) {
  300.         dbexit();
  301.       return(FAIL);
  302.     }
  303.  
  304.     if (!(hLib = LoadLibrary ("VIEWDLL.DLL")))
  305.             ErrorMsg(IDS_ERR_LOAD_VIEWDLL, DISP);
  306.     else {
  307.        GenViews = (PFNDLL) GetProcAddress (hLib,"GenViews");
  308.        RetCode = (GenViews)(dbproc,dbprocView,CreateGroupName,dbLoginInfo.DropOnly,CmdComplete);
  309.        GenResults(RetCode);
  310.      }
  311.     // Close the connection and exit
  312.     dbexit();
  313.  
  314.    return(1);
  315.   }
  316. //GenResults
  317.  
  318. void GenResults(int RetCode)
  319. {
  320.     char szMsg[MAX_PATH];
  321.     char szMsgTitle[32];
  322.  
  323.     LoadString(hInst, IDS_MSC_CREATE_VIEWS, szMsgTitle, 32);
  324.  
  325.     if (RetCode == SUCCEED) {
  326.         if ( !dbLoginInfo.DropOnly) {
  327.             LoadString(hInst, IDS_MSG_CREATE_VIEW, szMsg, MAX_PATH);
  328.         }
  329.         else {
  330.             LoadString(hInst, IDS_MSG_DROP_VIEW, szMsg, MAX_PATH);
  331.         }
  332.     }
  333.     else {
  334.         if ( !dbLoginInfo.DropOnly) {
  335.             LoadString(hInst, IDS_ERR_CREATE_VIEW, szMsg, MAX_PATH);
  336.         }
  337.         else {
  338.             LoadString(hInst, IDS_ERR_DROP_VIEW, szMsg, MAX_PATH);
  339.         }
  340.     }
  341.  
  342.     MessageBox(NULL, szMsg, szMsgTitle, MB_ICONINFORMATION );
  343.  
  344. }
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351. //ErrorMsg
  352. void ErrorMsg(int iString, int Disp)
  353. {
  354. #if 0
  355.   static HANDLE hEventLog = NULL;
  356.   char *pErrorStrings[1];
  357.  
  358.   pErrorStrings[0] = errorString;
  359.  
  360.   if (hEventLog == (HANDLE) NULL)
  361.      hEventLog = RegisterEventSource((HANDLE)NULL,"SMS View");
  362.  
  363.  
  364.   if (! CmdComplete && Disp)  // is it run with command line parameters
  365.    MessageBox( NULL, errorString, "Data Base Error", MB_ICONSTOP );
  366.  
  367.   if ( hEventLog != NULL)
  368.    ReportEvent(hEventLog, EVENTLOG_INFORMATION_TYPE, (WORD)0, (DWORD) 0, NULL
  369.    ,1,0, pErrorStrings, NULL);
  370. #endif
  371.  
  372.     char    szString[MAX_PATH];
  373.     char    szMsgTitle[32];
  374.  
  375.     if (Disp == DISP) {
  376.         LoadString(hInst, iString, szString, MAX_PATH);
  377.         LoadString(hInst, IDS_MSC_DBERROR, szMsgTitle, 32);
  378.         MessageBox(NULL, szString, szMsgTitle, MB_ICONSTOP);
  379.     }
  380.   }
  381.  
  382.  
  383.  
  384. /****************************************************************************
  385.  
  386.     FUNCTION: SqlTestInit(HANDLE)
  387.  
  388.     PURPOSE: Initializes window data and registers window class
  389.  
  390.     COMMENTS:
  391.  
  392.     Sets up a structure to register the window class.  Structure includes
  393.     such information as what function will process messages, what cursor
  394.     and icon to use, etc.
  395.  
  396. ****************************************************************************/
  397.  
  398. BOOL SqlTestInit(hInstance)
  399. HANDLE hInstance;                  /* current instance       */
  400. {
  401.     HANDLE hMemory;                /* handle to allocated memory */
  402.     PWNDCLASS pWndClass;               /* structure pointer      */
  403.     BOOL bSuccess;                 /* RegisterClass() result     */
  404.  
  405.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  406.     pWndClass = (PWNDCLASS)hMemory;
  407.  
  408.     pWndClass->style = 0; /*CS_HREDRAW | CS_VREDRAW; */
  409.     pWndClass->lpfnWndProc = (WNDPROC)NULL;
  410.     pWndClass->hInstance = hInstance;
  411.     pWndClass->hIcon = LoadIcon(hInstance,MAKEINTRESOURCE( IDI_SMSVIEW));
  412.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  413.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  414.     pWndClass->lpszMenuName = (LPSTR)"SMS Views";
  415.     pWndClass->lpszClassName = (LPSTR)"SMS Views";
  416.  
  417.     bSuccess = RegisterClass(pWndClass);
  418.  
  419.     LocalFree(hMemory);                 /* Returns it to NT */
  420.     return (bSuccess);       /* Returns result of registering the window */
  421. }
  422.  
  423. //
  424. int err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char * dberrstr, char * oserrstr)
  425. {
  426.     char    szString[512];
  427.     char    szMsgTitle[32];
  428.     int     iString;
  429.  
  430.     // Ignore any 'Changed context' errors. (#5701 dec)
  431.  
  432.     if (severity == 0x00001645) {
  433.         return 0;
  434.     }
  435.  
  436.     LoadString(hInst, IDS_ERR_SQL_ERROR, szString, MAX_PATH);
  437.     LoadString(hInst, IDS_MSC_DBERROR, szMsgTitle, 32);
  438.  
  439.     iString = sizeof(szString) - strlen(szString);
  440.     if (dberrstr != NULL) {
  441.         strncpy(szString, dberrstr, iString);
  442.     }
  443.     else if (oserr != DBNOERR && oserrstr != NULL) {
  444.         strncpy(szString, oserrstr, iString);
  445.     }
  446.     szString[sizeof(szString)-1] = 0;
  447.  
  448.     if (strstr(szString, "General SQL Server") == NULL) {
  449.         MessageBox(NULL, szString, szMsgTitle, MB_ICONSTOP);
  450.     }
  451.  
  452.     return(INT_CANCEL);
  453. }
  454.  
  455.