尝试为UDF返回PChar或OleVariant时编译错误(Compile Error when trying to return PChar or OleVariant for UDF)

编程入门 行业动态 更新时间:2024-10-07 14:26:58
尝试为UDF返回PChar或OleVariant时编译错误(Compile Error when trying to return PChar or OleVariant for UDF)

标题并没有完全体现问题的本质。

我有一个返回PChar的UDF函数。

function AccountDescription(sAccountId: PChar) : PChar; stdcall;

这工作正常但我意识到如果找不到accountId我想返回#N / A.

我发现了CVErr(xlErrNA)并更改了Signature以返回OleVariant。 但现在我收到[错误]不兼容的类型:'OleVariant'和'PAnsiChar'。

我找不到任何关于如何解决这个问题的信息,所以我认为我对问题的理解一定不正确。

我尝试传递一个字符串,该字符串已编译但产生了“无效变体类型”的运行时错误。

完整的代码是:

function AccountDescription(sAccountId: PChar): OleVariant; stdcall; var strResult: string; strPChar : PChar; begin try strResult:= repo.GetAccount(sAccountId).Description; strPChar := strAlloc(length(strResult)+1) ; StrPCopy(strPChar, strResult) ; Result := strPChar; except Result := CVErr(xlErrNA); end; end;

注意:excel是否负责销毁字符串或者是我的清理工作? 我应该创建副本还是应该返回指向现有字符串的指针。 输入后我觉得我应该返回一个指针。

更新:删除了示例中的一些不相关的代码。

现在使用:

function AccountDescription(sAccountId: PChar): OleVariant; stdcall; var strResult: string; begin try Result := PChar(repo.GetAccount(sAccountId).Description); except Result := CVErr(xlErrNA); end; end;

The title doesn't quite capture the essence of the issue.

I have a UDF function that returns a PChar.

function AccountDescription(sAccountId: PChar) : PChar; stdcall;

This was working fine but I realized I wanted to return #N/A if the accountId was not found.

I discovered CVErr(xlErrNA) and changed the Signature to return OleVariant. But now I am receiving [Error] Incompatible types: 'OleVariant' and 'PAnsiChar'.

I could not find any information on how to resolve this so I figure my understanding of the problem must not be correct.

I tried just passing a string which compiled but produced a runtime error of "Invalid variant type".

The full code is:

function AccountDescription(sAccountId: PChar): OleVariant; stdcall; var strResult: string; strPChar : PChar; begin try strResult:= repo.GetAccount(sAccountId).Description; strPChar := strAlloc(length(strResult)+1) ; StrPCopy(strPChar, strResult) ; Result := strPChar; except Result := CVErr(xlErrNA); end; end;

Note: Is excel responsible for destroying the string or is that my cleanup? Should I be creating a copy or should I just be returning a pointer to an existing string. After typing it I feel like I should be returning a pointer.

Update: Removed some irrelevant code in the example.

Now using:

function AccountDescription(sAccountId: PChar): OleVariant; stdcall; var strResult: string; begin try Result := PChar(repo.GetAccount(sAccountId).Description); except Result := CVErr(xlErrNA); end; end;

最满意答案

你不需要PChar OleVariant转换,你可以直接为OleVariant分配一个String (它将由RTL转换为BSTR ,接收器在完成后使用它将释放):

Result := repo.GetAccount(sAccountId).Description;

至于报告错误,您的Delphi代码中是否有可行的CVErr()函数? 在VB中, CVErr()返回一个类型为Error的Variant (Delphi中的varError ),其中包含错误代码( xlErrNA为2042)。 Delphi有一个VarAsError()函数用于同样的目的:

Result := VarAsError(2042);

You do not need the PChar cast, you can assign a String directly to an OleVariant (it will be converted by the RTL into a BSTR that the receiver will then free when done using it):

Result := repo.GetAccount(sAccountId).Description;

As for reporting an error, do you have a viable CVErr() function in your Delphi code? In VB, CVErr() returns a Variant of type Error (varError in Delphi) containing an error code (xlErrNA is 2042). Delphi has a VarAsError() function for that same purpose:

Result := VarAsError(2042);

更多推荐

本文发布于:2023-07-27 08:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287745.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   OleVariant   PChar   UDF   Error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!