InstallShield自定義對話方塊淺談(三) (轉)

themoney發表於2007-10-04
InstallShield自定義對話方塊淺談(三) (轉)[@more@] 

/*------------------------------------------------------------------------*/

/*  */

/*  Function:  BuildServerDSN   */

/*  */

/*  Descrip:  use custom sqlserver's dsn dlg to create dsn.  */

/*   */

/*  Misc:  */

/*  */

/*------------------------------------------------------------------------*/

function BuildSQLServerDSN(bFirstStep, bAllowNotSet,szTitle)

  BOOL bDone;

  NUMBER nMessage;

  STRING szSQLAddr, szSQL, szDBUserName, szDBUserPwd, szSQLDSN;

  SHORT nDSNType;

  STRING svArg[1024];

  STRING svDSN, svAddr, svDB, svUID, svPWD;

  NUMBER nSplitPos;

  HWND hwndDlg;

  BOOL bBuildResult;

  SHORT wErrMsgLen;

  STRING szErrMsg[MAX_PATH + 1];

  LONG dwErrCode;

  begin

 

  bDone = FALSE;

// 用EzDefineDialog定義一個對話方塊

  nMessage = EzDefineDialog(DSN_SQLSERVER_DLG, ISUSER, "", DLG_DSN_SQLSERVER);

  if(nMessage = DLG_ERR) then

  MessageBox("不能進行SQLSERVER資料來源——找不到對話方塊", SEVERE);

  bDone = TRUE;

  endif;

  while(bDone = FALSE)

  nMessage = WaitOnDialog(DSN_SQLSERVER_DLG);

  switch(nMessage)

  case DLG_INIT:

     hwndDlg = CmdGetHwndDlg(DSN_SQLSERVER_DLG);

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

   if(bFirstStep) then

  _WinSubEnableControl(hwndDlg, SD_PBUT_BACK, 0);

  endif;

  _WinSubSetWindowTitle(hwndDlg, szTitle);

  CtrlSetState(DSN_SQLSERVER_DLG, IDC_RADIO_DSN_USER, BUTTON_CHECKED);

 

  case DLG_ERR:

  MessageBox("不能顯示配置SQLSERVER資料來源對話方塊", SEVERE);

  bDone = TRUE;

 

  case CANCEL:

  bDone = TRUE;

 

  case DLG_CLOSE:

   bDone = TRUE;

 

  case SD_PBUT_CONTINUE:

  // 介面新增一個dsn

  // ...

  CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_DB_ADDR, szSQLAddr);

  CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_DB, szSQLDB);

  CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_USER_NAME, szDBUserName);

  CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_USER_PWD, szDBUserPwd);

  CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_DSN, szSQLDSN);

  if(StrLength(szSQLAddr) = 0) then

  if(AskYesNo("沒有輸入,是否以後進行配置?", YES) = NO) then

  _WinSubFocuntrol(hwndDlg, IDC_EDIT_DB_ADDR);

  else

  bDone = TRUE;

  endif;

  elseif(StrLength(szSQLDB) = 0) then

  if(AskYesNo("沒有輸入,是否以後進行配置?", YES) = NO) then

  _WinSubFocusControl(hwndDlg, IDC_EDIT_SQLSERVER_DB);

  else

  bDone = TRUE;

  endif;

   elseif(StrLength(szDBUserName) = 0) then

  if(AskYesNo("沒有輸入資料庫,是否以後進行配置?", YES) = NO) then

  _WinSubFocusControl(hwndDlg, IDC_EDIT_SQLSERVER_USER_NAME);

  else

  bDone = TRUE;

  endif;

  elseif(StrLength(szSQLDSN) = 0) then

  if(AskYesNo("沒有輸入資料來源名稱,是否以後進行配置?", YES) = NO) then

  _WinSubFocusControl(hwndDlg, IDC_EDIT_SQLSERVER_DSN);

  else

  bDone = TRUE;

  endif;

  else

  // 已經獲取了所有資料,可以呼叫SQLConfigData新增資料來源了

  // 組織語句

  // 注:由於DSN不儲存UID和PWD,所以在Attribute中不能加入這兩個

  //  關鍵字,並且每個關鍵子之間用''分割,由於InstallShield

  //  中不支援一個字串中間存在'',因此,不能直接用+連線這些

  //  關鍵字,下面是一個變通的方法

  if(CtrlGetState(DSN_SQLSERVER_DLG, IDC_RADIO_DSN_USER) = BUTTON_CHECKED) then

   nDSNType = ODBC_ADD_DSN;

  else

  nDSNType = ODBC_ADD_SYS_DSN;

  endif;

  svDSN = "DSN=" + szSQLDSN;

  svAddr = "SERVER=" + szSQLAddr;

  svDB = "DATABASE=" + szSQLDB;

  svUID = "UID=" + szDBUserName;

  svPWD = "PWD=" + szDBUserPwd;

  svArg = svDSN + ' ' + svAddr + ' ' + svDB;

  nSplitP= StrLength(svDSN);

  svArg[nSplitPos] = '';

  nSplitPos += StrLength(svAddr) + 1;

  svArg[nSplitPos] = '';

  nSplitPos += StrLength(svDB) + 1;

  svArg[nSplitPos] = '';

  // 呼叫ODBCCP32中的SQLConfigDataSource新增一個DSN

  // 如果返回FALSE,表示新增失敗,這時可以呼叫

  // SQLInstallerError來得到失敗的原因

  bBuildResult = SQLConfigDataSource(NULL, nDSNType, "", svArg);

  if(bBuildResult) then

     // 如果新增成功

    bDone = TRUE;

    else

    // 新增失敗 

    wErrMsgLen = MAX_PATH;

    SQLInstallerError(1, &dwErrCode, szErrMsg, wErrMsgLen, &wErrMsgLen);

    if(bAllowNotSet) then

       szErrMsg = "配置資料來源失敗——" + szErrMsg + "!是否以後進行配置?";

    if(AskYesNo(szErrMsg, YES) = NO) then

    bDone = FALSE;

    else

    bDone = TRUE;

    endif;

    else

    szErrMsg = "配置資料來源失敗——" + szErrMsg + "!";

    MessageBox(szErrMsg, SEVERE);

    endif;

    endif;

   endif;

 

  case SD_PBUT_BACK:

  // 上一步

  bDone = TRUE;

 

  case SD_PBUT_EXITSETUP:

  bDone = TRUE;

 

  case IDC_RADIO_DSN_USER:

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

 

  case IDC_RADIO_DSN_SYSTEM:

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

 

  case IDC_EDIT_DB_ADDR:

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

 

  case IDC_EDIT_SQLSERVER_USER_NAME:

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

 

  case IDC_EDIT_SQLSERVER_USER_PWD:

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

 

  case IDC_EDIT_SQLSERVER_DSN:

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

 

  case IDC_EDIT_SQLSERVER_DB:

  CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

   

  endswitch;

 

  endwhile;

 

  EndDialog(DSN_SQLSERVER_DLG);

  ReleaseDialog(DSN_SQLSERVER_DLG);

 

  if(nMessage = SD_PBUT_CONTINUE) then

  return NEXT;

  elseif(nMessage = SD_PBUT_BACK) then

  return BACK;

  else

  Do(EXIT);

  endif;

  end;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10794571/viewspace-974717/,如需轉載,請註明出處,否則將追究法律責任。

相關文章