CreateProcessW 失败 (ACESS_DENIED)

发布于 2024-10-14 22:13:36 字数 1667 浏览 6 评论 0原文

我目前将应用程序转换为使用 CreateProcessW() 而不是 Runtime.exec(),因为我需要它提供的信息。但是,对 CreateProcessW() 的任何调用都会失败,并显示错误代码 5(访问被拒绝)。我一直无法找出为什么会发生这种情况,因为 Runtime.exec() 在相​​同情况下运行良好。

我的错误可能出现在以下代码片段之一、方法调用和 jna 接口中。

public ProcessInfo createProcess(String dir, String name){
            ProcessInfo pi = new ProcessInfo();
            StartupInfo start = new StartupInfo();
            mem.CreateProcessW(new WString(name),
                    null,
                    null,
                    null,
                    false,
                    0,
                    null,
                    new WString(dir),
                    start.getPointer(),
                    pi.getPointer());
            return pi;
        }

我对 CreateProcessW 的定义

boolean CreateProcessW(WString apname,
                    char[] comline,
                    Pointer p,
                    Pointer p2,
                    boolean inheritHandles,
                    int createFlags,
                    String environment,
                    WString directory,
                    Pointer startinf,
                    Pointer processInfo);

附加信息:

  • Runtime.exec() 使用给定的字符串成功 设置
  • StartupInfo 的大小
  • 使用的测试环境: WinXP SP3 和 Netbeans 6.9.1

使用的示例参数:

  • 名称: moviemk.exe
  • 目录: C:\Programme\Movie Maker\

还使用不同的路径进行了测试,所以不是空格问题

谢谢

更新:

事实证明,错误是由于我在检查工作目录和exe路径后调用代码切换而引起的。由于导致访问被拒绝,我实际上认为它至少找到了 exe。我将添加一个 IllegalArgumentException 来解决这个问题。

由于我的 exe 相对于工作目录存在额外的错误,因此我将接受该答案。感谢大家的帮助。

I currently convert an application to use CreateProcessW() instead of Runtime.exec() as I need the information it provides. However any call to CreateProcessW() fails with the error code 5 (ACCESS DENIED). I have been unable to find out why this happens as Runtime.exec() runs fine in the same case.

My error could be in one of the following code snippets, the method call and the jna interface.

public ProcessInfo createProcess(String dir, String name){
            ProcessInfo pi = new ProcessInfo();
            StartupInfo start = new StartupInfo();
            mem.CreateProcessW(new WString(name),
                    null,
                    null,
                    null,
                    false,
                    0,
                    null,
                    new WString(dir),
                    start.getPointer(),
                    pi.getPointer());
            return pi;
        }

My definition of CreateProcessW

boolean CreateProcessW(WString apname,
                    char[] comline,
                    Pointer p,
                    Pointer p2,
                    boolean inheritHandles,
                    int createFlags,
                    String environment,
                    WString directory,
                    Pointer startinf,
                    Pointer processInfo);

Additional Info:

  • Runtime.exec() succeeds with the given Strings
  • The size of StartupInfo is set
  • Testenvironment used: WinXP SP3 and Netbeans 6.9.1

Example parameters used:

  • Name: moviemk.exe
  • Dir: C:\Programme\Movie Maker\

Also tested with different paths, so not a whitespace problem

Thanks

Update:

As it turns out the error was caused by my calling code switching around working dir and exe path after I checked them. Because of the resulting access denied I actually thought that it at least found the exe. I will add an IllegalArgumentException to take care of that problem.

Since I had the additional error with the exe being relative to the working dir I will accept that answer. Thanks to all for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太傻旳人生 2024-10-21 22:13:36

CreateProcessW 的第一个参数必须是完整路径或相对于当前目录的路径。它不能是相对于工作目录参数的路径,这似乎是您期望它执行的操作。

尝试传递 C:\Programme\Movie Maker\moviemk.exe 作为名称参数

CreateProcessW's first parameter has to be either a full path or a path relative to the current directory. It can't be a path relative to the working directory parameter, which seems like what you're expecting it to do.

Try passing C:\Programme\Movie Maker\moviemk.exe as the name parameter

塔塔猫 2024-10-21 22:13:36

的第一个参数 lpApplicationName CreateProcess 函数通常用作NULL,第二个参数lpCommandLine 应包含以要启动的程序名称开头的命令行。

只需通过 CreateProcessW 调用切换当前使用的第一个和第二个参数即可。

The first parameter lpApplicationName of the CreateProcess function will be used as NULL typically and the second parameter lpCommandLine should contain the command line starting with the program name which you want to start.

Just fry to switch the first and the second parameters which you use currently by the CreateProcessW call.

深爱不及久伴 2024-10-21 22:13:36

您输入的完整路径是什么? Runtime.exec 可能会在内部引用该参数,您可能会遇到这种情况:

http://support.microsoft.com/kb/179147

也许路径存在前缀并导致它尝试执行文件夹或其他文件?

尝试在整个路径周围加上引号,看看是否有帮助。

What is the full path you are entering? Runtime.exec might be quoting the argument internally, and you could be running into this situation:

http://support.microsoft.com/kb/179147

Maybe there is a prefix to the path that exists and is causing it to try to execute a folder or other file?

Try putting quotes around the entire path and see if that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文