Jos*_*eMG 1 c++ delphi dll pascal freepascal
首先,我使用Mingw 4.8作为代码中的C++ DLL的编译器:块13.12和Lazarus 1.4.2用于处理pascal代码.(windows 7)
我需要在c ++或c中生成一个可以从pascal程序调用的dll.
问题是我的关于pascal的知识是空的,制作一个简单的程序看起来并不复杂,但我找不到关于如何导入和使用C/C++ DLL的好信息.
唯一没有用的是:http://www.drbob42.com/delphi/headconv.htm 我的真实代码:
帕斯卡尔:
funtion hello():Integer; external 'function' index 1;
...
Label1.Caption:=IntToStr(hello());
Run Code Online (Sandbox Code Playgroud)
C++ DLL头:
#ifndef function_H
#define function_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_DLL
#define FUNCTION_DLL __declspec(dllexport)
#else
#define FUNCTION_DLL __declspec(dllimport)
#endif
int __stdcall FUNCTION_DLL hello( );
#ifdef __cplusplus
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
C++文件:
#include <stdio.h>
#include "function.h"
__stdcall int hello( )
{
return 8;
}
Run Code Online (Sandbox Code Playgroud)
但是当试图传递任何参数或做一些复杂的函数时,开始给出randoms数字.
这是新代码:Pascal:
function function1(t1:Integer):Integer; external 'function' index 1;
...
entero:=8;
Label1.Caption:=IntToStr(function1(entero2));
Run Code Online (Sandbox Code Playgroud)
我还将c ++代码更新为:
C++:
#include <stdio.h>
#include "function.h"
__stdcall int function1(int t1)
{
return t1*2;
}
Run Code Online (Sandbox Code Playgroud)
标题:
#ifndef funtion_H
#define funtion_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_DLL
#define FUNCTION_DLL __declspec(dllexport)
#else
#define FUNCTION_DLL __declspec(dllimport)
#endif
int __stdcall FUNCTION_DLL function1(int t1);
#ifdef __cplusplus
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
我还读了这个其他信息:http://www.jrsoftware.org/ishelp/index.php?topic = scriptdll .并试图像这样实现dll调用:
帕斯卡尔:
function function1(t1: Integer): Integer; external 'function1@files:function.dll';
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误说:
无法在动态链接库function.dll中找到过程入口点function1
我正在寻找一个可行的示例或在线教程或继续工作的东西,因为我非常坚持这一点.先感谢您.
您需要使调用约定匹配.您的C++代码使用__stdcall.Pascal代码未指定调用约定,因此默认为register.
像这样声明Pascal导入:
function function1(t1:Integer):Integer; stdcall; external 'function' index 1;
Run Code Online (Sandbox Code Playgroud)
你确定在导入时需要使用索引吗?按名称导入比按顺序导入更常见.我希望看到导入看起来像这样:
function function1(t1:Integer):Integer; stdcall; external 'function';
Run Code Online (Sandbox Code Playgroud)
没有参数的函数成功的原因是对于无参数函数,调用约定之间的差异无关紧要.一旦开始传递参数,stdcall意味着参数通过堆栈传递,并且register意味着它在寄存器中传递.这种不匹配解释了您观察到的行为.