问题
I have a piece of code written in Delphi 2005 that searches for a particular attribute on a user in LDAP. I get an access violation when this is run on either Windows 7 or Server 2008, but not on XP or 2003.
Function IsSSOUser(UserId: String): Boolean;
var
S : string;
ADOQuery : TADOQuery;
ADOConnectionSSO: TADOConnection;
begin
result := false;
Setdomainname;
ADOQuery := TADOQuery.Create(nil);
ADOConnectionSSO := TADOConnection.Create(nil);
try
ADOConnectionSSO.LoginPrompt := false;
ADOConnectionSSO.Mode := cmRead;
ADOConnectionSSO.Provider := 'ADsDSOObject';
ADOQuery.Connection := ADOConnectionSSO;
ADOQuery.ConnectionString := 'Provider=ADsDSOObject;Encrypt Password=False;Mode=Read;Bind Flags=0;ADSI Flag=-2147483648';
ADOQuery.SQL.Clear;
try
S := 'SELECT AdsPath, CN, SN, SSOguid FROM '''
+ LDAPString + ''' WHERE objectClass=''user'' and CN = ''' + UserId + ''' ';
ADOQuery.SQL.Add(S);
ADOQuery.Open;
ADOQuery.ExecSQL;
if trim(ADOQuery.FieldByName('SSOguid').AsString) = '' then
result := false
else
result := true;
except
on e:Exception do
if e.ClassType <> EOleException then
Showmessage(format('[%s] Exception in IsSSOUser: [%s]',[e.ClassType.ClassName, e.Message]));
end;
finally
ADOQuery.Close;
ADOConnectionSSO.Close;
ADOQuery.free;
ADOConnectionSSO.free;
end;
end;
This code works fine on Windows XP and Windows Server 2003, but I get an access violation on both Windows 7 and Server 2008. I see a number of threads online about how changes to ADODB interface can break things on downstream OSes, but I am seemingly having the opposite problem. I'm building on a Windows 7 machine and the code only works on previous versions of Windows.
回答1:
You will have to add
AdoQuery.ParamCheck := false;
before your
ADOQuery.SQL.Add(S);
since your LDAPString may contains a colon (:
) eg. "LDAP://...."
which causes the query to try create a parameter object for it. In addition there is no need for ADOQuery.ExecSQL
after ADOQuery.Open
.
来源:https://stackoverflow.com/questions/15103180/adodb-component-causes-access-violation-on-win7-server-2008