How can I make ADO database connections in 'TISAPIApplication` before processing incoming requests?

前端 未结 2 819
太阳男子
太阳男子 2021-01-22 02:11

TADOConnection is failing to connect in the application initialization section of Delphi ISAPI App (TISAPIApplication):

Application is built wi

2条回答
  •  后悔当初
    2021-01-22 03:03

    The begin ... end or an initialization part of a dll is the Delphi equivalent to dllmain in C++. So the same restrictions apply including:

    • Don't call CoInitialize
    • Don't call COM functions

    This implies that you cannot create an ADO connection.

    Do you know all the things that happen when you call TADOConnection.Create(Application);?

    So what you're trying to do isn't going to work. And even if it did, you should not do it. Here's some better explanations:

    http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx
    http://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx
    http://blogs.msdn.com/b/oldnewthing/archive/2004/01/27/63401.aspx
    http://blogs.msdn.com/b/oldnewthing/archive/2004/01/28/63880.aspx
    http://blogs.msdn.com/b/oldnewthing/archive/2014/08/21/10551659.aspx

    MSDN suggests creating the database connection in GetExtensionVersion. This is how your isapi dll is initialized. It is not just for reporting the extension version. So that is the way to go. Create your own GetExtensionVersion function that initializes your database and then call the previous Delphi function.

    library Project1;
    
    uses
      Winapi.ActiveX,
      System.Win.ComObj,
      Web.WebBroker,
      Web.Win.ISAPIApp,
      Web.Win.ISAPIThreadPool,
      Winapi.Isapi2,
      Winapi.Windows,
      WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule};
    
    {$R *.res}
    
    function GetExtensionVersion(var Ver: THSE_VERSION_INFO): BOOL; stdcall;
    begin
      Result := Web.Win.ISAPIApp.GetExtensionVersion(Ver);
      // create your ado connection here
    end;
    
    
    exports
      GetExtensionVersion,
      HttpExtensionProc,
      TerminateExtension;
    
    begin
      CoInitFlags := COINIT_MULTITHREADED;
      Application.Initialize;
      Application.WebModuleClass := WebModuleClass;
      Application.Run;
    end.
    

提交回复
热议问题