飞哥的海

导航

GOLANG:调用delphi7编写的dll

0. delphi对winapi有很强的封装,使其更易用。

1. delphi声明dll内函数 需要尽量以此方式:
   procedure test(data: pchar; count: integer);stdcall;

2.golang 调用方法:

    str := []byte("abcdedf")
    printTextDll := syscall.NewLazyDLL("demo.dll")
    printBytes := printTextDll.NewProc("test")
    printBytes.Call(uintptr(unsafe.Pointer(&str[0])), uintptr(4))

3. 运行后,大概率会提示,这是由于dll是32位的,golang编译环境为64位

go: no Go source files

4.运行 go env 查看当前运行环境 通常位amd64,此时运行下列指令位32位

go env -w GOARCH=386   
go env -w CGO_ENABLED=1

5. 再次运行代码,有可能出现错误提示 如下:

/ld.exe: cannot find -lmingwex
ld.exe: cannot find -lmingw32
collect2.exe: error: ld returned 1 exit status

6.这是由于mingw安装有异常,重新下载安装即可
  a.从此处https://sourceforge.net/projects/mingw/files/Installer/mingw-get-setup.exe/download下载 
  b.只需选中红框即可

   c.设置环境变量 path 如:c:\mingw\bin

7.再次运行代码即可

 

 

posted on 2023-09-19 11:14  飞哥的海  阅读(75)  评论(0编辑  收藏  举报