编程语言
首页 > 编程语言> > C#调用Go版DLL

C#调用Go版DLL

作者:互联网

private void button6_Click(object sender, EventArgs e)
{
    byte[] inParam = null;
    IntPtr ptr = IntPtr.Zero;
    int outlen = -1;
    string outstr = "";

    inParam = Encoding.UTF8.GetBytes("执着不可取");

    int ret = Inwhtl_DLL.TestApi(inParam,ref ptr, ref outlen);

    if(outlen > 0)
    {
        outstr = Marshal.PtrToStringAnsi(ptr, (int)outlen);
        byte[] byt = strToToHexByte(outstr);
        outstr = Encoding.UTF8.GetString(byt);

        MessageBox.Show(outstr);

    }
}

/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] strToToHexByte(string hexString)
{
    hexString = hexString.Replace(" ", "");
    if ((hexString.Length % 2) != 0)
        hexString += " ";
    byte[] returnBytes = new byte[hexString.Length / 2];
    for (int i = 0; i < returnBytes.Length; i++)
        returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    return returnBytes;
}


package main

import "C"
import (
    "e.coding.net/jiftle/inwarehousetool/checktool/chktl"
    "encoding/hex"
    "fmt"
    "os"
)

//export Sum
func Sum(a int, b int) int {
    //最简单的计算和
    return a + b
}

//export TestApi
func TestApi(inParam *C.char, outParam **C.char, outlen *C.int) int {
    // 输入参数
    in := C.GoString(inParam)

    // 注意事项
    s := "1122ABC你" + in

    bytS := []byte(s)
    s = hex.EncodeToString(bytS)

    // 输出参数
    *outParam = C.CString(s)
    var noutlen int
    noutlen = len(C.GoString(*outParam))
    *outlen = C.int(noutlen)

    return 0
}

 

 

https://www.cnblogs.com/Leo_wl/p/12075987.html

 

标签:inParam,C#,hexString,outlen,int,outstr,Go,byte,DLL
来源: https://www.cnblogs.com/jiftle/p/12817334.html