Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi, OleVariant as out parameter

I have a third party ActiveX control that I import to my application. One of the functions is imported as:

function GenerateMACClearIVSync(const KeyName: WideString;  
         out MacBytes: OleVariant): Integer;

What it does is to calculate certain 8 byte value and store it into "MacBytes" variable. However, I am having problems with this function call, as it give me access violation.

This is the code I try:

var i: integer;
MacBytes: OleVariant;
begin
   MacBytes := VarArrayCreate([0, 7], varByte);
   i := GenerateMACClearIVSync('MMM22', MacBytes);
end;

I expect that MacBytes gets filled with 8 bytes of data, but instead I get Access Violation in Delphi.

On another end I see the ActiveX control creates the 8 byte data (by looking at the trace of the DLL).

Please, advise. Thanks.

Additional info: I use Delphi XE5. Here is the imported function prototype:

GenerateMACClearIVSync(BSTR KeyName, VARIANT* MacBytes,long* pVal) 

And this is how they call it:

VARIANT macResult; long length = GenerateMACClearIVSync(EncKey, &macResult)

When I import the ActiveX into Delphi I get the ..._TLB.pas file that contains this declaration:

IKXSDMCtrl = interface(IDispatch)
...
function GenerateMACClearIVSync(const KeyName: WideString; out MacBytes: OleVariant): Integer; safecall;
....
end;

And then, further below:

IKXSDMCtrlDisp = dispinterface(IDispatch)
...
function GenerateMACClearIVSync(const KeyName: WideString; out MacBytes: OleVariant): Integer; dispid 13;
....
end;

And further down:

TKXSDMCtrl = class(TOleControl)
...
function GenerateMACClearIVSync(const KeyName: WideString; out MacBytes: OleVariant): Integer;
...
end

Finally this:

function TKXSDMCtrl.GenerateMACSync(const KeyName: WideString; out MacBytes: OleVariant):   Integer;
begin
  Result := DefaultInterface.GenerateMACClearIVSync(KeyName,  MacBytes); 
end;
like image 661
Zoran Avatar asked Nov 24 '25 06:11

Zoran


1 Answers

Got it partially resolved: it crashes only if I run the app in debug mode. If I run the EXE by double clicking it then the application runs properly without any errors. I have no idea what causes the problem, but I will move on with the project as this was a big stopper. Maybe some Delphi guru can give more explanation.

like image 127
Zoran Avatar answered Nov 26 '25 06:11

Zoran