delphi - How to return WideString from COM server? -
this interface @ _tlb.pas file
// *********************************************************************// // interface: itmycom // flags: (256) oleautomation // guid: {d94769d0-f4af-41e9-9111-4d8bc2f42d69} // *********************************************************************// itmycom = interface(iunknown) ['{d94769d0-f4af-41e9-9111-4d8bc2f42d69}'] function mydrawws(a: integer; b: integer): widestring; stdcall; end;
this looks @ os windows
[ odl, uuid(d94769d0-f4af-41e9-9111-4d8bc2f42d69), version(1.0), helpstring("interface tmycom object"), oleautomation ] interface itmycom : iunknown { bstr _stdcall mydrawws( [in] long a, [in] long b); };
function in com server looks as
function ttmycom.mydrawws(a, b: integer): widestring; begin result := widestring(inttostr(a+b)); end;
in com client i`m calling function
edit1.text := string(mycom.mydrawws(1,1));
and error first chance exception @ $75a9fbae. exception class eaccessviolation message 'access violation @ address 75a409a4 in module 'rpcrt4.dll'. read of address fffffff8'. process project1.exe (2296)
if want returning integer, it`s works. how return widestring?
the correct way handle follows:
[ odl, uuid(d94769d0-f4af-41e9-9111-4d8bc2f42d69), version(1.0), helpstring("interface tmycom object"), oleautomation ] interface itmycom : iunknown { hresult _stdcall mydrawws( [in] long a, [in] long b, [out, retval] bstr* ret); }; itmycom = interface(iunknown) ['{d94769d0-f4af-41e9-9111-4d8bc2f42d69}'] function mydrawws(a: integer; b: integer; out ret: widestring): hresult; stdcall; end; function ttmycom.mydrawws(a, b: integer; out ret: widestring): hresult; begin ret := inttostr(a+b); result := s_ok; end; var w: widestring; begin olecheck(mycom.mydrawws(1, 1, w)); edit1.text := w; end;
which can simplified little using delphi's safecall
calling convention in delphi declaration (not in typelibrary itself) of interface:
itmycom = interface(iunknown) ['{d94769d0-f4af-41e9-9111-4d8bc2f42d69}'] function mydrawws(a: integer; b: integer): widestring; safecall; end; function ttmycom.mydrawws(a, b: integer): widestring; begin result := inttostr(a+b); end; edit1.text := mycom.mydrawws(1, 1);
Comments
Post a Comment