On Sat, May 9, 2020 at 12:56 PM Gert <gert.cuyk...@gmail.com> wrote:
>
> This is the closest I get, I am out of ideas. Don't know why it runs but 
> doesn't update the string.

I don't know what you are really trying to do, but I want to be clear
that this code is not supported.  The supported way to call from Go to
C is to use cgo.  See https://golang.org/cmd/cgo and
https://blog.golang.org/cgo.

One big problem I see in your code is that you are assuming that Go
and C use the same ABI.  That is, that the arguments passed to the Go
function will be seen as the arguments passed to the C function.  That
is not the case.  When using the gc compiler, Go and C pass arguments
in different locations.

Ian



> go run .
> test [116 101 115 116 0 0 0 0 0 0] 4 0 0
>
> #include <stdio.h>
> #include <string.h>
>
> int hello(char *s) {
>  char c[80];
>  strcpy (c, s);
>  sprintf(s, "hello %s", c);
>  printf("------\n");
>  return 0;
> }
>
> #include "textflag.h"
>
> TEXT ·hello_trampoline(SB),NOSPLIT,$0-0
>  JMP hello_c(SB)
>
> package main
>
> import (
>  "fmt"
>  "unsafe"
>
>  "golang.org/x/sys/unix"
> )
>
> //go:linkname hello_t hello_c
> //go:cgo_import_dynamic hello_c hello "./c/hello.dylib"
>
> //go:nosplit
> func hello_t(s unsafe.Pointer) (uintptr, uintptr, unix.Errno) {
>  return unix.Syscall(funcPC(hello_trampoline), uintptr(s), 0, 0)
> }
>
> func hello_trampoline()
>
> //go:nosplit
> func funcPC(f func()) uintptr {
>  return **(**uintptr)(unsafe.Pointer(&f))
> }
>
> func main() {
>  b := make([]byte, 10, 10)
>  copy(b, []byte("test"))
>  r1, r2, err := hello_t(unsafe.Pointer(&b[0]))
>  fmt.Printf("%s %v %d %d %d\n", b, b, r1, r2, err)
> }
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/68003925-1a44-4b7c-8cec-21849784bee5%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcU3zDA30YrFvh0GrNtqEie2hi0RBVfbHEB_4BpS%2BOvQFg%40mail.gmail.com.

Reply via email to