2

_TLB.pas 文件中的这个接口

// *********************************************************************//
// 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;

这着眼于操作系统 Windows

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

COM 服务器中的函数如下所示

function TTMyCOM.MyDrawWS(a, b: Integer): WideString;
begin
Result := WideString(IntToStr(a+b));
end;

在 COM 客户端中,我正在调用此函数,例如

Edit1.Text := String(MyCOM.MyDrawWS(1,1));

并在 $75A9FBAE获得错误第一次机会异常。异常类 EAccessViolation 带有消息“模块 'RPCRT4.dll' 中地址 75A409A4 的访问冲突”。读取地址 FFFFFFF8'。处理 Project1.exe (2296)

如果我想返回整数,它可以工作。如何返回宽字符串?

4

5 回答 5

1

不要使用 HRESULT 以外的返回值。而是将您的返回值作为输出参数放入参数列表中。

function MyDrawWS(a: Integer; b: Integer; out str : WideString): HRESULT; stdcall;

这样,您也被迫使用 COM 内存管理器 IMalloc(CoTaskMemAlloc 用于 pur COM,SysAllocString 用于自动化)。

于 2010-11-15T12:53:44.110 回答
1

Let Delphi perform the conversions automatically. Don't cast. You can cast a (ansi)string to a PChar, because their memory layout are compatible, but you can't cast a string to a widestring or viceversa. Delphi will perfrom conversion when you assign one to the other.

In Delphi < 2009

var
S: string;
W: WideString;
...
S := W;  // Conversion, WideString -> AnsiString;
W := S; // Conversion, AnsiString -> WideString
于 2010-11-15T13:04:39.347 回答
1

The correct way to handle this is as follows:

[ 
odl, 
uuid(D94769D0-F4AF-41E9-9111-4D8BC2F42D69), 
version(1.0), 
helpstring("Interface for 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 then be simplified a little by using Delphi's safecall calling convention in the Delphi declaration (not in the TypeLibrary itself) of the 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);
于 2010-11-18T03:09:10.703 回答
0

您需要使用SysAllocString()SysAllocStringLen()分配 BSTR。

于 2010-11-15T11:21:37.023 回答
0

First chance exception at $75A9FBAE. Exception class EAccessViolation with message 'Access violation at address 75A409A4 in module 'RPCRT4.dll'

  1. the error is coming from RPCRT4.dll

  2. EAccessViolation is mostly caused by accessing a null object, step through your code make sure all objects are valid objects.

于 2010-11-16T05:17:30.793 回答