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)))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?