未处理的异常:System.EntryPointNotFoundException:无法在

我试图从 c# 调用 golang dll,并将结果和性能与从 c# 调用 c dll 进行比较,所以我做了以下操作:


我开始构建 c dll 并将其称为第 1 步:编写 C 代码


// cmdll.c

// Compile with: -LD

int __declspec(dllexport) SampleMethod(int i)

{

  return i*10;

}

第 2 步:编译 C 代码:


打开Visual Studio x64 Native Tools Command Prompt

运行命令:cl -LD cmdll.c

第 3 步:编写 C# 代码


// cm.cs

using System;

using System.Runtime.InteropServices;

public class MainClass

{

    [DllImport("Cmdll.dll")]

    public static extern int SampleMethod(int x); // function signature, must have a return type


    static void Main()

    {

        Console.WriteLine("SampleMethod() returns {0}.", SamplMethod(5));

    }

}

第 4 步:编译 c# 文件并将 exe 构建为:


打开Visual Studio x64 Native Tools Command Prompt

运行命令:csc -platform:x64 cm.cs

上面的事情运行顺利


我想使用 golang 做同样的事情,并遵循以下内容:


第一步:编写go代码:


//lib.go

package main


import "C"


//export SamplMethod

func SamplMethod(i int) int {

    return i * 10

}


func main() {

    // Need a main function to make CGO compile package as C shared library

}

第二步:构建dll文件,将上面的代码编译为:


go build -ldflags="-s -w" -o lib.dll -buildmode=c-shared lib.go

我使用 来-ldflags="-s -w"减小生成的文件大小,但不确定-buildmode我应该使用什么,所以随机选择c-shared而不是c-archive 更新:我也尝试过go build -ldflags="-s -w" -o lib.dll -buildmode=c-archive lib.go并得到相同的结果


第 3 步:编写 ac 代码,将.dll和.h生成的文件结合起来go生成等效文件c dll


//goDll.c

#include <stdio.h>

#include "lib.h"

// force gcc to link in go runtime (may be a better solution than this)

GoInt SamplMethod(GoInt i);


void main() {

}

第 4 步:将 goDll.c 文件编译为:


gcc -shared -pthread -o goDll.dll goDll.c lib.dll -lWinMM -lntdll -lWS2_32

第 5 步:构建 c# 代码以调用生成的 dll,代码与上面相同,但更改 dll 文件名:


// cm.cs

using System;

using System.Runtime.InteropServices;

public class MainClass

{

    [DllImport("goDll.dll")]

    public static extern int SampleMethod(int x); // function signature, must have a return type


    static void Main()

    {

        Console.WriteLine("SampleMethod() returns {0}.", SamplMethod(5));

    }

}


一只斗牛犬
浏览 929回答 1
1回答

慕妹3146593

它与我一起工作如下:第一步:编写go代码:// main.gopackage mainimport "C"import "fmt"//export HelloWorldfunc HelloWorld() {&nbsp; &nbsp; fmt.Printf("hello world from GO\n")}func main() {}// compile the code as:// go build -ldflags="-s -w" -buildmode=c-shared -o libgo.dll main.go第 2 步:将 C# 代码编写为:// main.csusing System;using System.Runtime.InteropServices;public class MainClass{&nbsp; &nbsp; [DllImport("libgo.dll")]&nbsp; &nbsp; public static extern void HelloWorld(); // function signature, must have a return type&nbsp; &nbsp; static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; HelloWorld();&nbsp; &nbsp; }}// compile as:// open:// Visual Studio x64 Native Tools Command Prompt// csc -platform:x64 cm.cs第三步:编译这两个文件,从编译go文件开始第四步:运行可执行文件:更新去文件:// main.gopackage mainimport (&nbsp; &nbsp; "fmt")// The import "C" should come directly after the // #include ..., i.e. no empty lines allowed,// if there are empty lines, the compliler will read the // #include as normal comment, not as C import/*#include <stdlib.h>*/import "C"//export GetHellofunc GetHello(Input *C.char) *C.char {&nbsp; &nbsp; cStr := C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))&nbsp; &nbsp; //&nbsp; C.free(unsafe.Pointer(cStr))&nbsp; &nbsp; return cStr}func main() {}// compile the code as:// go build -ldflags="-s -w" -buildmode=c-shared -o libgo.dll main.goC#文件:// main.csusing System;using System.Text;using System.Runtime.InteropServices;public class MainClass{&nbsp; &nbsp; [DllImport("libgo.dll", CharSet = CharSet.Unicode,&nbsp;&nbsp; &nbsp; CallingConvention = CallingConvention.StdCall)]&nbsp; &nbsp; public static extern IntPtr GetHello(byte[] data);&nbsp; &nbsp; static string CallDll(string name) {&nbsp; &nbsp; &nbsp; &nbsp; IntPtr output= IntPtr.Zero;&nbsp; &nbsp; &nbsp; &nbsp; var a = GetHello(Encoding.UTF8.GetBytes(name));&nbsp; &nbsp; &nbsp; &nbsp; return "GetHello Returns: " + Marshal.PtrToStringAnsi(a);&nbsp; &nbsp; }&nbsp; &nbsp; static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(CallDll("Ahmad"));&nbsp; &nbsp; }}// compile as:// open:// Visual Studio x64 Native Tools Command Prompt// csc -platform:x64 main.cs这是一个使用字符串的示例:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go