GetObject IADs

53 views
Skip to first unread message

Jean-Marc Autexier

unread,
Sep 10, 2014, 3:15:27 AM9/10/14
to comfyj...@teamdev.com
Hi, 

I'm trying to read AD/LDAP user information using the windows API (I don't want to authenticatite to LDAP). 

I've generated ADO classes an can retrieve user information with the following code: 

String defaultNamingContext = "DC=DE,DC=xxx,DC=xxx,DC=xxxx";
String username = "userName you want to query";

_Connection c = Connection.create(ClsCtx.SERVER);
c.setProvider(new BStr("ADsDSOObject"));
c.open();
msado.adodb._Command command = msado.adodb.Command.create(ClsCtx.SERVER);
command.setActiveConnectionByRef(c);

String requestedInfo = "givenName,sn,userPrincipalName,telephoneNumber,mail,HomeDirectory,streetAddress,department,company,title,manager,l,c,telephoneNumber,facsimileTelephoneNumber,location";
String query = "<LDAP://" + defaultNamingContext + ">;(sAMAccountName=" + username + ");" + requestedInfo + ";subTree";
command.setCommandText(new BStr(query));
_Recordset recordSet = command.execute(new Variant(), Variant.createUnspecifiedParameter(), new Int32(-1));

Fields userData = recordSet.getFields();
StringTokenizer tokenizer = new StringTokenizer(requestedInfo, ",");
while (tokenizer.hasMoreTokens()) {
   String currentFieldStr = tokenizer.nextToken();
   Field currentField = userData.getItem(new Variant(currentFieldStr));
   System.out.println(currentFieldStr +" : "+currentField.getValue().getBstrVal());
}

What I don't know is how to get the defaultNamingContext and the current user which is logged in. 
For the current user, I could use environment properties, but I would like to use the windows API as well. 


From vairous C#/C++/.. samples I know I have to use IADs object and "get" the defaultnaming context. Something like (VB Code): 
Dim rootDSE As IADs
Dim defaultNamingContext As String
 
Set rootDSE = GetObject("LDAP://rootDSE")
defaultNamingContext = rootDSE.Get("defaultNamingContext")

But how can I get an IADs object instance? 

Can you help?
Jean-Marc


Jean-Marc Autexier

unread,
Sep 10, 2014, 3:28:36 AM9/10/14
to comfyj...@teamdev.com
Update
I found on way to do it, but it's probably not the most elegant: from I can get the computer name, an dfrom it I cn extract the "defaultNamingContext" by parsing the String. 
IADsADSystemInfo info = ADSystemInfo.create(ClsCtx.SERVER) ;
String computername = info.getComputerName().toString() ;
int start = computername.indexOf("DC=") ;
defaultNamingContext = computername.substring(start, computername.length()) ;

This returns something like"DC=DE,DC=xxxx,DC=xxxxx,DC=xxxxx"

Jean-Marc Autexier

unread,
Sep 16, 2014, 4:07:40 AM9/16/14
to comfyj...@teamdev.com
Hi,

I'm still looking for a solution:

How can I instantiate with ComfyJ an objekt IADs with those parameters? Here is the C++ code:

IADs *pads;
hr = ADsGetObject(  L"LDAP://rootDSE",IID_IADs, (void**)&pads);

http://msdn.microsoft.com/en-us/library/ms676736%28v=vs.85%29.aspx

regards,
Jean-Marc


Am Mittwoch, 10. September 2014 09:15:27 UTC+2 schrieb Jean-Marc Autexier:

Anna Dolbina

unread,
Sep 16, 2014, 8:30:19 AM9/16/14
to Jean-Marc Autexier, ComfyJ Forum
Hi Jean-Marc,

As a straightforward solution, you can load the function and invoke it in your code with appropriate parameters.

The general idea is shown below:

        Library activeDS = new Library("Activeds");
        activeDS.load();

        Function adsGetObject = activeDS.getFunction("ADsGetObject");

        HResult result = new HResult();
        BStr lpszPathName = new BStr("LDAP://rootDSE");
        IADs pReqInterface = new IADsImpl();

        Pointer.Const refiid = new Pointer.Const(new IID(IADs.INTERFACE_IDENTIFIER));
        Pointer.OutOnly ppObject = new Pointer.OutOnly((Parameter)pReqInterface);
        adsGetObject.invoke(result, lpszPathName,refiid, ppObject);
You can use the ComException.checkResult() method to check the result value.

Sincerely,
Anna


--
You are receiving this email because you have joined the "ComfyJ Forum" group or have been added by request. You may choose not to receive emails from this group or unsubscribe completely on "Edit my membership" page.
Go to http://links.teamdev.com/comfyj-forum, choose "Edit my membership" link on the right. Specify your email preferences in suggested options, or click "Unsubscribe" button.
---
You received this message because you are subscribed to the Google Groups "ComfyJ Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to comfyj-forum...@teamdev.com.
To post to this group, send email to comfyj...@teamdev.com.
Visit this group at http://groups.google.com/a/teamdev.com/group/comfyj-forum/.
For more options, visit https://groups.google.com/a/teamdev.com/d/optout.

Message has been deleted

Jean-Marc Autexier

unread,
Sep 16, 2014, 9:16:44 AM9/16/14
to comfyj...@teamdev.com
Hi Anna, 

thank you for your help. 

Jean-Marc

Am Mittwoch, 10. September 2014 09:15:27 UTC+2 schrieb Jean-Marc Autexier:

Anna Dolbina

unread,
Sep 16, 2014, 9:38:44 AM9/16/14
to Jean-Marc Autexier, ComfyJ Forum
Hi Jean-Marc,

You can use pReqInterface object after the successful call, there is no need to get the referenced object from the pointer.

Sincerely,
Anna

Reply all
Reply to author
Forward
0 new messages