Home > front end >  Delphi - using C DLL with int* parameter
Delphi - using C DLL with int* parameter

Time:07-06

I'm having trouble using a DLL function with an int* parameter.

DLL supplier information:

image

The function is declared in the DLL as:

int sendQuoGetInfDstn(char* nomed, int *rigd)

I have imported this into Delphi 11 using:

const
   QUODLL = 'PcQuoDllNoWrap.dll';
    
implementation
    
function sendQuoGetInfDstn(Name: PAnsiChar; Count: PInteger): integer; stdcall; external QUODLL;

This compiles fine.

My question is, how do I call this function from my Delphi program? I have tried all sorts of things, but I get Access violation errors or program crash.

For example, I have made this wrapper:

function TPCQuo.GetWorklistInfoTest(Name: String; Count: integer): integer;
begin
  Result := sendQuoGetInfDstn(PAnsiChar(Ansistring(Name)), @Count); {I have also tried PInteger(Count)}
end;

And I call the wrapper like this:

procedure TForm1.Button4Click(Sender: TObject);
var
  name: String;
  count: integer;
begin
  if QUO.GetWorklistInfoTest(name, count) <> 0 then
     ShowMessage('No worklist available ')
  else
    ShowMessage('Worklist available '   name   ' number of lines: '   count.ToString );
end;

So, how should I call this function?

CodePudding user response:

There are a few possible issues here:

  1. The function may be cdecl rather than stdcall. It's not possible to say for sure but you'd need to look more closely at the supplied header file.
  2. The first argument char *nomed might be expecting a modifiable character array. Perhaps the function does attempt to modify the text, or perhaps the author made a mistake and should have written const char *nomed. With the information provided here we cannot tell.
  3. The encoding of the text could be ANSI, or it could be some other 8-bit encoding such as UTF-8. With the information provided here we cannot tell what it is.
  4. The second argument, int *rigd, could be either a pointer to a single integer, or it could be a pointer to an array. With the information provided here we cannot tell.

In summary, the only potential mistake in your translation is item 1. But getting the arguments, types and calling convention correct is only the first step. You also need to know the semantics of how to call a function. My best guess is that the issues are in that area, which will require information that you have, but we do not.

  • Related