Thursday, July 08, 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 my 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. I've been planning to finish up a set of ADSI components for Delphi - never had the time though. I don't even know if there'll be enough people who need it - but I do get something like 10-20 mails a month on my paper. If the market's big enough, I'd like to make it commercial - which gives me time to work on it. Otherwise it goes as "free" stuff, but no time allocated to finish it. Tough call.

0 Comments:

Post a Comment

<< Home