char* Zqdm[]如何更改成golang的入参呢

myfunc(byte Market[], char* Zqdm[], short& Count)这个函数的三个入参,如何用golang重新定义这个函数呢,

原来的调用是如下:
byte Market[] = {0,1}; char* Zqdm[] = {"000001","600030"}; short ZqdmCount = 2; bool1 = TdxHq_GetSecurityQuotes(Market, Zqdm, ZqdmCount, Result, ErrInfo); if (!bool1) { cout << ErrInfo << endl; return 0; } cout << Result << endl;
如何改成golang的代码呢?!

共 10 个回复


xjjrocker

C的函数:

byte[] Market = { 0, 1, 1, 0 };
string[] Zqdm = { "000001", "600030", "600000", "000750" };

如何转换成go的定义啊?

# 0

xjjrocker

发现golang中,数组作为参数传递时, 需要被检查长度(或许有时候不报错,但必须带长度),如下方式比如 fun test_array(args [4]int){….}
数字4必须带上。

# 1

xjjrocker

char xMarket[] = {0,1, 0, 1 };
const char* Zqdm[] = { "000001", "600030", "000002", "601288" };
short ZqdmCount = 4;
bool1 = TdxHq_GetSecurityQuotes(xMarket, Zqdm, &ZqdmCount, Result, ErrInfo);

这段代码我用go实现如下

arrmarket := [2]byte{1,0}
arrzqdm := [2]string{"601166","000001"}
Hq_GetSecurityQuotes(arrmarket, arrzqdm, 2)

其中函数实现如下
func Hq_GetSecurityQuotes(Market [2]byte, Zqdm [2]string, Count int) {
...//省略
ret, _, err := Hq_GetSecurityQuotes.Call(uintptr(unsafe.Pointer(&Market)),
        uintptr(unsafe.Pointer(&Zqdm)), uintptr(unsafe.Pointer(&zqdmCount)),
        uintptr(unsafe.Pointer(&Result[0])), uintptr(unsafe.Pointer(&ErrInfo[0])))
...//省略
}

我发现传入参数只有一条有效,明明两条,但似乎只传入了一条,因为返回也只有一条数据,这里用&Market难道不对,请大侠指点。

# 3

jimmykuu

package main

func TdxHq_GetSecurityQuotes(market []byte, zqdm []string, count int) {
    // ...
}

func main() {
    market := []byte{0, 1}
    zqdm := []string{"601166", "000001"}
    count := 2

    TdxHq_GetSecurityQuotes(market, zqdm, count)
}
# 5

xjjrocker

报错如下
@jimmykuu

Exception 0xc0000005 0x0 0x2 0x57755daa
PC=0x57755daa

syscall.Syscall6(0x577529c0, 0x5, 0x116c4490, 0x116c44a0, 0x116c446c, 0x11822000, 0x117c8200, 0x0, 0x0, 0x40af9f, ...)
        C:/Go/src/runtime/syscall_windows.go:174 +0x51
syscall.(*Proc).Call(0x116c44c0, 0x116a4b40, 0x5, 0x5, 0x100, 0x100, 0x116c43ec, 0x0)
        C:/Go/src/syscall/dll_windows.go:155 +0x4ae
main.Hq_GetSecurityQuotes(0x116c4468, 0x2, 0x2, 0x116c4480, 0x2, 0x2, 0x2)
        C:/mygo/testTdxHq/testdll.go:367 +0x1d1
main.main()
        C:/mygo/testTdxHq/testdll.go:40 +0xc2
eax     0x1
ebx     0x116c4444
ecx     0x3197c226
edx     0x2
edi     0x116c446c
esi     0x3197c208
ebp     0x8fe54
esp     0x8f8b0
eip     0x57755daa
eflags  0x10297
cs      0x1b
fs      0x3b
gs      0x0
exit status 2
# 6

xjjrocker

自己来把解决思路放上来,给后来者参考吧:

//C
myfunc(byte Market[], char* Zqdm[], short& Count)

这个函数用golang重新定义

//go
func myfunc(Market []byte, Zqdm []string, Count int) bool {
    ...
}

难点也是重点的是,原来的char* Zqdm[]虽然在go里面用了Zqdm []string,但不代表这样就可以了,其实应注意原来的数组是指针数组,所以我们变成字符串数组后,但真正的调用的地方这个字符串数组要转成指针数组([] *byte)。

# 8

liusiming

您好,你是再调通达信的dll吗,可以加qq请教一下吗

# 9