I am trying to write DLL in go. I want it to execute some stuff when the 
DLL is attached to a process.

I thought init() will be the equivalent of onattach but It seems I am wrong.

I created this as a proof of concept:
```
package main

import "C"
import (
 "syscall"

 "golang.org/x/sys/windows"
)

//export RunMe
func RunMe() {

      windows.MessageBox(windows.HWND(0), 
syscall.StringToUTF16Ptr("RunMe"), syscall.StringToUTF16Ptr("RunMe"), 
windows.MB_OK)
}

func init() {
      windows.MessageBox(windows.HWND(0), syscall.StringToUTF16Ptr("DLL 
Loaded"), syscall.StringToUTF16Ptr("DLL Load"), windows.MB_OK)
}

func main() {}
```

I compile on Linux with:
```
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build 
-a -o ~/Desktop/dropamd64.dll -buildmode=c-shared cmd/dll64bit/main.go
```

I transfer on my Windows machine then here is the amazing result:
*rundll32.exe Z:\dropamd64.dll ----->  Nothing happens*
*rundll32.exe Z:\dropamd64.dll,RunMe  ------> Runs RunMe() and init() and I 
get 2 MessageBox*

My question is, how to get a DLL that runs stuff only when process is 
attached ?

There not a lot of official Golang doc about DLL (some few people write 
here and there some tutorials that are not always right and correct ...) So 
if you have some trustworthy doc please share.




-- 
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 visit 
https://groups.google.com/d/msgid/golang-nuts/2e12920a-e5b0-435c-b9c0-4a43eefbbeecn%40googlegroups.com.

Reply via email to