├── cs ├── test.dll └── test.cs ├── go ├── make.bat ├── test.def └── test.go ├── README.md ├── .gitignore └── LICENSE /cs/test.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baozisoftware/go-dll/HEAD/cs/test.dll -------------------------------------------------------------------------------- /go/make.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baozisoftware/go-dll/HEAD/go/make.bat -------------------------------------------------------------------------------- /go/test.def: -------------------------------------------------------------------------------- 1 | EXPORTS 2 | Sum 3 | Hello 4 | GetStr 5 | GetBytes -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-dll 2 | 使用Go语言生成dll并使用C#调用的示例(具体实现步骤见[Wiki](https://github.com/Baozisoftware/go-dll/wiki). 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /go/test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "C" 4 | import "fmt" 5 | 6 | //export Sum 7 | func Sum(arg1, arg2 int32) int32 { 8 | return arg1 + arg2 9 | } 10 | 11 | //export Hello 12 | func Hello() { 13 | fmt.Println("hello world from go dll!") 14 | } 15 | 16 | //export GetStr 17 | func GetStr() string { 18 | return "Hello 世界!" 19 | } 20 | 21 | //export GetBytes 22 | func GetBytes() []byte { 23 | return []byte(fmt.Sprintf("%s\n%s",GetStr(),"世界、こんにちは!")) 24 | } 25 | 26 | func main(){} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, Baozisoftware 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /cs/test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | using System.Text; 4 | 5 | namespace GolangDllTest 6 | { 7 | 8 | //本示例使用32位版本的DLL. 9 | class Program 10 | { 11 | 12 | //以下两个结构体来自test.h,注意32位,64位版本的数据长度. 13 | struct GoString 14 | { 15 | public IntPtr p; 16 | public int n; 17 | } 18 | 19 | struct GoSlice 20 | { 21 | public IntPtr data; 22 | public int len; 23 | public int cap; 24 | } 25 | 26 | //DLL引用声明 27 | [DllImport("test", CallingConvention = CallingConvention.Cdecl)] 28 | static extern int Sum(int a, int b); 29 | 30 | [DllImport("test", CallingConvention = CallingConvention.Cdecl)] 31 | static extern void Hello(); 32 | 33 | [DllImport("test", CallingConvention = CallingConvention.Cdecl)] 34 | static extern GoString GetStr(); 35 | 36 | [DllImport("test", CallingConvention = CallingConvention.Cdecl)] 37 | static extern GoSlice GetBytes(); 38 | 39 | static void Main(string[] args) 40 | { 41 | Environment.SetEnvironmentVariable("GODEBUG", "cgocheck=0"); //如果DLL有返回数组(切片),这一行是必须的,并且必须在DLL文件加载前设置. 42 | 43 | Hello(); //调用Hello 44 | 45 | Console.WriteLine(Sum(5, 15)); //调用Sum 46 | 47 | GoString str = GetStr(); //调用GetStr 48 | //以下为GoString转String 49 | byte[] bytes = new byte[str.n]; 50 | for (int i = 0; i < str.n; i++) 51 | bytes[i] = Marshal.ReadByte(str.p, i); 52 | string s = Encoding.UTF8.GetString(bytes); 53 | Console.WriteLine(s); //输出结果 54 | 55 | GoSlice arr = GetBytes(); //调用GetBytes 56 | //以下为GoSlice转Array 57 | bytes = new byte[arr.len]; 58 | for (int i = 0; i < arr.len; i++) 59 | bytes[i] = Marshal.ReadByte(arr.data, i); 60 | //Byte[] to String 61 | s = Encoding.UTF8.GetString(bytes); 62 | Console.WriteLine(s); //输出结果 63 | } 64 | } 65 | } --------------------------------------------------------------------------------