Skip to content

Since there are no examples, i made this. Perhaps this will be useful to someone.

Notifications You must be signed in to change notification settings

k4ties/go-dll-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Minimal example (without GUI)

package main

import "C"
import "fmt"

func init(){
  // injection func
  fmt.Println("Injected")
}

func main(){}

To build the DLL you need to make function init()

Everything in this function will be executed after DLL injection.

Minimal example with GUI

I'm using library github.com/AllenDang/giu, basically easier imgui bindings for Go.

package main

import "C"
import "github.com/AllenDang/giu"

func init(){
	wnd := giu.NewMasterWindow("Hello world", 400, 200, giu.MasterWindowFlagsNotResizable)
	wnd.Run(func(){
		// some code
	})
}

func main(){}

You can also run this script without building DLL, for example if you want to test it, but build and inject dll is too long

Full example with GUI and button

Using syscall and windows libraries for requesting message box.

package main

import "C"
import (
    "golang.org/x/sys/windows"
    "github.com/AllenDang/giu"
    "syscall"
)

func init(){
	wnd := giu.NewMasterWindow("Hello world", 200, 50, giu.MasterWindowFlagsNotResizable)
	wnd.Run(func(){
		giu.SingleWindow().Layout(
			giu.Button("Example Button").OnClick(func(){
				requestMessage("Hello from DLL Gui", "Title")
			}),
		)
	})
}

func requestMessage(message, title string) {
	windows.MessageBox(
		windows.HWND(0),
		syscall.StringToUTF16Ptr(message),
		syscall.StringToUTF16Ptr(title),
		windows.MB_OK,
	)
}

func main(){}

More examples you can find here.

Building

You can build DLL only on Windows, which is obvious

Format: go build -o <filename> -buildmode=c-shared <filepath>

Example: go build -o dllmain.dll -buildmode=c-shared main.go

About

Since there are no examples, i made this. Perhaps this will be useful to someone.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages