buddy148
buddy148

Reputation: 167

Golang CGO unable to use converted string

I'm attempting to set the title of the Windows command prompt using CGO and the windows c header:

// #include <windows.h>
import "C"
import "unsafe"

func Title(title string) {
  ctitle := C.CString(title)
  defer C.free(unsafe.Pointer(ctitle))
  C.SetConsoleTitle(ctitle)
}

But at compile time, the following error occurs:

cannot use ctitle (type *C.char) as type *C.CHAR in argument to _Cfunc_SetConsoleTitle

It would seem that C.SetConsoleTitle(ctitle) is expecting a string of type *C.CHAR but C.CString(title) is returning *C.char

How should I go about converting the string to the expected type?

Upvotes: 2

Views: 1545

Answers (1)

buddy148
buddy148

Reputation: 167

I've found a solution, You're able to cast the pointer to an *C.CHAR:

// #include <windows.h>
import "C"
import "unsafe"

func Title(title string) {
  ctitle := unsafe.Pointer(C.CString(title))
  defer C.free(ctitle)
  C.SetConsoleTitle((*C.CHAR)(ctitle))
}

Upvotes: 1

Related Questions