Google
 

Friday, August 27, 2004

Unloading an ISAPI dll from IIS

I got this snippet from the borland.public.delphi.internet.isapi-webbroker newsgroup - a simple way to unload an ISAPI dll programmatically:
var
WSite, WServer, WRoot: Variant;
begin
WSite := CreateOleObject('IISNamespace');
WSite := WSite.GetObject('IIsWebService', 'localhost/w3svc');
WServer := WSite.GetObject('IIsWebServer', '1');
WRoot := WServer.GetObject('IIsWebVirtualDir', 'Root');
wserver.stop;
wroot.appunload;
wserver.start;
end;
This works on XP - I didn't even know you could create an OLE object with "IISNamespace". But you can. Anyhow, in the WSite.GetObject call you see a "1" - if you're wondering what that is, it's the number given to a web site by IIS. You can get the numbers of all your websites through Adsi - import the Ads type library and use the code from ADSI paper like this:
var serv : IADSContainer;
e : IEnumVARIANT;
varArr : OleVariant;
lNumElements : ULong;
obj : IADs;
hr : integer;
begin
// list web sites
AdsGetObject('IIS://localhost/w3svc',IAdsContainer, serv) ; hr := ADsBuildEnumerator(serv,e);
while(Succeeded(Hr)) do
begin
hr := ADsEnumerateNext(e,1,
varArr ,lNumElements);
if (lNumElements=0) then
break;
IDispatch(varArr).QueryInterface(IADs, obj);
if obj<>nil then
begin
if obj.Class_ = 'IIsWebServer' then
// obj.Name contains the "number" you need.
end;
obj := nil;
varArr := NULL;
end;
//don't call ADsFreeEnumerator(e); //since e will be released by Delphi
...
end;
You can use ADSI to do a ton of things.

1 comment:

Ondrej Kelle said...

Interesting, but doesn't stopping the server already unload all DLLs?