CreateProcessW 失败 (ACESS_DENIED)
我目前将应用程序转换为使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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的第一个参数
lpApplicationName
CreateProcess 函数通常用作NULL
,第二个参数lpCommandLine
应包含以要启动的程序名称开头的命令行。只需通过 CreateProcessW 调用切换当前使用的第一个和第二个参数即可。
The first parameter
lpApplicationName
of the CreateProcess function will be used asNULL
typically and the second parameterlpCommandLine
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.您输入的完整路径是什么?
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.