如何从 IIS ISAPI 的 GetExtensionVersion 获取我网站的 URL?

How can I grab the URL of my website from IIS ISAPI's GetExtensionVersion?

我有一个 Delphi ISAPI DLL(32 位),使用 Delphi XE SPI 构建,并托管在服务器 运行 WinServer 2008 RS2 和 IIS 7.5 上.

参见 MSDN:

Initialization Using GetExtensionVersion:

Initialization is handled by the entry-point function GetExtensionVersion. This function's role is to perform all initialization, including the creation of worker threads, synchronization objects, and database connections, and to establish the version of ISAPI that was used to build the DLL.

在我的 ISAPI DLL 中,我使用 GetExtensionVersion 按照上述 MSDN 参考执行初始化。 GetExtensionVersion 非常适合初始化需要在 Web 应用程序的生命周期内持续存在的资源,而不是根据每个客户端请求进行初始化,因为当第一个请求映射到您的 IS 时,IIS 只调用一次API动态链接库。如果有兴趣,请参阅 How can I make ADO database connections in TISAPIApplication before processing incoming requests? 了解更多详情。

我的一个初始化函数采用托管该 DLL 的网站的 URL,但我似乎找不到任何 Delphi 函数或 属性 公开网站的 URL 在 GetExtensionVersion 的上下文中运行,它在应用程序开始处理实际客户端请求之前运行 - 这发生在 HttpExtensionProc 中(Delphi 的 TWebApplication 挂钩到使用 TWebActionItem)。

由于我的 ISAPI DLL 托管在网站中,并且 GetExtensionVersion 仅在客户端请求发布到该网站时由 IIS 调用,我相信该网站的 URL应该在某处可用 - 也许通过 IIS API 调用。

如何从 GetExtensionVersion 中获取我网站的 URL?

您需要扩展控制块才能获得 URL。它会传递给 HttpExtensionProc,但不会传递给 GetExtensionVersion。有可能 IIS 在调用 GetExtensionVersion 时甚至没有将 DLL 映射到任何网站。此外,IIS 可能正在为多个网站使用相同的 DLL,并且仍然调用 GetExtensionVersion 一次。

针对你的问题,我看到了两个选项:你可以访问 IIS Admin API (perhaps with this) 来找出你的 DLL 用于哪个网站,但这可能会是您唯一需要 IIS 管理员 API 的东西,并且需要太多您不想 运行 您的应用程序的访问权限,安全方面。

另一种选择(我自己在xxm中使用的一个)是在DLL中可用的HInstance全局调用GetModuleFileName,并读取目录中的配置文件DLL 在。(默默地假设 DLL 本身的目录不能通过 IIS 使用 URL 访问...)