douhui0975 2018-01-11 13:20 采纳率: 100%
浏览 961
已采纳

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

我一直试图遵循z505/goDLL的方法,但遇到了一个大问题。该方法无法返回字符串,我无法读取结果的输出变量。

这是我目前使用的代码(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!
", 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 + "
");
        Console.WriteLine("PrintHello2 Returns: " + PrintHello2(Encoding.UTF8.GetBytes(res), ref output));
        Console.WriteLine("Ref Val changed to: " + Encoding.UTF8.GetString(output) + "
");
    }
}

预期产出结果:

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!

我没有发现错误。

  • 写回答

1条回答 默认 最新

  • dongzhong1929 2018-01-12 13:43
    关注

    Anyway, after some time trying and error"ing", this is the solution

    Go

    //export PrintHello2
    func PrintHello2(Input *C.char, Output **C.char) int32 {
        *Output = C.CString(fmt.Sprintf("From DLL: Hello, %s!
    ", 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!
    ", 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));
        }
    }
    

    It will probably be used by many others that might be trying to accomplish the same as i am.

    Thanks for the tips and pacience JimB

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么nacos注册失败了呀?
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥15 抖音看过的视频,缓存在哪个文件