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

VC调用Delphi制作的动态链接库如何互相传递字符串

2008-07-10 09:17 645 查看
1、VC to Delphi

Delphi源程序:

library ExportChartData;

uses

SysUtils,

Classes;

{$R *.res}

const

DLL_VER : Word = $0001;

function GetVer: Word;

begin

Result := DLL_VER;

end;

procedure SayHello(aString:pchar);cdecl;

begin

if aString = 'Hello!' then Beep;

end;

exports

GetVer,

SayHello;

begin

end.
VC源程序

// TestInterface.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <windows.h>

int main(int argc, char* argv[])

{

char *szDllName = "..\\..\\OBJ\\ExportChartData.dll";

HINSTANCE hInstance = LoadLibrary(szDllName);

if (hInstance != NULL)

{

void (*f)(char &) = (void (*)(char &))GetProcAddress(hInstance, "SayHello");

char *s = "Hello!";

f(*s);

}

FreeLibrary(hInstance);

return 0;

}

2、Delphi to VC

[b]Delphi源程序


library ExportChartData;

uses

SysUtils,

Classes;

{$R *.res}

const

DLL_VER : Word = $0001;

function GetVer: Word;

begin

Result := DLL_VER;

end;

procedure SayHello(index:Integer;aString:pchar);cdecl;

begin

if index = 0 then strcopy(aString,'A Test for Pass String!')

else

strcopy(aString,'OK!');

end;

exports

GetVer,

SayHello;

begin

end.

[/b]VC源程序

// TestInterface.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <windows.h>

#include <stdio.h>

typedef char* PCHAR;

int main(int argc, char* argv[])

{

PCHAR szDllName = "..\\..\\OBJ\\ExportChartData.dll";

HINSTANCE hInstance = LoadLibrary(szDllName);

char *s = new char[255];

if (hInstance != NULL)

{

void (*f)(int, char *) = (void (*)(int, char *))GetProcAddress(hInstance, "SayHello");

f(1, s);

printf("%s",s);

}

FreeLibrary(hInstance);

delete[]s;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: