Search code examples
cgocgo

Convert C string to Go string without CGO


I'm using the Go Windows syscall libraries to get data out of a function in a DLL. This all works great, but I can't figure out a way to convert a LPCTSTR (pointer to C String) into a proper Go string without using CGO.

I'd like to avoid CGO if at all possible, because the two options for CGO code on Windows (cross-compiling, and installing gcc on windows) are still fairly complex.


Solution

  • If you have an 8 bit string, you can convert the LPCTSTR pointer to a []byte of the proper size, and copy it to a new string or slice.

    a := (*[1 << 30-1]byte)(unsafe.Pointer(lpctstr))
    size := bytes.IndexByte(a[:], 0)
    // if you just want a string
    // goString := string(a[:size:size])
    
    // if you want a slice pointing to the original memory location without a copy
    // goBytes := a[:size:size]
    
    goBytes := make([]byte, size)
    copy(goBytes, a)
    

    If the LPCTSTR points to an LPCWSTR which contains 16bit unicode characters, you can convert that with the utf16 package.

    a := (*[1 << 30-1]uint16)(unsafe.Pointer(lpctstr))
    size := 0
    for ; size < len(a); size++ {
        if a[size] == uint16(0) {
            break
        }
    }   
    runes := utf16.Decode(a[:size:size])
    goString := string(runes)