I want to execute HelloWorldConsole.exe with an shipped Mono Framework through Go.
So I want to call mono-2.0-sgen.dll funtion "mono_main" to execute the exe. mono-2.0-sgen.dll is a PE32 executable for MS Windows, see .
But I get the error "not a valid Win32 application"
_ = os.Setenv("MONO_PATH", `\mono\lib\mono\4.5;C:\DEV\HelloWorldConsole\HelloWorldConsole\bin\Debug`)
_ = os.Setenv("MONO_CFG_DIR", `"C:\Program Files (x86)\Mono\etc"`)
_ = os.Setenv("MONO_CONFIG", `"C:\Program Files (x86)\Mono\etc\mono\config"`)
mono := `C:\Program Files (x86)\Mono\bin\mono-2.0-sgen.dll`
app := `C:\DEV\HelloWorldConsole\HelloWorldConsole\bin\Debug\HelloWorldConsole.exe`
fmt.Println("Execute")
var mod = syscall.NewLazyDLL(mono)
var proc = mod.NewProc("mono_main")
ret, _, _ := proc.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("mono.exe"))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(app))) )
Same behavior with windows.NewLazySystemDLL
dll := windows.NewLazySystemDLL(mono)
lazyProc := dll.NewProc("mono_main")
lazyProc.Call()
Function Name
Full Error:
panic: Failed to load C:\Program Files (x86)\Mono\bin\mono-2.0-sgen.dll: %1 is **not a valid Win32 application**.
The error %1 is not a valid Win32 application
is a discription of Win32 error code from windows itself.
The Win32 error code itself is 0x000000C1 ERROR_BAD_EXE_FORMAT
, see docs.microsoft.com .
BAD_EXE_FORMAT
means you call a x86 dll within a x64 process. You must use the x86 variant of Go, eg go1.12.1.windows-386.zip from https://golang.org/dl/ .
Now must change the GOROOT and PATH to the extracted bin folder and then you are ready to go.
When you call mono_main
you need to consume the this function in the right way. If you take an look at the implementation you see that the signature is int mono_main (int argc, char* argv[]);
. argc and argv is a widly used pattern, see here .
func main() {
_ = os.Setenv("MONO_PATH", `C:\DEV\HelloWorldConsole\HelloWorldConsole\bin\x86\Debug\mono\lib\mono\4.5;C:\DEV\HelloWorldConsole\HelloWorldConsole\bin\x86\Debug\`)
_ = os.Setenv("MONO_CFG_DIR", `C:\DEV\HelloWorhpm.goldConsole\HelloWorldConsole\bin\x86\Debug\mono\etc`)
_ = os.Setenv("MONO_CONFIG", `C:\DEV\HelloWorldConsole\HelloWorldConsole\bin\x86\Debug\mono\etc\mono\config`)
_ = os.Chdir(`C:\DEV\HelloWorldConsole\HelloWorldConsole\bin\x86\Debug\`)
mono := `C:\DEV\HelloWorldConsole\HelloWorldConsole\bin\x86\Debug\mono\bin\mono-2.0-sgen.dll`
// https://github.com/mono/mono/blob/c5b88ec4f323f2bdb7c7d0a595ece28dae66579c/mcs/tools/mkbundle/template_main.c#L1
dll := windows.NewLazySystemDLL(mono)
lazyProc := dll.NewProc("mono_main")
dotNetAssembly := []byte(`HelloWorldConsole.exe`)
var argumentData [260]byte
ptr := unsafe.Pointer(&argumentData)
copy(argumentData[:], dotNetAssembly)
args := [2]uintptr{0, uintptr(ptr)}
_, _, _ = lazyProc.Call(2, uintptr(unsafe.Pointer(&args)))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.