Can a dll made in C # be used in a golang application

I created a base class that adds two numbers in C #. I built it in dll, but trying to name it in golang, I was unsuccessful. Firstly, is this currently possible in the Golang? If so, can anyone provide an example of how to do this?

Edit: I included the last attempt I made with this. C # dll is just a method that adds two numbers that are passed in.

package main import ( "fmt" "syscall" ) func main() { var mod = syscall.NewLazyDLL("MathForGo.dll") var proc = mod.NewProc("Add"); proc.Call(2,3); fmt.Printf("%v",proc) } 
+7
go
source share
2 answers

Yes it is possible: https://github.com/golang/go/wiki/WindowsDLLs

(Copy here in case the link dies)

There are several ways to call the "C" code from within Go.

The first method: dynamically load the DLL, and then call the method on it. You can call the method via "syscallXX" (XX is the number of parameters, but if it has few of them, for example, if you need seven parameters, then syscall9 will work anyway, you just say that the number of arguments is 7). This method also works with shared Linux libraries if you are targeting Linux:

Example program that calls the Windows DLL from Go:

 package main import ( "fmt" "syscall" "unsafe" ) func abort(funcname string, err error) { panic(fmt.Sprintf("%s failed: %v", funcname, err)) } var ( kernel32, _ = syscall.LoadLibrary("kernel32.dll") getModuleHandle, _ = syscall.GetProcAddress(kernel32, "GetModuleHandleW") user32, _ = syscall.LoadLibrary("user32.dll") messageBox, _ = syscall.GetProcAddress(user32, "MessageBoxW") ) const ( MB_OK = 0x00000000 MB_OKCANCEL = 0x00000001 MB_ABORTRETRYIGNORE = 0x00000002 MB_YESNOCANCEL = 0x00000003 MB_YESNO = 0x00000004 MB_RETRYCANCEL = 0x00000005 MB_CANCELTRYCONTINUE = 0x00000006 MB_ICONHAND = 0x00000010 MB_ICONQUESTION = 0x00000020 MB_ICONEXCLAMATION = 0x00000030 MB_ICONASTERISK = 0x00000040 MB_USERICON = 0x00000080 MB_ICONWARNING = MB_ICONEXCLAMATION MB_ICONERROR = MB_ICONHAND MB_ICONINFORMATION = MB_ICONASTERISK MB_ICONSTOP = MB_ICONHAND MB_DEFBUTTON1 = 0x00000000 MB_DEFBUTTON2 = 0x00000100 MB_DEFBUTTON3 = 0x00000200 MB_DEFBUTTON4 = 0x00000300 ) func MessageBox(caption, text string, style uintptr) (result int) { var nargs uintptr = 4 ret, _, callErr := syscall.Syscall9(uintptr(messageBox), nargs, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), style, 0, 0, 0, 0, 0) if callErr != 0 { abort("Call MessageBox", callErr) } result = int(ret) return } func GetModuleHandle() (handle uintptr) { var nargs uintptr = 0 if ret, _, callErr := syscall.Syscall(uintptr(getModuleHandle), nargs, 0, 0, 0); callErr != 0 { abort("Call GetModuleHandle", callErr) } else { handle = ret } return } func main() { defer syscall.FreeLibrary(kernel32) defer syscall.FreeLibrary(user32) fmt.Printf("Return: %d\n", MessageBox("Done Title", "This test is Done.", MB_YESNOCANCEL)) } func init() { fmt.Print("Starting Up\n") } 

The second way is through syscall.NewProc (etc.) instead of syscall.GetProcAddress. These are basically some of the helper methods you saw above, and are only available on Windows: http://golang.org/src/pkg/syscall/dll_windows.go

 package main import ( "fmt" "syscall" "unsafe" ) func main() { var mod = syscall.NewLazyDLL("user32.dll") var proc = mod.NewProc("MessageBoxW") var MB_YESNOCANCEL = 0x00000003 ret, _, _ := proc.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Done Title"))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("This test is Done."))), uintptr(MB_YESNOCANCEL)) fmt.Printf("Return: %d\n", ret) } 

A third way would be to invoke libraries mainly by "linking" against the library using the "cgo" method (this works on Linux and Windows):

This method will look something like this.

 import ("C") ... C.MessageBoxW(...) 

See cgo for more details.

+7
source share

Github has a project whose purpose is this.

https://github.com/matiasinsaurralde/go-dotnet

C # assemblies do not match C or C ++ and will not load using syscall as we would like.

+3
source share

All Articles