- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我更改了一个旧的 MFC 应用程序以支持 Unicode。现在代码如下所示。基本上我在这里调用一个 .cmd 文件:
STARTUPINFO StartupInfo;
DWORD dwErrorCode;
PROCESS_INFORMATION * processInformation = new PROCESS_INFORMATION[2];
for (int i = 0; i < 2; i++)
{
GetStartupInfo(&StartupInfo);
if (processList[i].bHide)
{
StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
}
if (processList[i].sCustomTitle.GetLength())
StartupInfo.lpTitle = processList[i].sCustomTitle.GetBuffer(processList[i].sCustomTitle.GetLength());
CreateProcess(NULL,
/*I changed LPSTR to LPWSTR*/(LPWSTR)(LPCTSTR)processList[i].sCommandLine,
NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &processInformation[i]);
[...]
}
我所做的唯一更改是将 LPSTR
更改为 LPWSTR
。在转换为 Unicode 之前,这是没有任何问题的。但是现在它没有运行。可能是什么原因?在转换为 Unicode
支持时,我是否遗漏了任何需要完成的事情?
P.S.:我调试检查了所有的参数。他们看起来很好。 sCommandLine
是 PROCESS_STARTUP_INFO 结构
中的一个 CString
变量。
最佳答案
如评论中所述,您不应转换字符串。您应该使用临时变量,因为 documentation 中的以下注释:
The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.
最近我遇到了类似的问题。我通过以下方式解决了它(我试图让它防弹):
// The maximum length of the command line string is 32,768 characters,
// including the Unicode terminating null character.
#define MAX_CMD_LINE_LEN 32768
// Use a temporary variable for the command line.
TCHAR szCmdLine[MAX_CMD_LINE_LENGTH];
// Get the length of the command line. Crop the command line if it is too long.
int iLen = min(processList[i].sCommandLine.GetLength(), MAX_CMD_LINE_LEN - 1);
// Copy the command line string to the temporary variable.
_tcsncpy_s(szCmdLine, processList[i].sCommandLine, iLen);
// Create the process by passing the temporary variable holding a copy of your
// original command line string.
CreateProcess(NULL,
szCmdLine,
NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL,
NULL, &StartupInfo, &processInformation[i]);
此解决方案应该编译并适用于 Unicode 和非 Unicode 版本。
关于c++ - 将项目转换为支持 Unicode 后,CreateProcess 不运行 .cmd 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25439783/
我正在使用 Android Studio。我想在 github 上分享我的项目,但我遇到了很多问题。 首先,Android studio找不到git.exe。我可以用某种方式解决那个问题,但现在是下一
今天,我刚刚使用 Android SDK 管理器更新了 Windows 7 上的 Android SDK,并在 Eclipse 中构建期间开始出现错误 "Error executing aapt: C
由于某种原因创建进程打开cmd即使未指定按 Enter 键会导致命令行中弹出更多 cmd 实例。 #include #include void main() { PROCESS_INFOR
这个问题已经有答案了: Unhandled Error with CreateProcess [duplicate] (2 个回答) 已关闭 4 年前。 我的目标是在我的程序中执行外部可执行文件。首先
当使用 CreateProcess 运行另一个程序时,捕获标准输出的推荐方法是什么?也就是说,将第二个程序打印到 stdout 的任何内容,并以第一个程序可以分析它的数组结束? 这两个程序都是用 C
这个问题在这里已经有了答案: Unhandled Error with CreateProcess [duplicate] (2 个答案) 关闭 4 年前。 我的目标是在我的程序中执行一个外部可执行
我的以下代码有问题: int main(int argc, char **argv) { PROCESS_INFORMATION pi; STARTUPINFO si; prin
forking() 和 CreateProcess(带有所有必需的参数)对于 Linux 和 WinXP 是否分别是相同的? 如果它们不同,那么有人可以解释两种情况下发生的情况的差异吗? 谢谢 最佳答
我正在寻找 Delphi 中 CreateProcess 的快速替代方案,以在 exe 中执行某些计算,包括 XML 中的多个返回值。目前,我正在调用带有特定参数的 C#-exe。这些电话之一需要大约
我有我的主应用程序,从我的主应用程序我将调用另一个 模块(第三方)在我的主应用程序中执行一个小操作,当我调用该模块时..它处理特定时间说 5 秒。而它的处理它在命令窗口中显示了一些信息..现在我的主应
我注意到了这种行为: 当前目录下有2个可执行文件,分别名为“somefile”和“somefile.abc”。 CreateProcessA(NULL, "somefile", ...) - 失败,错
C 初学者警告!!! 我正在用 c 编写一个应用程序,该应用程序应该在“cmd.exe”上下文中运行用户定义的命令并将输出写入变量。例如,如果命令变量是“dir C:\”,则 c 程序应使用 Crea
我正在尝试使用 CreateProcess() 启动服务器。这是代码: int APIENTRY WinMain(HINSTANCE hInstance, H
所以我使用这段代码来启动带有参数的控制台应用程序: #include #include using namespace std; void StartProgram(char argv[]) {
我刚刚安装了代码块 我在安装时遇到错误(无法在 gcc gnu 中配置的路径中找到编译器可执行文件) 所以我安装了编译器MinGW出现这个错误 错误: *CreateProcess:没有这样的文件或目
我正在尝试弄清楚如何使用 CreateProcess() 函数,但我不太精通 C++。我已经尝试了一些方法来尝试让错误消失,但应用程序似乎没有按照我期望的方式执行。 我想做的是将“cmd.exe/c
我有一个应用程序,用户可以在其中将文件上传到远程服务器,接收该文件的同一台服务器应该运行该应用程序。我正在使用 CreateProcess 方法。问题是,文件目录已经在 std::字符串中定义,我很难
如果程序A被用户以管理员身份执行,程序A是否可以使用CreateProcess() windows函数以管理员权限启动程序B?提前谢谢你 最佳答案 答案是肯定的。如果启动程序以提升的方式运行,您可以以
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我正在尝试在 windows 7 上的 windows visual studio 2012 professional 上使用 visual c++ 打开一个程序。代码将顺利运行,但实际上不会打开程序
我是一名优秀的程序员,十分优秀!