gpt4 book ai didi

go - 从 go 更改 *C.char 的正确方法

转载 作者:IT王子 更新时间:2023-10-29 01:40:44 24 4
gpt4 key购买 nike

我是 go 和 cgo 的新手,在浏览互联网一段时间后,我还没有想出一个好的和快速的方法来从 go 中更改 char*。

从 go& 改变 *C.char 最快的方法是什么

这是我的代码和我尝试更改字符串的尝试(它不起作用)

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))
}

输出

P

P

我也是 C 的新手,我正在这样做,因为我找不到一个库来做这件事

最佳答案

例如,将 gData 用作底层 cData C char 数组的 Go byte slice 引用。

package main

import (
"fmt"
"unsafe"
)

/*
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int printData(unsigned char *data) {
return printf("cData: %lu \"%s\"\n", (long unsigned int)strlen(data), data);
}
*/
import "C"

func main() {
// Allocate C data buffer.
width, height := 8, 2
lenData := width * height
// add string terminating null byte
cData := (*C.uchar)(C.calloc(C.size_t(lenData+1), C.sizeof_uchar))

// When no longer in use, free C allocations.
defer C.free(unsafe.Pointer(cData))

// Go slice reference to C data buffer,
// minus string terminating null byte
gData := (*[1 << 30]byte)(unsafe.Pointer(cData))[:lenData:lenData]

// Write and read cData via gData.
for i := range gData {
gData[i] = '.'
}
copy(gData[0:], "Data")
gData[len(gData)-1] = 'X'
fmt.Printf("gData: %d %q\n", len(gData), gData)
C.printData(cData)
}

输出:

gData: 16 "Data...........X"
cData: 16 "Data...........X"

引用:Command cgo

关于go - 从 go 更改 *C.char 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53120657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com