dongxie559554 2019-03-22 20:04
浏览 212
已采纳

用syscall调用mono-2.0-sgen.dll收到“不是有效的Win32应用程序”

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

mono-2.0-sgen.dll

Full Error:

panic: Failed to load C:\Program Files (x86)\Mono\bin\mono-2.0-sgen.dll: %1 is **not a valid Win32 application**.
  • 写回答

1条回答 默认 最新

  • duanhan9334 2019-03-24 18:39
    关注

    %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_FORMATmeans you call a x86 dll within a x64 process. You must use the x86 variant of Go, e.g. 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.

    Type of Arguments

    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.

    Working Sample

    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 argummentData [260]byte
        ptr := unsafe.Pointer(&argummentData)
    
        copy(argummentData[:], dotNetAssembly)
    
        args := [2]uintptr{0, uintptr(ptr)}
    
        _, _, _ = lazyProc.Call(2, uintptr(unsafe.Pointer(&args)))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料