Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Convert olevar to string


This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Mar 12, 2018 7:00 AM Last Post By: Yury Med Threads: [ Previous | Next ]
Yury Med

Posts: 3
Registered: 3/6/08
Convert olevar to string  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 2, 2018 2:58 AM
Hello!

I write app that retrieve some info from MS Active Directory. Ususally attribute "pwdlastset" contains 0 or Date when user changed password.
Function Get returns OleVariant. I want to convert it to string.
But I could not avoid error:
Could not convert variant of type (dispatch) into type (olestr).
Thank you for any help!

var
ole:olevariant;
cntr,i,j:integer;
......
 
if ADsGetObject(PWideChar(Widestring('LDAP://'+ip+'/'+Item.Subitems[4])),IAds, user)=S_OK then
    begin
             try
                      ole:=user.get('pwdlastset');
                         i := VarArrayLowBound(ole, 1);
                        j := VarArrayHighBound(ole, 1);
      
    for cntr := i to j do
                           begin
                              showmessage(ole[cntr]); // ERROR THERE
                          end;
       except
               on E: Exception do
        begin
          form1.sStatusBar1.Panels[0].Text:=E.Message;
        end;
      end;
 
    end
    else
    showmessage('User bind error: '+Item.Subitems[4])
  end;


Edited by: Yury Med on Mar 2, 2018 2:58 AM

Edited by: Yury Med on Mar 2, 2018 3:01 AM

Edited by: Yury Med on Mar 2, 2018 3:54 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Convert olevar to string [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 2, 2018 10:49 AM   in response to: Yury Med in response to: Yury Med
Yury Med wrote:

I write app that retrieve some info from MS Active Directory.
Ususally attribute "pwdlastset" contains 0 or Date when user changed
password. Function Get returns OleVariant. I want to convert it to
string. But I could not avoid error: *Could not convert variant of
type (dispatch) into type (olestr).*

'pwdlastset' is not an array, so why are you treating it as one?

--
Remy Lebeau (TeamB)
Yury Med

Posts: 3
Registered: 3/6/08
Re: Convert olevar to string [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 5, 2018 10:56 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Yury Med wrote:

I write app that retrieve some info from MS Active Directory.
Ususally attribute "pwdlastset" contains 0 or Date when user changed
password. Function Get returns OleVariant. I want to convert it to
string. But I could not avoid error: *Could not convert variant of
type (dispatch) into type (olestr).*

'pwdlastset' is not an array, so why are you treating it as one?

--
Remy Lebeau (TeamB)

Thank you for reply.

I have tried :
ole:=user.get('pwdlastset');
showmessage(string(ole));


also

ole:=user.get('pwdlastset');
VarAsType(ole, varString)


The same error...
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Convert olevar to string [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 6, 2018 11:04 AM   in response to: Yury Med in response to: Yury Med
Yury Med wrote:

I have tried :

ole:=user.get('pwdlastset');
showmessage(string(ole));


also

ole:=user.get('pwdlastset');
VarAsType(ole, varString)


The same error...

According to the ADS documentation, the 'pwdlastset' field
(https://msdn.microsoft.com/en-us/library/ms679430.aspx) is an 8-byte
"Interval" type
(https://msdn.microsoft.com/en-us/library/ms684426.aspx).

"Interval" is an ADSTYPE_LARGE_INTEGER (a 64-bit integer) wrapped
inside an IDispatch interface. Which is exactly what the error message
is complaining about ("Could not convert variant of type (dispatch)
into ...").

The "Interval" documentation says the IDispatch can be casted to
IADsLargeInteger
(https://msdn.microsoft.com/en-us/library/aa706037.aspx), which in turn
has LowPart and HighPart properties.

So, to convert the 'pwdlastset' field value to a human-readable string,
you will have to dothe following:

- cast the Variant to IDispatch

- cast the IDispatch to IADsLargeInteger

- extract its LowPart and HighPart values

- assign those values to a Win32 FILETIME record

- convert that FILETIME to a SYSTEMTIME record using the Win32
FileTimeToSystemTime() function

- convert that SYSTEMTIME to a TDateTime using the RTL's
SystemTimeToDateTime() function

- convert that TDateTime to a string using the RTL's DateTimeToString()
or FormatDateTime() function as needed.

--
Remy Lebeau (TeamB)
Yury Med

Posts: 3
Registered: 3/6/08
Re: Convert olevar to string [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 12, 2018 6:58 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy, thank you for brilliant analysis!

According to your plan:
var
 
....
 
ole:olevariant;
disp: IDISPATCH;
li: IADsLargeInteger;
logonDate: TDateTime;
lpSysTime: TSystemTime;
ft: TFileTime;
localft: TFileTime; 
 
 
Begin
 
    if ADsGetObject(PWideChar(Widestring('LDAP://'+ip+'/'+Item.Subitems[4])),IAds, user)=S_OK then
    begin
      
 
     ole:= user.Get('pwdlastset');
     disp:=idispatch(ole);
     li := disp as IADsLargeInteger;
     ft.dwLowDateTime := li.LowPart;
     ft.dwHighDateTime := li.HighPart;
 
    if Int64(ft) = 0 then begin
                                     showmessage('User must change password during first logon!')
                                end
                      else
                      begin
                           if FileTimeToLocalFileTime(ft, localft) then // time zone shift
                           if FileTimeToSystemTime(localft, lpSysTime) then
                             begin
                                LogonDate := SystemTimeToDateTime(lpSysTime);
                                ShowMessage(DateTimeToStr(logonDate));
                             end
                     end;
End;


Thank you very much!

Edited by: Yury Med on Mar 12, 2018 6:59 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02