[笔记]Delphi 2007写DLL供VC调用实例

考虑如下几种常用情况:

- VC传入int,返回int
- VC传入char *,返回int
- VC传入char *,返回char *及int

为简化问题,传递的字符串参数只考虑ANSI格式,不考虑UNICODE。
如果需要UNICODE,可以自行使用字符串格式转换函数。

Delphi 2007的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
library DemoDll;
 
uses
  SysUtils, Classes, Windows, StrUtils;
 
{$R *.res}
 
// Write to log file
procedure AddLog(const AFormat: string; Args: array of const); overload;
var
  LogFile: TextFile;
  FileName: string;
begin
  FileName := GetEnvironmentVariable('TEMP') + '\DemoDll.log';
  try
    AssignFile(LogFile, FileName);
 
    if FileExists(FileName) then
      Append(LogFile)
    else
      ReWrite(LogFile);
 
    Writeln(LogFile, Format('[%s] ', [DateTimeToStr(Now)]) + Format(AFormat, Args));
  finally
    CloseFile(LogFile);
  end;
end;
 
// Write to log file
procedure AddLog(const AFormat: string); overload;
begin
  AddLog(AFormat, []);
end;
 
// return a+b
function TestAdd(a, b: Integer): Integer; stdcall;
begin
  AddLog('TestAdd(%d,%d)', [a, b]);
  Result := a + b;
end;
 
// do nothing, just log input string
function TestInputStr(PInStr: PChar): Integer; stdcall;
var
  str: string;
begin
  str := StrPas(PInStr);
  AddLog('TestInputStr(%s)', [str]);
  Result := 0;
end;
 
// reverse first string and write to second string
function TestOutputStr(PInStr, POutStr: PChar): Integer; stdcall;
var
  str: string;
begin
  str := StrPas(PInStr);
  str := ReverseString(str);
  StrCopy(POutStr, PChar(str));
  AddLog('TestOutputStr(%s)', [str]);
  Result := 0;
end;
 
exports
  TestAdd,
  TestInputStr,
  TestOutputStr;
 
begin
  AddLog('BEGIN');
end.

  

VC2013的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Put DemoDll.dll in the same dir
// DemoDll.dll is developed by Delphi
 
#include "windows.h"
#include <iostream>
 
typedef int(__stdcall  *fTestAdd)(int, int);
typedef int(__stdcall  *fTestInputStr)(char *);
typedef int(__stdcall  *fTestOutputStr)(char *, char *);
 
using namespace std;
 
int main(int argc, char* argv[])
{
    HINSTANCE       hDllLibrary = NULL;
     
    fTestAdd        TestAdd = NULL;
    fTestInputStr   TestInputStr = NULL;
    fTestOutputStr  TestOutputStr = NULL;
     
    hDllLibrary = LoadLibraryA("DemoDll.dll");
    if (hDllLibrary){
        TestAdd = (fTestAdd)GetProcAddress(hDllLibrary, "TestAdd");
        TestInputStr = (fTestInputStr)GetProcAddress(hDllLibrary, "TestInputStr");
        TestOutputStr = (fTestOutputStr)GetProcAddress(hDllLibrary, "TestOutputStr");
 
        // check if a == 3
        int a = TestAdd(1, 2);
        cout << "TestAdd(1,2)=" << a << endl;
 
        // check if input string has output to log file
        char b[] = "nothing";
        TestInputStr(b);
        cout << "TestInputStr(" << b << ")" << endl;
 
        // check if output string is reversed
        char c[] = "nothing";
        char d[256] = { 0 };
        TestOutputStr(c, d);
        cout << "TestOutputStr(" << c << ")=" << d << endl;
    }
 
    getchar();
    return 0;
}
 
// Console output:
//
// TestAdd(1, 2) = 3
// TestInputStr(nothing)
// TestOutputStr(nothing) = gnihton
//
// %Temp%/DemoDll.log
//[2017 / 6 / 4 14:48 : 46] BEGIN
//[2017 / 6 / 4 14:48 : 46] TestAdd(1, 2)
//[2017 / 6 / 4 14:48 : 46] TestInputStr(nothing)
//[2017 / 6 / 4 14:48 : 46] TestOutputStr(gnihton)

  

 

posted @   ET民工[源自火星]  阅读(611)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示