I am new with go and cgo and after browsing the internet for some time I have not figured out a good and fast way to change a char* from go.
What is the fastest way to change *C.char from go&
Here is my code and my attempt to change the string(It does not work)
package asciiEngine
// #include <windows.h>
import "C"
type Screen struct {
Width, Height int
Length C.ulong
Data *C.char
GoData string
HConsole C.HANDLE
BytesWritten C.DWORD
Start C.COORD
}
func (s Screen) Draw() {
C.WriteConsoleOutputCharacter(s.HConsole, s.Data, s.Length, s.Start, &s.BytesWritten)
}
func CreateScreen(width, height int) Screen {
screen := Screen{
Width: width,
Height: height,
Length: C.ulong(width * height),
Data: (*C.char)(C.malloc(C.ulonglong(width * height))),
HConsole: C.CreateConsoleScreenBuffer(C.GENERIC_READ|C.GENERIC_WRITE, 0, nil, C.CONSOLE_TEXTMODE_BUFFER, nil),
BytesWritten: 0,
}
screen.GoData = C.GoString(screen.Data) // my attempt to get a reference to the C string
//C.SetConsoleActiveScreenBuffer(screen.HConsole)
return screen
}
main.go:
package main
// #include "stdio.h"
// void print(char* data) {
// printf(data);
// }
import "C"
import (
"fmt"
"github.com/demantar/ascii-engine"
)
func main() {
screen := asciiEngine.CreateScreen(100, 50)
C.print((*C.char)(screen.Data))
fmt.Println()
screen.GoData = "askdssdfselkkskdkflsekfjdkjfksjeflsdkfjjekdjflskasdfkksdjjekdskdfjkskd"
C.print((*C.char)(screen.Data))
}
output
P
P
I'm also pretty new to C and am doing this because I could not find a library to do this