在Delphi7中包含C/C++函数

问题描述 投票:0回答:0

我想在我的 Delphi7 项目中包含一个 JSON 解析器,因为在这个版本的 Delphi 中,默认情况下不包含 JSON 处理。

我的论文需要这样做,这就是为什么我不注入用 Delphi 编写的解析器的原因。

我要注入的 JSON 解析器的链接:michaelg29 - yt-challenges/28-CJSON/

我尝试了

cdecl
stdcall
调用约定,使用不同的字符串组合方式,我什至用 C++ 重写了整个 C 项目,但我仍然遇到
EAccessViolation
错误...

Delphi 中的方法签名 - 仅包含第一种方法以证明整个调用 C 方法概念正在运行:

console_h.pas
中的Delphi签名:

function writing_output(): PAnsiChar; stdcall; external 'converter32.dll'

像这样在 Delphi 中调用 C 函数

console.dpr
:

program console;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  console_h in 'console_h.pas';

var
  output: pansichar;
begin
  writeln('Hello');
  try
    output := writing_output();
  except
    readln;
  end;
  writeln(output);
  readln;
end.

我只会在 C: 中调用的方法签名和实现

#include <stdlib.h>
#include "src/json.h"

extern __declspec(dllexport) char* __stdcall writing_output(){
    json json_list = json_type(JSON_LIST);
    json json_object_arr[1];

    json_object_arr[0] = json_type(JSON_OBJ); //set them as JSON-objects
    json_obj_put_int(&json_object_arr[0], "azonos", 16388); //add as JSON object {"azonos":16388}

    json_list_add(&json_list, &json_object_arr[0]); //add to JSON-list [{...},{...},{"azonos":16388}]

    int n;
    char* output = json_dump(json_list, false, &n);
    json_freeDeep(&json_object_arr[0]);
    free(json_object_arr);
    json_freeDeep(&json_list);
    return output;
}

另一件事是我已经将来自 Github 的全部源代码(8 个 .h 和 .c 文件)包含到我的 DLL 中并将它们放入

src/
文件夹中,这是 JSONParser 以这种方式使用的问题吗?我是否还必须将
src/
文件夹与我编译的 DLL 一起复制,或者它们已经链接在一起?

Delphi调用导出方法时出错

writing_output

Project console.exe raised exception class EAccessViolation with message 
'Access violation at address 00A02F41 in module 'converter32.dll'.Read of address 00000000'. 
Process stopped. Use Step or Run to continue.

结果将是一个简单的 JSON 格式的字符串,其中包含如下对象:

[{"azonos":49156}, {"azonos":16388}, {"azonos":32772}, ...]
Folder structure in converter32.dll (the C-project):
📦converterDLL
 ┣ 📂src
 ┃ ┣ 📜dynamicarray.c
 ┃ ┣ 📜dynamicarray.h
 ┃ ┣ 📜hashmap.c
 ┃ ┣ 📜hashmap.h
 ┃ ┣ 📜json.c
 ┃ ┣ 📜json.h
 ┃ ┣ 📜strstream.c
 ┃ ┣ 📜strstream.h
 ┣ 📂Win32
 ┃ ┗ 📂Debug
 ┃ ┃ ┣ 📜converter32.dll

如何解决这个问题?

c libraries delphi-7 jsonparser stdcall
© www.soinside.com 2019 - 2024. All rights reserved.