delphi 调用GoLang dll


本文整理自网络,侵删。

 

/*

 GoLang DLL example. Goals: load golang dll into fpc/lazarus, and load golang
 dll into another go executable.
 The syntax
   //export SomeFunc
 needs to be used above the function you want to export
 To compile this program run:
   go build -buildmode=c-archive exportgo.go
 then compile goDLL.c that exports the functions for GCC to link, and run:
   gcc -shared -pthread -o goDLL.dll goDLL.c exportgo.a -lWinMM -lntdll -lWS2_32
*/

package main

import "C"
import "fmt"

//export GetIntFromDLL
func GetIntFromDLL() int32 {
return 42
}

//export PrintHello
func PrintHello(name string) {
fmt.Printf("From DLL: Hello, %s!\n", name)
}

//export PrintBye
func PrintBye() {
fmt.Println("From DLL: Bye!")
}

func main() {
// Need a main function to make CGO compile package as C shared library
}

go build -buildmode=c-shared -o exportgo.dll exportgo.go  编译成dll


下面是delphi 调用代码
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
windows,
  System.SysUtils;


  var h: THandle;

type
  TProc = procedure(); cdecl;
var
  PrintBye: TProc;
const
  GO_LIB = 'goDLL.dll';
  PROC1 = 'PrintBye';


function LoadLib: boolean;
var p: pointer;
begin
  h := LoadLibrary(GO_LIB);
  writeln('handle: ', h);
  p := GetProcAddress(h, PROC1);
  if p = nil then begin
    writeln('error getting procedure address: ', PROC1);
    result := false;
  end;
  PrintBye := TProc(p);
end;

procedure UnloadLib;
begin
 closehandle(h);
end;





var
  c: char;
begin
  writeln('Loading library: ', GO_LIB);
  if LoadLib then begin
    PrintBye();
  end;

  writeln('Press enter to exit');
  readln;

  writeln('Unloading library: ', GO_LIB);
  UnloadLib;
  try
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

来源参考:https://github.com/z505/goDLL

相关阅读 >>

Delphi的tservice 服务路径获取 dll中获取文件路径

Delphi string.create(char,count)

tidtcpserver控件中文指南

Delphi 日期时间函数

Delphi 获取unixtime

Delphi xe5 安卓设置屏幕横屏竖屏

Delphi idhttpserver的使用方法

Delphi中combobox的datavalue值

Delphi fmx 截图

Delphi 检测 iat hook api 的方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...