Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将_spawnvpe()与自定义路径值一起使用?_C_Windows_Spawn - Fatal编程技术网

C 如何将_spawnvpe()与自定义路径值一起使用?

C 如何将_spawnvpe()与自定义路径值一起使用?,c,windows,spawn,C,Windows,Spawn,我问了一个相关的问题(http://stackoverflow.com/questions/10969488/why-does-windows-spawn-process-sometimes-trigger-error-status-sxs-assembly-not-f)但我担心这个问题的复杂性会把它弄糊涂,所以,这里有一个非常简单的版本: 下面是一个调用_spawnpe的示例,手动传递路径值 它不起作用。它会出错,不会运行记事本 更改为_spawnv或不传递路径值可使其工作。然而,_puten

我问了一个相关的问题(http://stackoverflow.com/questions/10969488/why-does-windows-spawn-process-sometimes-trigger-error-status-sxs-assembly-not-f)但我担心这个问题的复杂性会把它弄糊涂,所以,这里有一个非常简单的版本:

下面是一个调用_spawnpe的示例,手动传递路径值

它不起作用。它会出错,不会运行记事本

更改为_spawnv或不传递路径值可使其工作。然而,_putenv的文档清楚地说明了env值的格式是KEY=value

我如何让它工作

请具体说明,并提供以下代码的差异或完整副本,包括修复

#include <stdio.h>
#include <windows.h>
#include <process.h>
#include <errno.h>

int main(int argc, char *argv[]) {

  char *path_value;
  char buffer[4000];
  const char *env[2];
  const char *args[1];
  char *command;
  int result;
  intptr_t procHandle;

  path_value = getenv("PATH");
  sprintf(buffer, "PATH=%s", path_value);
  env[0] = buffer;
  env[1] = NULL;

  args[0] = NULL;

  int offset = 0;
  while (env[offset] != NULL) {
    printf("env %d: %s\n", offset, env[offset]);
    ++offset;
  }

  offset = 0;
  while (args[offset] != NULL) {
    printf("arg %d: %s\n", offset, args[offset]);
    ++offset;
  }

  command = "C:\\windows\\system32\\notepad.exe";

  procHandle = _spawnvpe(_P_NOWAIT, command, args, NULL);
  if (procHandle == -1) {
    printf("Failed to invoke command: %s\n", strerror(errno));
    exit(1);
  }

  _cwait(&result, procHandle, 0);
  if (result != 0)
    printf("Command exited with error code %d\n", result);
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
char*path_值;
字符缓冲区[4000];
const char*env[2];
常量字符*args[1];
char*命令;
int结果;
intptr_t程序句柄;
路径_值=getenv(“路径”);
sprintf(缓冲区,“路径=%s”,路径值);
env[0]=缓冲区;
env[1]=NULL;
args[0]=NULL;
整数偏移=0;
while(环境[偏移量]!=NULL){
printf(“环境%d:%s\n”,偏移量,环境[offset]);
++抵消;
}
偏移量=0;
while(args[offset]!=NULL){
printf(“参数%d:%s\n”,偏移量,参数[offset]);
++抵消;
}
command=“C:\\windows\\system32\\notepad.exe”;
procHandle=\u spawnvpe(\u P\u NOWAIT,command,args,NULL);
if(prochHandle==-1){
printf(“调用命令失败:%s\n”,strerror(errno));
出口(1);
}
_cwait(&result,procHandle,0);
如果(结果!=0)
printf(“命令退出,错误代码为%d\n”,结果);
}

它适用于我以下代码(仅显示更改的行):

VisualStudio2010,WindowsHPCServer2008R2


请注意,Windows在
路径中搜索程序和动态库
与大多数Unix系统不同,这些系统的可执行路径和库路径有单独的变量。

请确保使用调试版本,它会在此代码上提供断言。argv[0]不能为NULL,它必须指向exe路径。您甚至没有在此处显示的
\u spawnvpe
调用中使用
env
。^\uuuuuuuu^您刚才让我非常高兴。谢谢
...
const char *args[2];
...
args[0] = "notepad.exe";
args[1] = NULL;
...
procHandle = _spawnvpe(_P_NOWAIT, command, args, env);
...