简体   繁体   中英

Unexpected Win32Exception on getMainModule

I have the following piece of code.

    string currentDir = Directory.GetCurrentDirectory();
    Process mcProcess = new Process();
    mcProcess.StartInfo.WorkingDirectory = currentDir;
    //mcProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    mcProcess.StartInfo.UseShellExecute = true;
    mcProcess.StartInfo.FileName = "java.exe";
    mcProcess.StartInfo.Arguments = @"-jar Minecraft\forge.jar -Xms2G -Xmx2G";
    //mcProcess.StartInfo.RedirectStandardOutput = true;
    mcProcess.Start();
    //MetroMessageBox.Show(this, mcProcess.StandardOutput.ReadToEnd());
    //mcProcess.WaitForExit();

This throws a Win32Exception when the process starts, i've checked the target architecture and it's x64. Java path is properly set, if I run the command on console, it works fine. However this won't work.

Message: only part of a readprocessmemory or writeprocessmemory request was completed(Translated so it might not be 100% accurate)

Stacktrace:

'mcProcess.MainModule' threw an exception of type 'System.ComponentModel.Win32Exception'
   at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
   at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
   at System.Diagnostics.Process.get_MainModule()

The native error code is 299 AKA ERROR_OPLOCK_NOT_GRANTED

EDIT: This seems to be a permission issue, since runing the compiled executable as admin works fine. How can I make it work for non-elevated users?

As expected it was a permissions issue.

The way i've solved it is by getting the JAVA_HOME variable from registry and appending "\\bin\\java.exe" to it to get the full path.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM