c++ - 如何设置从 CreateProcess 创建的进程的搜索路径

标签 c++ visual-studio compiler-errors createprocess search-path

我正在使用 CreateProcess调用 cl将另一个 C++ 程序 ( TestProg.cxx) 编译并链接到 DLL 中。我调用 cl.exe具有以下编译选项:

编译选项:

/Od /nologo /Fo /RTC /w /Zc TestProg.cxx /DLL

电话:

if ( CreateProcess(PATH_TO_EXE, COMPILATION_OPTIONS, NULL,NULL,
                   FALSE,0,NULL,NULL,&si,&pi) ) 
{
    //....
}

如果TestProg.exe包含 #include <iostream.h>我收到以下编译错误:

TestProg.cpp(1) : fatal error C1034: iostream.h: no include path set

没有任何#include命令,我收到以下链接错误:

LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'

我做错了什么?

我在网上搜索了最近 6-7 个小时的答案,但没有找到。使用 Windows API 对我来说是新的。

最佳答案

如果您查看 CreateProcess 的定义:

BOOL WINAPI CreateProcess(
  __in_opt     LPCTSTR lpApplicationName,
  __inout_opt  LPTSTR lpCommandLine,
  __in_opt     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  __in_opt     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in         BOOL bInheritHandles,
  __in         DWORD dwCreationFlags,
  __in_opt     LPVOID lpEnvironment,
  __in_opt     LPCTSTR lpCurrentDirectory,
  __in         LPSTARTUPINFO lpStartupInfo,
  __out        LPPROCESS_INFORMATION lpProcessInformation
);

您正在设置可选参数 __in_opt LPVOID lpEnvironment,NULL .

根据上述定义:

A pointer to the environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process.

cl.exe从环境变量中获取其包含位置信息和库搜索路径 - 查看 setenv.bat在 VS 目录中。在这种情况下,您的调用进程或目标进程都没有在设置了这些变量的环境中运行。

你有一个选择——你可以根据MSDN自己创建环境变量:

An environment block consists of a null-terminated block of null-terminated strings. Each string is in the following form:

name=value\0

Because the equal sign is used as a separator, it must not be used in the name of an environment variable.

或者您可以要求您的程序在 VS 工具提示符下运行。一个很好的检查,这实际上是问题所在,从这个提示符而不是 Visual Studio 运行你的程序,看看是否能解决问题。

不使用 #include 的原因产生链接器错误是因为如果没有包含,cl.exe不会查找它们 - 然后它会查找 C/C++ 运行时库。

附带说明 - 我相信 C++ 中的标准是 #include <iostream>即没有 .h .

关于c++ - 如何设置从 CreateProcess 创建的进程的搜索路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710893/

相关文章:

c++ - 声明 stringstream ss 之间的区别;和 std::stringstream ss;在 C++ 中?

c++ - for循环每次计算相同的值

c++ - 数组大小和常量

visual-studio - TF400324 : Team Foundation services are not available. 无法创建 SSL/TLS 安全通道

c++ - 错误 LNK2019 : unresolved external symbol Direct Sound

asp.net - Visual Studio 2010 SP1 中的 .ASPX 文件不会启动自动大纲(可折叠部分)

sql - PL/SQL -- 变量 "must be a type"编译错误

java - 使用.jar文件在cmd提示符下编译并运行Java代码

c++ - 我可以报告 openmp 任务的进度吗?

java - Apache FOP - Fop 类型的 getDefaultHandler() 方法未定义