I have been attempting to use an Agile Reference (see https://devblogs.microsoft.com/oldnewthing/20151020-00/?p=91321) created in a Delphi (XE7) COM In-process server (MTA) to pass to an Out of process COM server (STA) that the in-process server started. This should enable the out-of-process server to make calls back in to the in-process server successfully marshalling the interface pointers between the two processes.
This is the definition of the Agile Reference code. I found this code in an open source library. (I cant find it!). Perhaps a newer than XE7 version of ole32.pas might include it?
unit libagile;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, {ActiveX,} StdVcl;
type
AgileReferenceOptions = (
AGILEREFERENCE_DEFAULT = 0,
AGILEREFERENCE_DELAYEDMARSHAL = 1);
{$HPPEMIT 'DECLARE_DINTERFACE_TYPE(IAgileReference);'}
{$EXTERNALSYM IAgileReference}
IAgileReference = interface(IUnknown)
['{C03F6A43-65A4-9818-987E-E0B810D2A6F2}']
function Resolve(const riid: TGUID;
out ppvObjectReference: Pointer): HResult; stdcall;
end;
IID_IAgileReference = IAgileReference;
{$EXTERNALSYM IID_IAgileReference}
function RoGetAgileReference(options: AgileReferenceOptions;
const riid: TGuid;
pUnk: IUnknown;
out ppAgileReference: IAgileReference): HResult; stdcall;
external 'Ole32.dll';
{$EXTERNALSYM RoGetAgileReference}
implementation
end.
This seems to work when called in the in-process server. (The si.o( are just logging calls.) but the call to the out of process server fails when it tries to call Resolve on the passed interface.
procedure SetupCallback(obj:IInterface);
var
hres : hresult;
ref : IAgileReference;
pref: pointer;
begin
hres := RoGetAgileReference( AGILEREFERENCE_DEFAULT,
IWTSPlugin,
obj,
ref
);
if (hres = s_ok) then
begin
if (ref.Resolve(IID_IWTSPLUGIN,pRef)= S_OK) then
begin
fLocalProxy.SetAgileRef(ref);
end
else
si.o('Failed Resolve hres = '+Inttostr(hres));
end
else
si.o('Failed RoGetAgileReference hres = '+Inttostr(hres));
The SetAgileRef is a method in the Out Of Process server. However calling Resolve on the ref value passed in call fails usually with a Stack Overflow.