How to make call to DLL function from Delphi? -
// list of accounts in domain separated \x00 , ended \x00\x00 function getuserlist(aname: pansichar; var list; size: longint): longint; stdcall;
i need call above xe6.
would kind enough post example of how can buffer, , put stream or string.
the variable "list" supposed fill buffer, can read off list of users.
after trying couple of options, have tried options such as:
thanks!
var buffer: array of byte; icount : integer; sname : ansistring; begin ... setlength(buffer, 4096); icount := getuserlisttest(pansichar(sname)@buffer[0], length(buffer)); // cannot // icount := getuserlist(pansichar(sname), buffer, length(buffer));
that not win32 api function, must third-party function. ask vendor example.
a var
parameter expects pass variable it. var
receives address of variable. @buffer[0]
not satisfy requirement, @
returns pointer, , var
ends address of pointer itself, not address of variable being pointed at. function expecting pointer buffer. using var
receive pointer, need drop @
, pass first array element, address of element (effectively address of buffer) passed function, eg:
icount := getuserlist(pansichar(sname), buffer[0], icount);
alternatively, can use syntax instead, pass same address of first element:
icount := getuserlist(pansichar(sname), pbyte(buffer)^, icount);
now, said, chances function may allow query necessary array size can allocate needed (but check documentation sure, i'm making assumption here since have not said otherwise)), eg:
procedure getdomainusers(const domain: ansistring; users: tstrings); var buffer: array of ansichar; icount : integer; user: pansichar; begin // call assumes function returns needed // bytecount when given null/empty array - check // documentation!!! icount := getuserlist(pansichar(domain), pansichar(nil)^, 0); if icount > 0 begin setlength(buffer, icount); icount := getuserlist(pansichar(domain), buffer[0]{or: pansichar(buffer)^}, icount); end; if icount > 0 begin users.beginupdate; try user := pansichar(buffer); while user^ <> #0 begin users.add(user); inc(user, strlen(user)+1); end; users.endupdate; end; end; end;
if not work, have pre-allocate large array:
procedure getdomainusers(const domain: ansistring; users: tstrings); var buffer: array of ansichar; user: pansichar; begin setlength(buffer, 1024); if getuserlist(pansichar(domain), buffer[0]{or: pansichar(buffer)^}, length(buffer)) > 0 begin users.beginupdate; try user := pansichar(buffer); while user^ <> #0 begin users.add(user); inc(user, strlen(user)+1); end; users.endupdate; end; end; end;
Comments
Post a Comment