将字符串传递给带有cgo的win32函数

huangapple go评论73阅读模式
英文:

passing string to win32 function with cgo

问题

我尝试了这个:

name := C.CString("vds")
C.OpenService(scm, (name), C.DWORD(C.SC_MANAGER_ALL_ACCESS))

但它无法编译通过:

.\test.go:28: cannot use name (type *C.char) as type *C.CHAR in argument to _Cfunc_OpenService

我尝试查找类似的例子(例如sqlite),但它们似乎使用了相同的习惯用法,但可以编译通过。

英文:

I try this

name := C.CString("vds")
C.OpenService(scm, (name), C.DWORD(C.SC_MANAGER_ALL_ACCESS))

but it wont compile

.\test.go:28: cannot use name (type *C.char) as type *C.CHAR in argument to _Cfunc_OpenService

I tried looking for similar things (sqlite for example) but they seem to use this same idiom, but it compiles

答案1

得分: 0

尝试显式转换它:

name := C.CString("vds")
C.OpenService(scm, (*C.CHAR)(unsafe.Pointer(name)), C.DWORD(C.SC_MANAGER_ALL_ACCESS))
英文:

Try explicitly casting it:

name := C.CString("vds")
C.OpenService(scm, (*C.CHAR)(unsafe.Pointer(name)), C>DWORD(C.SC_MANAGER_ALL_ACCESS))

huangapple
  • 本文由 发表于 2016年2月23日 09:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/35567028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定