SQL Server Performance

DELPHI source for custom MSDE Install

Discussion in 'Contribute Your SQL Server Scripts' started by Chappy, May 27, 2004.

  1. Chappy New Member

    Whilst this forum is really for T-SQL, I chose to post this as it will hopefully be useful to others. <br /><br />A bit of background.. The MSDE installer launches several processes to do subinstalls. These communicate their progress back to the main installer by broadcasting windows messages to all top level windows (very odd architecture design by MS if you ask me).<br /><br />The problem with this is that if you write a custom installer which launches the MSDE install, then the MSDE installer is no longer a top level window, and the MSDE install, well, stalls! The solution is to write a message handler into your custom installer which forwards the necessary messages onto the MSDE install window, allowing it to complete.<br /><br />Thanks to argyle for web research into this, the code below is a modified port of Microsoft C source.<br /><br /><pre><br />function TForm_Installer.DoMSDEInstall :integer;<br />var<br /> iResult :integer;<br /> dwECode <img src='/community/emoticons/emotion-2.gif' alt=':D' />WORD;<br /> msg :TMsg;<br /> si<img src='/community/emoticons/emotion-7.gif' alt=':S' />TARTUPINFO;<br /> pi:TPROCESSINFORMATION;<br /> fSuccess :boolean;<br /> szCommandLine <img src='/community/emoticons/emotion-7.gif' alt=':s' />tring;<br /> instance <img src='/community/emoticons/emotion-7.gif' alt=':s' />tring;<br />begin<br /> iResult := 1;<br /><br /> FillChar(si,SizeOf(TStartupInfo),#0);<br /> FillChar(pi,SizeOf(TProcessInformation),#0);<br /> si.cb := SizeOf(TStartupInfo);<br /> si.dwFlags := si.dwFlags or STARTF_USESHOWWINDOW;<br /> si.wShowWindow := 0;<br /><br /> instance := 'A_DEFAULT_INSTANCE_NAME';<br /> if (ParamCount &gt;= 1) then<br /> instance := Trim(ParamStr(1));<br /> szCommandLine := ExtractFilePath(ParamStr(0)) + 'setup.exe INSTANCENAME=' + instance + ' SAPWD=ASTRONGPASSWORD';<br /><br /> // Start the SQL Server Desktop Engine Setup process by sending the appropriate parameters.<br /> // You must customize this code according to your installation requirements.<br /> si.cb := sizeof(si);<br /> fSuccess := CreateProcess(nil, PChar(szCommandLine), nil, nil, FALSE, 0, nil, nil, si, pi);<br /><br /> if (fSuccess) then<br /> begin<br /> Hide;<br /> CloseHandle(pi.hThread);<br /><br /> // The following code replaces the call to WaitForSingleObject(pi.hProcess, INFINITE);<br /> while (true) do<br /> begin<br /> while (PeekMessage(msg, 0, 0, 0, PM_REMOVE)) do<br /> begin<br /> TranslateMessage(msg);<br /> // If it is a quit message, exit.<br /> if (msg.message = WM_QUIT) then<br /> begin<br /> result := 1;<br /> break;<br /> end;<br /> <br /> // Otherwise, dispatch the message.<br /> DispatchMessage(msg);<br /> end;<br /><br /> // Wait for any message that is sent to or is posted to this queue<br /> // or for the process handle to be signaled.<br /> if (MsgWaitForMultipleObjects (1, pi.hProcess, FALSE, 1000, QS_ALLINPUT) = WAIT_OBJECT_0) then<br /> break;<br /> end;<br /> GetExitCodeProcess(pi.hProcess, dwECode);<br /> CloseHandle(pi.hProcess);<br /> end;<br /><br /> if (dwECode = 0) then<br /> begin<br /> Run('NET START MSSQL$' + instance);<br /> MessageDlg('Microsoft MSDE installed successfully. ' + #13#13 + 'You should now install the Proform Database.', mtConfirmation, [mbOk], 0)<br /> end<br /> else<br /> begin<br /> MessageDlg('Microsoft MSDE install failed.', mtError, [mbOk], 0);<br /> result := 0;<br /> end;<br />end;<br /><br />function TForm_Installer.Run(cmd <img src='/community/emoticons/emotion-7.gif' alt=':s' />tring) :boolean;<br />var<br /> StartInfo : TStartupInfo;<br /> ProcInfo : TProcessInformation;<br /> ErrNo :integer;<br /> Msg <img src='/community/emoticons/emotion-4.gif' alt=':p' />Char;<br /> E :Exception;<br />begin<br /> result := true;<br /> FillChar(StartInfo,SizeOf(TStartupInfo),#0);<br /> FillChar(ProcInfo,SizeOf(TProcessInformation),#0);<br /> StartInfo.cb := SizeOf(TStartupInfo);<br /> StartInfo.dwFlags := StartInfo.dwFlags or STARTF_USESHOWWINDOW;<br /> StartInfo.wShowWindow := 0;<br /><br /> try<br /> if (CreateProcess(nil, PChar('' + cmd), nil, nil,False,<br /> CREATE_NEW_CONSOLE, nil, PChar(ExtractFilePath(ParamStr(0))), StartInfo, ProcInfo)) then<br /> WaitForSingleObject(ProcInfo.hProcess, INFINITE)<br /> else<br /> begin<br /> result := false;<br /> ErrNo := GetLastError;<br /> Msg := AllocMem(4096);<br /> try<br /> FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, ErrNo, 0, Msg, 4096, nil);<br /> E := Exception.Create('Create Process Error #' + IntToStr(ErrNo) + ': ' + string(Msg));<br /> finally<br /> FreeMem(Msg);<br /> end;<br /> raise E;<br /> end;<br /> finally<br /> CloseHandle(ProcInfo.hThread);<br /> CloseHandle(ProcInfo.hProcess);<br /> end;<br />end;<br /></pre>

Share This Page