Git Product home page Git Product logo

Comments (4)

hankchan avatar hankchan commented on June 9, 2024

对,这是一个问题。我也正在尝试解决。

from goctp.

sunnyboy00 avatar sunnyboy00 commented on June 9, 2024

%typemap(gotype) (char **ppInstrumentID, int nCount) "[]string"

%typemap(in) (char *ppInstrumentID, int nCount)
%{
{
int i;
gostring
a;

$2 = $input.len;
a = (_gostring_*) $input.array;
$1 = (char **) malloc (($2 + 1) * sizeof (char *));
for (i = 0; i < $2; i++) {
  
  /* Not work */
  //_gostring_ *ps = &a[i];
  //$1[i] = (char *) ps->p;
  //$1[i][ps->n] = '\0';

  /*Work well*/
  _gostring_ *ps = &a[i];
  $1[i] = (char*) malloc(ps->n + 1);
  memcpy($1[i], ps->p, ps->n);
  $1[i][ps->n] = '\0';

}
$1[i] = NULL;

}
%}

%typemap(argout) (char *ppInstrumentID, int nCount) "" / override char *[] default */

%typemap(freearg) (char **ppInstrumentID, int nCount)
%{
{
int i;
for (i = 0; i < $2; i++)
{
free ($1[i]);
}
free($1);
}
%}

把这段代码改掉,参照下面的代码,用golang 实现应该就可以了,对swig 不太熟悉,不知道怎么更改这段

func Test(args[] string){
arg := make([](* Ctype_char), 0) //C语言char*指针创建切片
l := len(args)
for i,
:= range args{
char := C.CString(args[i])
defer C.free(unsafe.Pointer(char)) //释放内存
strptr := (_Ctype_char)(unsafe.Pointer(char))
arg = append(arg, strptr) //将char
指针加入到arg切片
}

C.test(C.int(l), (_Ctype_char)(unsafe.Pointer(&arg[0]))) //即c语言的main(int argc,charargv)
}

from goctp.

sunnyboy00 avatar sunnyboy00 commented on June 9, 2024

///订阅行情
func (p *GoCThostFtdcMdSpi) SubscribeMarketData(name []string) {

arg := make([](*C.char), 0)
l := len(name)
for i, _ := range name {
	char := C.CString(name[i])
	defer C.free(unsafe.Pointer(char)) //释放内存
	strptr := (*C.char)(unsafe.Pointer(char))
	arg = append(arg, strptr) //将char指针加入到arg切片
}

iResult := p.Client.MdApi.SubscribeMarketData((*string)(unsafe.Pointer(&arg[0])), int(l))

if iResult != 0 {
	log.Println("发送行情订阅请求: 失败.")
} else {
	log.Println("发送行情订阅请求: 成功.")
}

}
这样就可以了

from goctp.

ceasar09 avatar ceasar09 commented on June 9, 2024

楼上正解 好牛

from goctp.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.