您的位置:首页 > 编程语言 > Delphi

c++builder delphi 调用dll dll编写

2015-11-17 09:43 405 查看

c++builder动态调用dll

// 定义
typedef int __stdcall MyFunction (int x, char *str);

int rtn = 0;
String dllName = "XXXX.dll";
HINSTANCE hInstance = LoadLibrary(dllName.c_str());
MyFunction * pMyFunction = (MyFunction*) GetProcAddress(hInstance, "rdcompany"); // 函数名称要正确
if (pMyFunction == NULL)
{
// 提示
}

rtn = pMyFunction(1, "aa");
FreeLibrary(hInstance);
hInstance = NULL;


C++builder的dll导出类

// Enable RTTI generation for private fields
#pragma explicit_rtti fields(private)

class __declspec(delphiclass) TBuffer {
private:
int wrHead, rdHead;

// ...
public:
TBuffer() {

}
};

http://docwiki.embarcadero.com/RADStudio/Berlin/en/Delphi_RTTI_and_C++Builder
静态调用

extern "C" __declspec(dllimport) int __cdecl test();

根据dll生成lib文件

cmd命令后

implib E:\MYDEV\CRT_310.lib E:\MYDEV\CRT_310.dll

调用的工程里添加lib文件或者#pragma link "CRT_310.lib"

这里HANDLE 是typedef void *HANDLE;

typedef HANDLE APIENTRY CommOpenWithBaut(char *Port, BYTE _BaudOption);

delphi静态调用dll

function Write_DF02(ucSFI: byte; wFileLen: Word; pucData: PByte): integer; stdcall external 'my.dll';
procedure Set_Path(pchPath: PAnsiChar); stdcall external 'my.dll';

function(var age: integer; var name: double): BOOL; stdcall

const
UrlMonLib = 'URLMON.DLL';

function CreateURLMoniker;                external UrlMonLib name 'CreateURLMoniker';
function readCardInfo_json():PAnsiChar;stdcall; external 'test.dll';


pansichar参数返回

name: array[0..50] of AnsiChar;

age:integer;

money:double;

fun(name,@age,@money);

fun(name,age,money);

用 name:TArray<AnsiChar>; 应该也可以。就是麻烦,定义和分配2行代码。

setlength(name,50);

delphi动态调用dll

//函数原型生命
type
Taddc = function: TStringList; stdcall;
TGetCPUID = function(CPUID: PAnsiChar): integer; stdcall;  //注1
//
var
hh: THandle;
addc: Taddc;
GetId: TGetCPUID;
temp: TStringList;
i: Integer;
begin
hh := LoadLibrary('DLL.dll');
try
if hh <> 0 then
//装载方法
@addc := GetProcAddress(hh, PChar('testStr'));
GetId := GetProcAddress(dllHandle, pchar('GetCPUID'));

//invoke
GetId(1);
if not (@addc = nil) then
begin
addc;
end
else
begin
RaiseLastWin32Error;
end;
finally
FreeLibrary(hh);
end;


delphi Berlin版本 如何给PAnsiChar赋值?

StrCopy(value, PAnsiChar(AnsiString(kvalue)));

因为是UnicodeString,所以必须先进行AnsiString转换。

#define DLLEXPORT_API extern "C" _declspec(dllexport)

dll函数导出 改名别名

delphi太简单了

procedure a(); stdcall;
begin

end;

exports
a name '@$xp$20Controls@TAnchorKind';


c++builder找不到方法
http://toby.logdown.com/posts/220527/c-builder-define-dll-export-function-names-using-def-file
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: