C++ 将两个字符 * 连接到命令行参数的 LPTSTR



我是C++新手,但我正在尝试在Windows中构建一个程序来玩父进程将启动子进程的并发性。是否有一个 Windows 函数将从我的方法中获取 arg1 和 arg2,BuildChildProcess(char * arg1, char * arg2) ,并将这两个参数连接到要在 CreateProcess 函数中使用的LPTSTR命令行字符串?

在我得知它不安全之前,我最初只尝试使用其中一个参数进行strcat,所以我随后尝试了strcat_s但要么它不适用于我正在尝试做的事情,要么我只是做错了。如果我应该使用它,那么使用它的工作示例会有所帮助。

谢谢

编辑:基本上,如何将两个char * C 样式字符串(arg1 和 arg2(连接到 CreateProcess 函数的 lpCommandLine 参数的末尾,即类型 LPTSTR

由于您使用C++因此可以使用标准库字符串。您对TCHAR的使用令人困惑。是char还是wchar_t?如果是前者,为什么不支持Unicode?您不需要使用TCHAR它只是为了帮助处理需要在不支持 Unicode 的 Windows 9x 上运行的代码。

从问题的其他部分,您希望连接两个名为 arg1arg2char* C 字符串。这样做:

#include <string>
....
std::string str1 = arg1;
std::string str2 = arg2;
std::string combined = str1 + str2;

它可以写得更简洁,但我想为你明确一点。

然后通过combined.c_str()如果您需要const char*&combined[0]如果您需要char*char*的后一个选项要求您使用 C++-11 或更高版本。

就个人而言,我会在这里使用本机宽的Unicode API。这意味着std::wstring而不是std::string

您可以使用

以下代码:

BuildChildProcess(char * arg1, char * arg2){
     char buff[256];
     sprintf_s(buff, 256, TEXT("%s%s"), arg1, arg2);
     .........
}

然后buff将包含你的两个字符*。如果您需要使用 unicode 字符串,则需要 unicode 版本的 sprintf_s。

https://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx

如果您需要从 char 转换为 UNICODE,那么有 API 函数 MultiByteToWideChar((

https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85(.aspx

最新更新