2

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?

2
  • try to change C.SetConsoleTitle(ctitle) to C.SetConsoleTitle(title.c_str()) Commented Jan 22, 2016 at 7:47
  • @LPs That doesn't seem possible title.c_str undefined (type string has no field or method c_str) Thanks anyway :) Commented Jan 22, 2016 at 7:49

1 Answer 1

1

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))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.