cc623213878 2019-07-30 20:10 采纳率: 0%
浏览 1405

c#调用go语言生成的dll传string不成功,急!

图片说明

c#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace test
{
    [StructLayout(LayoutKind.Sequential)]
    struct GoString
    {
        public char[] str;
        public int length;
    };
class Program
    {

            //System.Runtime.InteropServices.MarshalDirectiveException:“方法的类型签名与 PInvoke 不兼容。”
        [DllImport("SearchMusic.dll", CharSet = CharSet.Unicode)]
        static extern GoString Search(GoString key,int apageNum, int pageSize);


                //这个函数能调用成功
        [DllImport("SearchMusic.dll", EntryPoint = "Add")]
        static extern int Add(int a, int b);
        static void Main()
        {
            GoString str = new GoString();
            StringBuilder sb = new StringBuilder("沙漠骆驼");
            str.str= ("沙漠骆驼").ToCharArray();
            str.length = str.str.Length;
            Search(str, 1, 10);
        }
    }
}

go语言代码

package main

import (
    "C"
    "fmt"
    "io/ioutil"
    "net/http"
)

//Search :
//export Search
func Search(key string, pageNum int, pageSize int) {
    url := "https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=57152133376853167&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&w=" + key + "&p=" + fmt.Sprintf("%d", pageNum) + "&n=" + fmt.Sprintf("%d", pageSize)

    resp, _ := http.Get(url)
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    json := string(body)
    fmt.Println(json)

}

//Add :
//export Add
func Add(a int, b int) int {
    return a + b
}
func main() {

}

go生成dll时的.h文件

/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package command-line-arguments */


#line 1 "cgo-builtin-export-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif

/* Start of preamble from import "C" comments.  */




/* End of preamble from import "C" comments.  */


/* Start of boilerplate cgo prologue.  */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif


//Search :

extern void Search(GoString p0, GoInt p1, GoInt p2);

//Add :

extern GoInt Add(GoInt p0, GoInt p1);

#ifdef __cplusplus
}
#endif

  • 写回答

5条回答 默认 最新

  • threenewbee 2019-07-31 08:48
    关注

    关键是 GoString
    这个一看就不对,你的数组没有长度,也没有用FieldOffset
    另外Go那里是不是要的指针而不是直接传结构体

    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型