Calling DLL exported function - Issue with arguments

68 views
Skip to first unread message

rudeus greyrat

unread,
Mar 18, 2025, 7:20:02 PMMar 18
to golang-nuts
I have a DLL compiled with CGO. It exports 3 functions:

```
//export Run1
func Run(text string) (stdout []byte, stderr []byte, err error) {
fmt.Println(text)
return nil, nil, nil
}

//export Run2
func Run2() (stdout []byte, stderr []byte, err error) {
       fmt.Println("Heyyyyy")
        fmt.Println(111111)
return nil, nil, nil
}

//export Run3
func Run3(a int) (stdout []byte, stderr []byte, err error) {
        fmt.Println(a)
return nil, nil, nil
}
```


In a seperate Go file I import the DLL and try to call the exported function.
For Run2 everything works

```
s = uinptr(ADDRESS_RUN2)
syscall.SyscallN(s)
```

It prints Heyyyyy and 1111111 in my output


For Run3, I am not sure how to pass the argument to SyscallN.
```
  
    s = uinptr(ADDRESS_RUN3)
    a := 999999999
    syscall.SyscallN(s, uintptr(unsafe.Pointer(&a)))  ----> Fail (random Number printed)

    a := 999999999
    syscall.SyscallN(s, uintptr(unsafe.Pointer(a)))  -----> Fail (random Number printed)

```

For Run1, it get worse as sometimes I get C0000005 error as I use the string elsewhere:
```
        s = uinptr(ADDRESS_RUN1)

cmdBytes := []byte(torun)
cmdBytes = append(cmdBytes, 0)
ptr := unsafe.Pointer(&cmdBytes[0])
        syscall.SyscallN(d, uintptr(ptr))
```

Any help please ??

rudeus greyrat

unread,
Mar 18, 2025, 8:46:58 PMMar 18
to golang-nuts
Ok, seem to have got it,

I need to change function signature in the DLL to be pointers instead of go objects.
Ex.
```
//export Run1
func Run1(ptext *byte) (stdout []byte, stderr []byte, err error) {
          text := windows.BytePtrToString(ptext)
         fmt.Println(text)
return nil, nil, nil
}
```
Weird but ok...
Reply all
Reply to author
Forward
0 new messages