craznar
asked on
ADSI, IIS, AdsGetObject and Delphi
I need to programmatically add and/or edit IIS 4/5 virtual servers and directories. In VB this is straightforward with the GetObject routine.
I need to work out how to (in Delphi code - not by exec or stuff) do this.
So far I have found the relevant routine
function ADsGetObject(lpszPathName : PWideChar; riid : TGuid; out ppObject) : HRESULT; stdcall;
I need to know how to get to use this to return an object which I can actually use as the VB Getobject routine does.
Double points for a complete working segment of code that creates a complete virtual site.
I need to work out how to (in Delphi code - not by exec or stuff) do this.
So far I have found the relevant routine
function ADsGetObject(lpszPathName : PWideChar; riid : TGuid; out ppObject) : HRESULT; stdcall;
I need to know how to get to use this to return an object which I can actually use as the VB Getobject routine does.
Double points for a complete working segment of code that creates a complete virtual site.
just in case you havent done it before
to import a type library go to delphi's menu and do project - "import type library",this will create a file called ActiveDs_TLB.pas ..
to import a type library go to delphi's menu and do project - "import type library",this will create a file called ActiveDs_TLB.pas ..
ASKER
Usr := GetObject( 'WinNT://' + ComboBox1.text + '/' + Edit1.Text + ',user' )as IADsUser;
I have replaced ComboBox1.text with 'localhost' and with my machine name. Either way the Getobject line just freezes the process.
Also with Lst.next(1,o,@l)
I have had to redefine l as a longword and change @l to l to get it to compile. The 3rd param is defined as out.
I have replaced ComboBox1.text with 'localhost' and with my machine name. Either way the Getobject line just freezes the process.
Also with Lst.next(1,o,@l)
I have had to redefine l as a longword and change @l to l to get it to compile. The 3rd param is defined as out.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Getting close - it no longer fails.
But I still cannot do anything with the object e.g. the VB code
Max = 0
For Each webServer In w3svc
If webServer.Class = "IIsWebServer" Then
Max = webServer.Name
End If
Next
Relies on somehow using w3svc (which = webservice in your code) as a variant array of some form.
Summary - I have an object, but cannot use it to do anything.
But I still cannot do anything with the object e.g. the VB code
Max = 0
For Each webServer In w3svc
If webServer.Class = "IIsWebServer" Then
Max = webServer.Name
End If
Next
Relies on somehow using w3svc (which = webservice in your code) as a variant array of some form.
Summary - I have an object, but cannot use it to do anything.
ASKER
Could <b>inthe</b> please answer this question again to get your points.
Spent several hours and managed to work around the rest my self.
Still cannot enumerate through the list but can do everything I need to do.
Thanks....
Spent several hours and managed to work around the rest my self.
Still cannot enumerate through the list but can do everything I need to do.
Thanks....
ASKER
These are not perfect, but do a fair bit of useful stuff
----------
interface
function ADsGetObject(lpszPathName:
function hhGetObject(Id:PWideChar):
function hhGetTopServerId(const w3svc:variant):integer;
function hhMakeWebServer(const w3svc:variant;
const IPAddress:string='';
const IPPort:integer=80;
const Description:string='New Web Site'):variant;
function hhMakeVirtualDirectory(con
const Path:string; //**
const HomeDirectory:string; //**
const DefaultDocument:string='de
const EnableDefaultDocument:bool
const ReadPermission:boolean=FAL
const WritePermission:boolean=FA
const DirectoryBRowsing:boolean=
const ExecutePermission:boolean=
implementation
function ADsGetObject; external 'activeds.dll';
function hhGetObject(Id:PWideChar):
begin
if ADsGetObject(Id, IDispatch, TVarData(result).VDispatch
TVarData(result).vType := varDispatch
else
Result:= Unassigned;
end;
function hhGetTopServerId(const w3svc:variant):integer;
var
I:integer;
ws:variant;
begin
if not varisempty(w3svc) then
begin
Result:=-1;
i:=0;
repeat
try
inc(i);
ws:=w3svc.GetObject('IIsWe
except on e:exception do
Result:=i-1;
end;
until Result<>-1;
end
else
Result:=-1;
end;
function hhMakeWebServer(const w3svc:variant;
const IPAddress:string='';
const IPPort:integer=80;
const Description:string='New Web Site'):variant;
var
NewId:integer;
begin
if not VarIsEmpty(w3svc) then
begin
NewId:=hhGetTopServerId(w3
result:=w3svc.Create('IIsW
result.ServerComment:=Desc
result.ServerBindings:=Var
result.Keytype:='IIsWebSer
result.setinfo;
end
else
result := Unassigned;
end;
function hhMakeVirtualDirectory(con
const Path:string; //**
const HomeDirectory:string; //**
const DefaultDocument:string='de
const EnableDefaultDocument:bool
const ReadPermission:boolean=FAL
const WritePermission:boolean=FA
const DirectoryBRowsing:boolean=
const ExecutePermission:boolean=
begin
if not VarIsEmpty(WebServer) then
begin
Result:=WebServer.Create('
Result.Path := HomeDirectory;
Result.DefaultDoc := DefaultDocument;
Result.EnableDefaultDoc := EnableDefaultDocument;
Result.EnableDirBrowsing := DirectoryBrowsing;
Result.AccessExecute := ExecutePermission;
Result.AccessRead := ReadPermission;
Result.AccessWrite := WritePermission;
Result.AccessSource := FALSE;
Result.SetInfo;
end
else
Result:=UnAssigned;
end;
------------
ASKER
-- EXAMPLE CALLS --
var
w3svc,WebServer,RootDir,Sc riptDir:va riant;
begin
w3svc:=hhGetObject('IIS:// LocalHost/ W3SVC');
if not VarIsEmpty(w3svc) then
begin
WebServer:=hhMakeWebServer (w3svc,'19 2.168.10.1 ',80,'Web Web Site');
RootDir:=hhMakeVirtualDire ctory(WebS erver,'ROO T','C:\INE TPUB\WWW', 'index.htm l',TRUE,TR UE,FALSE,F ALSE,FALSE );
ScriptDir:=hhMakeVirtualDi rectory(Ro otDir,'Scr ipts','C:\ INETPUB\IM AGES','',F ALSE,FALSE ,FALSE,FAL SE,TRUE);
WebServer.Start;
end
else
ShowMEssage('Failed');
end;
var
w3svc,WebServer,RootDir,Sc
begin
w3svc:=hhGetObject('IIS://
if not VarIsEmpty(w3svc) then
begin
WebServer:=hhMakeWebServer
RootDir:=hhMakeVirtualDire
ScriptDir:=hhMakeVirtualDi
WebServer.Start;
end
else
ShowMEssage('Failed');
end;
ASKER
-- USEFUL URL --
MSDN Documentation on IIS ADSI ..
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/psdk/iisref/adsi57hv.htm
MSDN Documentation on IIS ADSI ..
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/psdk/iisref/adsi57hv.htm
WOW THANKS :-)
sorry I couldnt helped much more
thank you for the good advice
Regards Barry
sorry I couldnt helped much more
thank you for the good advice
Regards Barry
this is some stuff i found,
you need to import the type library activeds.tlb then you can do something like:
uses activex,comobj,ActiveDs_TL
function GetObject (const Name : string): IDispatch;
var
Moniker : IMoniker;
Eaten : integer;
BindContext : IBindCtx;
Dispatch : IDispatch;
begin
OleCheck( CreateBindCtx( 0, BindContext ) );
OleCheck( MkParseDisplayName( BindContext, PWideChar( WideString( Name ) ), Eaten, Moniker ) );
OleCheck( Moniker.BindToObject( BindContext, NIL, IDispatch, Dispatch ) );
Result := Dispatch;
end;
procedure TForm1.Button1Click(Sender
var
Usr: IADsUser;
l:longint;
Grp: IAdsGroup;
lst: IEnumVariant;
o:olevariant;
begin
Usr := GetObject( 'WinNT://' + ComboBox1.text + '/' + Edit1.Text + ',user' )as IADsUser;
memo1.lines.add( 'User: ' + Usr.FullName);
Lst := usr.Groups._newenum as IEnumVariant;
while Lst.next(1,o,@l) 0 do begin
grp:=IUnknown(o) as IAdsGroup;
if l = 1 then begin
memo1.lines.add( 'Member of ' + o.name + ' groups');
// just for test in variant format
memo1.lines.add('Member of ' + grp.name + ' groups');
end
else
memo1.lines.add( 'Err');
end;
end;
Regards Barry