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¬ice=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