My first reaction is, do you *really* want to call a Go DLL from a Go main 
program? It seems to me like you will have two active copies of the Go 
runtime with their own garbage collectors etc.

Go "plugins" might be closer to what you need:
https://medium.com/learning-the-go-programming-language/writing-modular-go-programs-with-plugins-ec46381ee1a9

But there are a number of caveats:
https://www.reddit.com/r/golang/comments/b6h8qq/is_anyone_actually_using_go_plugins/ejkxd2k/?utm_source=reddit&utm_medium=web2x&context=3

Also consider whether you'd be better off with two separate processes 
communicating using gRPC or similar. Hashicorp have published a go-plugin 
library which takes this approach:
https://github.com/hashicorp/go-plugin

On Thursday, 29 September 2022 at 10:34:38 UTC+1 pe...@wonderland.org wrote:

> Oh, hang on, please ignore my last message. It's that was because the 
> *caller* was defined that way - it's NOT a Go thing. Oops, my bad.
>
> Peter
>
> On Thursday, 29 September 2022 at 10:33:23 UTC+1 Peter Galbavy wrote:
>
>> On Linux at least - I have not tried building or using a Windows DLL, you 
>> have to accept C-style args and process them in the exported function 
>> signature:
>>
>> e.g.
>>
>> //export SendMail
>> func SendMail(n C.int, args **C.char) C.int {
>>     conf := parseArgs(n, args)
>> ...
>>
>> Here my parseArgs() func loops over the args and puts them in a map - 
>> which is what I want, but your requirement will be different.
>>
>> Even if you are calling the function from Go code, I believe it still 
>> goes through the C ABI. I just followed https://pkg.go.dev/cmd/cgo
>>
>> Peter
>>
>>
>> On Thursday, 29 September 2022 at 09:58:02 UTC+1 squadglad...@gmail.com 
>> wrote:
>>
>>> Hi Everyone,
>>>
>>> I'm trying to create go DLL for the below program using "go build 
>>> -buildmode=c-shared -o calc.so calc.go"
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *package mainimport "C"func main() {}//export SayHellofunc SayHello(name 
>>> string) {fmt.Printf("Go says: Hello, %s!\n", name)}//export Addfunc 
>>> Add(num0, num1 int) int {return num0 + num1}*
>>>
>>> *-----------------------------------------------------------------------------------------------------*
>>> I have written the below logic to access DLL methods,
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *import "C"import (   "fmt"   "syscall"   "unsafe")var (   Library     
>>>  syscall.Handle   IsInitialize = false   data         = "World")func 
>>> Loaddll() {   if Library == 0 {      var error_val error      
>>> fmt.Printf("creating handle of c-share Wrapper\n")      Library, _ = 
>>> syscall.LoadLibrary("calc.dll")      if error_val != nil {        
>>>  fmt.Println(fmt.Sprintf("Error occured while loading dll - %s", 
>>> error_val.Error()))      }      getDetails()   }}func getDetails() {  
>>>  key_uintptr := getUintPtrOfString(data)   helloMethod := 
>>> getProc("SayHello")   get_secret_result, _, _ := 
>>> syscall.SyscallN(uintptr(helloMethod), key_uintptr)   value := 
>>> C.GoString((*C.char)(unsafe.Pointer(get_secret_result)))   if 
>>> get_secret_result != 0 {      fmt.Println(fmt.Sprintf("Get code has given 
>>> nonzero result\n %v", value))   }}func getProc(funcname string) (result 
>>> uintptr) {   result, error_val := syscall.GetProcAddress(Library, 
>>> funcname)   if error_val != nil {      fmt.Println(fmt.Sprintf("Error while 
>>> getting proc %s", error_val.Error()))   }   return result}func 
>>> getUintPtrOfString(s string) uintptr {   byte_s := append([]byte(s), 0)  
>>>  return uintptr(unsafe.Pointer(&byte_s[0]))}*
>>>
>>> *----------------------------------------------------------------------------------------------------*
>>> I'm able to access the DLL method(Add) when we pass int as argument, but 
>>> I'm getting errors while passing arguments as a string (Sayhello)..Here we 
>>> are expecting the result as *Go says: Hello,<some string>*
>>>
>>

-- 
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/6d82ea6c-88e1-4272-91c8-717f81248a0fn%40googlegroups.com.

Reply via email to