登录
首页 >  Golang >  Go问答

使用生成的 Golang DLL 返回字符串或 *C.Char

来源:Golang技术栈

时间:2023-04-17 10:51:27 165浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《使用生成的 Golang DLL 返回字符串或 *C.Char》将会介绍到golang等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我一直在尝试关注z505/goDLL repo 并遇到了一个大问题。如果我无法读取结果的输出变量,该方法将无法返回字符串。

这是我目前使用的代码(Go)(完整代码https://play.golang.org/p/Yfg85DCeMLh)

//export PrintHello2
func PrintHello2(Input *C.char, Output **C.char) int32 {
    fmt.Println(C.GoString(Input))
    *Output = C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))
    fmt.Println("Message: ", C.GoString(*Output))
    return 1
}

//export PrintHello3
func PrintHello3(Input *C.char, Output *int32) int32 {
    fmt.Println(C.GoString(Input))
    *Output = 3
    fmt.Println("Value: ", *Output)
    return 0
}

C# 测试代码

class Program
{
    [DllImport("goDLL.dll", CharSet = CharSet.Unicode)]
    public static extern int PrintHello2(byte[] data, ref byte[] output);

    [DllImport("goDLL.dll", CharSet = CharSet.Unicode)]
    public static extern int PrintHello3(byte[] data, ref int output);

    static void Main(string[] args)
    {
        string res = "demo";
        byte[] output = null;
        Int32 refVal = 0;

        Console.WriteLine("PrintHello3 Returns: " + PrintHello3(Encoding.UTF8.GetBytes(res), ref refVal));
        Console.WriteLine("Ref Val changed to: " + refVal + "\n");
        Console.WriteLine("PrintHello2 Returns: " + PrintHello2(Encoding.UTF8.GetBytes(res), ref output));
        Console.WriteLine("Ref Val changed to: " + Encoding.UTF8.GetString(output) + "\n");
    }
}

预期输出结果:

C:\tmp\DLL>ConsoleApp.exe
demo
Value:  3
PrintHello3 Returns: 0
Ref Val changed to: 3

demo
Message:  From DLL: Hello, demo!
PrintHello2 Returns: 1
Ref Val changed to: From DLL: Hello, demo!

当前结果:

C:\tmp\DLL>ConsoleApp.exe
demo
Value:  3
PrintHello3 Returns: 0
Ref Val changed to: 3

demo
Message:  From DLL: Hello, demo!

没有可见的恐慌,没有发现错误。只是输出不完整thx

正确答案

无论如何,经过一段时间的尝试和错误“ing”,这就是解决方案

//export PrintHello2
func PrintHello2(Input *C.char, Output **C.char) int32 {
    *Output = C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))
    return int32(len(C.GoString(*Output)))
}

//export PrintHello4
func PrintHello4(Input *C.char) *C.char{
    return C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))
}

C#

class Program
{
    [DllImport("goDLL.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
    public static extern int PrintHello2([In] byte[] data, ref IntPtr output);

    [DllImport("goDLL.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr PrintHello4(byte[] data);

    static void Main(string[] args)
    {
        string res = "demo";
        IntPtr output= IntPtr.Zero;
        var a = PrintHello4(Encoding.UTF8.GetBytes(res));

        Console.WriteLine("PrintHello4 Returns: " + Marshal.PtrToStringAnsi(a));
        var i = PrintHello2(Encoding.UTF8.GetBytes(res), ref output);

        Console.WriteLine("PrintHello2 Returns: " + i);
        Console.WriteLine("Ref Val changed to: " + Marshal.PtrToStringAnsi(output, i));
    }
}

它可能会被许多其他可能试图完成与我一样的人使用。

感谢您的提示和耐心 JimB

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>