Can't return a string from COM (Delphi)?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
5 views
There is a task to write a COM server output it needs to be a string.


Here is the interface in the _TLB.pas file
// *********************************************************************//
// Interface: ITMyCOM
// Flags: (256) OleAutomation
// GUID: {D94769D0-F4AF-41E9-9111-4D8BC2F42D69}
// *********************************************************************//
ITMyCOM = interface(IUnknown)
['{D94769D0-F4AF-41E9-9111-4D8BC2F42D69}']
MyDrawWS function(a: Integer; b: Integer): WideString; stdcall;
end;



This is how it looks in Axis Windows
[
odl
uuid(D94769D0-F4AF-41E9-9111-4D8BC2F42D69),
version(1.0),
helpstring("Interface for TMyCOM Object")
oleautomation
]
ITMyCOM interface : IUnknown {
BSTR _stdcall MyDrawWS(
[in] long a,
[in] long b);
};


ie WideString and BSTR is how the idea should be.


The function in the COM server are as follows
function TTMyCOM.MyDrawWS(a, b: Integer): WideString;
begin
Result := WideString(IntToStr(a+b));
end;



Call it as follows
Edit1.Text := String(MyCOM.MyDrawWS(1,1));



and get the error First chance exception at $75A9FBAE. Exception class EAccessViolation with message 'Access violation at address in module 75A409A4 'RPCRT4.dll'. Read of address FFFFFFF8'. Process Project1.exe (2296)


The return numbers are working fine. Already tried different ways to translate from type to type, but nothing happens.
by | 5 views

3 Answers

0 like 0 dislike
The answer was found on stackoverflow.com/questions/4183773/how-to-return-widestring-from-com-server
\r[
odl
uuid(D94769D0-F4AF-41E9-9111-4D8BC2F42D69),
version(1.0),
helpstring("Interface for TMyCOM Object")
oleautomation
]
ITMyCOM interface : IUnknown {
HRESULT _stdcall MyDrawWS(
[in] long a,
[in] long b,
[out, retval] BSTR* ret);
};
\r
ITMyCOM = interface(IUnknown)
['{D94769D0-F4AF-41E9-9111-4D8BC2F42D69}']
MyDrawWS function(a: Integer; b: Integer; out ret: WideString): HResult; stdcall;
end;
\r
function TTMyCOM.MyDrawWS(a, b: Integer; out ret: WideString): HRESULT;
begin
ret := IntToStr(a+b);
Result := S_OK;
end;
\r
var
W: WideString;
begin
OleCheck(MyCOM.MyDrawWS(1, 1, W));
Edit1.Text := W;
end;
\r

\r
or change the call stdcall to safecall
\r
\rITMyCOM = interface(IUnknown)
['{D94769D0-F4AF-41E9-9111-4D8BC2F42D69}']
MyDrawWS function(a: Integer; b: Integer): WideString; safecall;
end;
\r
function TTMyCOM.MyDrawWS(a, b: Integer): WideString;
begin
Result := IntToStr(a+b);
end;
\r
Edit1.Text := MyCOM.MyDrawWS(1, 1);
by
0 like 0 dislike
And MyCOM and you initialize Edit1 forgot the hour?
by
0 like 0 dislike
Try to return the result of using StringToOleStr().
by

Related questions

0 like 0 dislike
2 answers
asked Apr 1, 2019 by Egorh
0 like 0 dislike
2 answers
asked Apr 2, 2019 by Egorh
0 like 0 dislike
2 answers
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
27,912 users