简体   繁体   English

将Delphi DLL函数导入C ++ Builder,传输PAnsiChar

[英]Import Delphi DLL function into C++ Builder , transfer PAnsiChar

I want to use Delphi code , export via DLL from C++ Builder 我想使用Delphi代码,从C ++ Builder通过DLL导出

Delphi Code fragment goes like this Delphi代码片段是这样的

//  function declare 
function NameData(ItemIndex: Integer;
  Buffer: PAnsiChar; var BufSize: DWORD): DWORD; stdcall;
  external 'database.dll' 


// function calling code 
  s1, S2: AnsiString;
begin


  for i := 1 to  ...  do
  begin
    BufSize := 0;
    NameData(i, nil, BufSize);
    SetLength(s1, BufSize);
    NameData(i, PAnsiChar(s1), BufSize);

    mmo_dll.lines.Add(' name ->  ' + string(s1));

relevant DLL code 相关的DLL代码

library DLLCode; 


function NameData(ItemIndex: Integer;
  Buffer: PAnsiChar; var BufSize: DWORD): DWORD; stdcall;
var
  returnString: Ansistring;
begin
  returnString := ' call some other functions .....';

  if BufSize < Length(returnString) then
    result := ERROR_BUFFER_TOO_SMALL
  else
  begin
    StrPCopy(Buffer, returnString);
    result := ERROR_NO_ERROR;
  end;
  BufSize := Length(returnString);
end;

this and a lot of more stuff works fine, Delphi and Delphi DLL. Delphi和Delphi DLL可以很好地解决这个问题。 Now here is my not working C++ code : 现在这是我无法正常工作的C ++代码:

//  function prototype 
typedef void (__stdcall*IntCharIntIn_VoidOut)(int, PAnsiChar, int);

// DLL prototype 
extern "C" __declspec(dllimport)
    IntCharIntIn_VoidOut  __stdcall NameData(int, PAnsiChar, int);

//  instance declaration 
IntCharIntIn_VoidOut   NameData;


//  load library data, no error raise,  other simpler function call already working

........
NameData = (IntCharIntIn_VoidOut)::GetProcAddress(load,
            "NameData");


///  calling code 
    int  Bufsize;
    PAnsiChar DataName;


    for (i = 0; i < count - 1; i++) {

        *Bufsize = 0;

        NameData(i, NULL, Bufsize);

        StrLen(SignalName);

        NameData(i, DataName, Bufsize );


        Memo1->Lines->Add(IntToStr(i));  // for test only 
    }

In the second call I get an access violation, but can't see why/where I'm wrong 在第二个电话中,我遇到访问冲突,但是看不到我为什么/在哪里错

You don't allocate any memory, and your function declaration is wrong. 您不分配任何内存,并且函数声明错误。

The function really should be declared like so: 函数实际上应该这样声明:

typedef void (__stdcall *IntCharIntIn_VoidOut)(int, char*, unsigned int*);

And your calling code should be: 您的调用代码应为:

unsigned int Bufsize;
char* DataName;

for (i = 0; i < count - 1; i++) {
    Bufsize = 0;
    NameData(i, NULL, &Bufsize);
    DataName = new char[Bufsize + 1];
    NameData(i, DataName, &Bufsize);
    // do something with DataName
    delete[] DataName;
}

I've omitted error checking on the memory allocation and deallocation. 我已经省略了对内存分配和释放的错误检查。 If it were me I would be using grown up C++ string objects and not raw memory. 如果是我,我会使用长大的C ++字符串对象,而不是原始内存。 The loop looks like it misses the final iteration, should be <= count - 1 or < count surely. 循环看起来好像错过了最后的迭代,应该为<= count - 1< count Your type name, IntCharIntIn_VoidOut fails to recognise that two of the arguments are pointers. 您的类型名称IntCharIntIn_VoidOut无法识别两个参数是指针。 I'm using char* rather than PAnsiChar , but I guess that the latter is just an alias to the former. 我使用的是char*而不是PAnsiChar ,但是我想后者只是前者的别名。

I'll leave all of the above for you to deal with. 我将上述所有内容留给您处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM