Compile Error when trying to return PChar or OleVariant for UDF

Chris B.

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;
Remy Lebeau

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compile type error when trying to return Extendable record

Return error while trying to compile fftw

Why do I get a compile-time error when trying to return values from a mock?

Compile error when trying to use increment operator

Namespace not found error when trying to compile

Compile error when trying to run make

Compile error when trying to install qjournalctl

Error when trying to compile SASS code

CoreData : error return nil when trying to fetch

Error when trying to return a value with generics

error when trying to return InstanceType of extended class

error when trying to return an integer as the entity

object object error when trying to return value

Error when trying to return Redirect in Laravel

Make Error 127 when running trying to compile code

ERROR when trying to compile protoc files: file not found or had errors

Golang: compile error when trying to delete an item in map

Compile error when trying to print an integer in Rust v0.13.0

Got compile error when trying to invoke function pointer to member functions

Compile error when trying to use std::result_of

GFortran error: ld: library not found for -lSystem when trying to compile

Maven crashes when trying to compile a project "Error executing Maven."

No exported member error with firebase when trying to compile angular 10 project

Boost thread c2064 error when trying to compile

BEX error when trying to compile/run a program using Intellij IDEA

Compilation results in an error when trying to compile code containing binary ifstream

Why do I get an Undefined reference error when trying to compile?

Webpack error 'Error: Final loader didn't return a Buffer or String' when trying to compile React server side components using ReactJS.NET and MVC 5

Java Compile error when return Generic Class type