golang 编译 dll 过程中需要用到 gcc,所以先安装 MinGW。

windows 64 位系统应下载 MinGW 的 64 位版本: https://sourceforge.net/projects/mingw-w64/

下载后运行 mingw-w64-install.exe,完成 MingGW 的安装。

首先撰写 golang 程序 exportgo.go:

package main

import "C"
import "fmt" //export PrintBye
func PrintBye() {
fmt.Println("From DLL: Bye!")
} //export Sum
func Sum(a int, b int) int {
return a + b;
} func main() {
// Need a main function to make CGO compile package as C shared library
}

编译成 DLL 文件:

go build -buildmode=c-shared -o exportgo.dll exportgo.go

编译后得到 exportgo.dll 和 exportgo.h 两个文件。

参考 exportgo.h 文件中的函数定义,撰写 C# 文件 importgo.cs:

using System;
using System.Runtime.InteropServices; namespace HelloWorld
{
class Hello
{
[DllImport("exportgo.dll", EntryPoint="PrintBye")]
static extern void PrintBye(); [DllImport("exportgo.dll", EntryPoint="Sum")]
static extern int Sum(int a, int b); static void Main()
{
Console.WriteLine("Hello World!");
PrintBye(); Console.WriteLine(Sum(33, 22));
}

编译 CS 文件得到 exe

csc importgo.cs

将 exe 和 dll 放在同一目录下,运行。

>importgo.exe

Hello World!
From DLL: Bye!
55

golang 中的 string 参数在 C# 中可以如下引用:

public struct GoString
{
public string Value { get; set; }
public int Length { get; set; } public static implicit operator GoString(string s)
{
return new GoString() { Value = s, Length = s.Length };
} public static implicit operator string(GoString s) => s.Value;
}

  

// func.go
package main import "C"
import "fmt" //export Add
func Add(a C.int, b C.int) C.int {
return a + b
} //export Print
func Print(s *C.char) {
/*
函数参数可以用 string, 但是用*C.char更通用一些。
由于string的数据结构,是可以被其它go程序调用的,
但其它语言(如 python)就不行了
*/
print("Hello ", C.GoString(s)) //这里不能用fmt包,会报错,调了很久...
}
func main() {
}                                                                                                                                        
 

编译
go build -ldflags "-s -w" -buildmode=c-shared -o func.dll func.go
还是有点大的,880KB,纯C 编译的只有48KB,应该是没有包含全部的依赖吧,go是全包进来了

Go 调用

package main
import (
"fmt"
"syscall"
) func main() {
dll := syscall.NewLazyDLL("func.dll")
add := dll.NewProc("Add")
prt := dll.NewProc("Print")
r, err, msg := add.Call(32, 44)
fmt.Println(r)
fmt.Println(err)
fmt.Println(msg) name := C.CString("Andy")
prt.Call(uintptr(unsafe.Pointer(name)))
}
out:
76
0
The operation completed successfully.
Hello Andy

Python 调用

from ctypes import CDLL, c_char_p
dll = CDLL("func.dll")
dll.Add(32, 33)
dll.Print(c_char_p(bytes("Andy", "utf8")))

C++调用

#include <iostream>
#include <windows.h> using namespace std;
typedef int(*pAdd)(int a, int b);
typedef void(*pPrt)(char* s); int main(int argc, char *argv[])
{
HMODULE dll= LoadLibraryA("func.dll");
pAdd add = (pAdd)GetProcAddress(dll, "Add");
pPrt prt = (pPrt)GetProcAddress(dll, "Print");
cout << add(321, 33) << endl;
prt("Andy");
FreeLibrary(dll);
return 0;
}

  

Golang 编译成 DLL 文件的更多相关文章

  1. Golang调用windows下的dll动态库中的函数 Golang 编译成 DLL 文件

    Golang调用windows下的dll动态库中的函数 package main import ( "fmt" "syscall" "time&quo ...

  2. C#.NET常见问题(FAQ)-如何将cs文件编译成dll文件 exe文件 如何调用dll文件

    比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...

  3. C#.NET如何将cs文件编译成dll文件 exe文件 如何调用dll文件

    比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...

  4. C#.NET常见问题(FAQ)-如何把写好的类编译成dll文件

    1 新建一个类库项目   2 直接改写这个Class1.cs文件   3 记得要添加Windows.Forms引用   4 我直接把在别的项目中做好的cs文件搞到这里来,连文件名也改了(FilesDi ...

  5. C#如何把写好的类编译成dll文件

    1 新建一个类库项目   2 直接改写这个Class1.cs文件   3 记得要添加Windows.Forms引用   4 我直接把在别的项目中做好的cs文件搞到这里来,连文件名也改了(FilesDi ...

  6. 如何把.cs文件编译成DLL文件

    开始--程序--Microsoft Visual Studio.NET 2013--Visual Studio.NET工具,点击其中的"VS2013 开发人员命令提示",就会进入M ...

  7. asp.net源程序编译为dll文件并调用的实现过程

    很多时候,我们需要将.cs文件单独编译成.dll文件,这就需要使用csc命令将.cs文件编译成.dll动态链接库文件.具体的操作步骤如下: 打开命令窗口->输入cmd到控制台->cd C: ...

  8. Go 程序编译成 DLL 供 C# 调用。

    Go 程序编译成 DLL 供 C# 调用. C# 结合 Golang 开发   1. 实现方式与语法形式 基本方式:将 Go 程序编译成 DLL 供 C# 调用. 1.1 Go代码 注意:代码中 ex ...

  9. Unity 代码编译成dll 更新dll实现热更代码

    Unity 代码编译成dll 更新dll实现热更代码 实现流程 代码编译成DLL DLL打包成AssetBundle 加载AssetBundle 加载代码程序集 获取指定类 使用反射赋值 C#代码编译 ...

随机推荐

  1. C++模板专门化与重载

    最近在复习C++有关知识,又重新看<<Effective C++>>,收获颇丰.原来以前看这边书,好多地方都是浅尝辄止.<<Effective C++>> ...

  2. HDU - 6321 Problem C. Dynamic Graph Matching (状压dp)

    题意:给定一个N个点的零图,M次操作,添加或删除一条边,每一次操作以后,打印用1,2,...N/2条边构成的匹配数. 分析:因为N的范围很小,所以可以把点的枚举状态用二进制表示集合.用一维数组dp[S ...

  3. webform中Repeater的Command用法、Repeater的替代方法

    Command: 在Repeater控件循环执行过程中,可以给每一项的某个按钮或其他控件设置CommandName.CommandArgument属性,用于在后台代码中获取单项 数据进行调用.   需 ...

  4. libvirt-qemu-虚拟机设备热插拔

    cpu热插拔 # virsh setvcpus $domain_name --count 4 --live (--config可写入配置文件永久保存) #前提条件和后续激活参考<libvirt- ...

  5. 关于div可编辑的复制粘贴问题

    todoFilter(e) { e.preventDefault(); if (!e.target.getAttribute('contenteditable')) { return; } const ...

  6. Treflection02_getMethods()_getMethod()

    1. package reflectionZ; import java.lang.reflect.Constructor; import java.lang.reflect.Method; impor ...

  7. GDI+ 双缓冲字体模糊

    只是记录自己的UI库,对其他估计没什么帮助 void CListCtrlUI::ReFillRect(HDC hdc){ if (!m_pImage) { Graphics gs(hdc); int ...

  8. LeetCode第[62]题(Java):Unique Paths 及扩展

    题目:唯一路径(机器人走方格) 难度:Medium 题目内容: A robot is located at the top-left corner of a m x n grid (marked 'S ...

  9. Node.js基础知识普及

    Node.js只支持单线程,故不会产生死锁,采用非阻塞I/O机制和事件环机制.非常适合与开发需要处理大量并发的输入/输出的应用程序.   一. Node.js的核心模块有很多,这里先写几个比较常用的( ...

  10. selenium学习笔记(webdriver下载配置)

    selenium安装后默认安装firefox可以直接使用,当然可以通过其它浏览器 博主这里整理了:chrome . IE 首先是下载地址 http://docs.seleniumhq.org/down ...