第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未處理的異常:System.EntryPointNotFoundException:無法在

未處理的異常:System.EntryPointNotFoundException:無法在

Go
一只斗牛犬 2023-03-07 14:08:12
我試圖從 c# 調(diào)用 golang dll,并將結(jié)果和性能與從 c# 調(diào)用 c dll 進(jìn)行比較,所以我做了以下操作:我開始構(gòu)建 c dll 并將其稱為第 1 步:編寫 C 代碼// cmdll.c// Compile with: -LDint __declspec(dllexport) SampleMethod(int i){  return i*10;}第 2 步:編譯 C 代碼:打開Visual Studio x64 Native Tools Command Prompt運行命令:cl -LD cmdll.c第 3 步:編寫 C# 代碼// cm.csusing 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 構(gòu)建為:打開Visual Studio x64 Native Tools Command Prompt運行命令:csc -platform:x64 cm.cs上面的事情運行順利我想使用 golang 做同樣的事情,并遵循以下內(nèi)容:第一步:編寫go代碼://lib.gopackage mainimport "C"//export SamplMethodfunc SamplMethod(i int) int {    return i * 10}func main() {    // Need a main function to make CGO compile package as C shared library}第二步:構(gòu)建dll文件,將上面的代碼編譯為:go build -ldflags="-s -w" -o lib.dll -buildmode=c-shared lib.go我使用 來-ldflags="-s -w"減小生成的文件大小,但不確定-buildmode我應(yīng)該使用什么,所以隨機選擇c-shared而不是c-archive 更新:我也嘗試過go build -ldflags="-s -w" -o lib.dll -buildmode=c-archive lib.go并得到相同的結(jié)果第 3 步:編寫 ac 代碼,將.dll和.h生成的文件結(jié)合起來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 步:構(gòu)建 c# 代碼以調(diào)用生成的 dll,代碼與上面相同,但更改 dll 文件名:// cm.csusing 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));    }}
查看完整描述

1 回答

?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗 獲得超9個贊

它與我一起工作如下:


第一步:編寫go代碼:


// main.go

package main


import "C"

import "fmt"


//export HelloWorld

func HelloWorld() {

    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.cs

using System;

using System.Runtime.InteropServices;

public class MainClass

{

    [DllImport("libgo.dll")]

    public static extern void HelloWorld(); // function signature, must have a return type


    static void Main()

    {

        HelloWorld();

    }

}


// compile as:

// open:

// Visual Studio x64 Native Tools Command Prompt

// csc -platform:x64 cm.cs

第三步:編譯這兩個文件,從編譯go文件開始


第四步:運行可執(zhí)行文件:

http://img1.sycdn.imooc.com//6406d4e30001e73617560935.jpg

更新


去文件:

// main.go

package main


import (

    "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 GetHello

func GetHello(Input *C.char) *C.char {

    cStr := C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))

    //  C.free(unsafe.Pointer(cStr))

    return cStr

}


func main() {}


// compile the code as:

// go build -ldflags="-s -w" -buildmode=c-shared -o libgo.dll main.go

C#文件:

// main.cs

using System;

using System.Text;

using System.Runtime.InteropServices;

public class MainClass

{

    [DllImport("libgo.dll", CharSet = CharSet.Unicode, 

    CallingConvention = CallingConvention.StdCall)]

    public static extern IntPtr GetHello(byte[] data);


    static string CallDll(string name) {

        IntPtr output= IntPtr.Zero;

        var a = GetHello(Encoding.UTF8.GetBytes(name));


        return "GetHello Returns: " + Marshal.PtrToStringAnsi(a);

    }


    static void Main()

    {

        Console.WriteLine(CallDll("Ahmad"));

    }

}


// compile as:

// open:

// Visual Studio x64 Native Tools Command Prompt

// csc -platform:x64 main.cs

這是一個使用字符串的示例:

http://img1.sycdn.imooc.com//6406d4f600013a5e19200763.jpg

查看完整回答
反對 回復(fù) 2023-03-07
  • 1 回答
  • 0 關(guān)注
  • 1171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號