如何在VC ++中正确启动进程?

时间:2012-04-25 01:20:52

标签: visual-studio-2010 c++-cli

我有一个非常简单的窗体程序,我想按下按钮时启动notepad.exe。我得到了一些预期的错误。请帮忙。

在我的代码开头,我有

#pragma once
#include <windows.h>
#include <Shellapi.h>

在事件处理程序中,我有

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {


                //memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));

                ExecuteInfo.cbSize       = sizeof(ExecuteInfo);
                ExecuteInfo.fMask        = NULL;              
                ExecuteInfo.hwnd         = NULL;               
                ExecuteInfo.lpVerb       = "open";                      // Operation to perform
                ExecuteInfo.lpFile       = "C:\\Windows\\notepad.exe";  // Application name
                ExecuteInfo.lpParameters = NULL;           // Additional parameters
                ExecuteInfo.lpDirectory  = NULL;                          // Default directory
                ExecuteInfo.nShow        = SW_SHOW;
                ExecuteInfo.hInstApp     = NULL;

                ShellExecuteEx(&ExecuteInfo);

         }

注意:如果我在属性页&gt;配置属性&gt;下设置为“使用Unicode字符集”,则会出现以下错误消息:一般(ALT-F7)

1>c:\users\marco\desktop\new folder (2)\test000\test000\Form1.h(140): error C2440: '=' : cannot convert from 'const char [5]' to 'LPCWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\marco\desktop\new folder (2)\test000\test000\Form1.h(141): error C2440: '=' : cannot convert from 'const char [23]' to 'LPCWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

注意:如果我在属性页&gt;配置属性&gt;下设置为“使用多字节字符集”,则会出现以下错误消息:一般(ALT-F7)

1>test000.obj : error LNK2028: unresolved token (0A000012) "extern "C" int __stdcall ShellExecuteExA(struct _SHELLEXECUTEINFOA *)" (?ShellExecuteExA@@$$J14YGHPAU_SHELLEXECUTEINFOA@@@Z) referenced in function "private: void __clrcall test000::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@test000@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>test000.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall ShellExecuteExA(struct _SHELLEXECUTEINFOA *)" (?ShellExecuteExA@@$$J14YGHPAU_SHELLEXECUTEINFOA@@@Z) referenced in function "private: void __clrcall test000::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@test000@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>C:\Users\Marco\Desktop\New folder (2)\test000\Debug\test000.exe : fatal error LNK1120: 2 unresolved externals

2 个答案:

答案 0 :(得分:1)

您需要在字符串周围使用TEXT()宏(例如TEXT(“open”)而不是“open”)或在运行时将ANSI字符串转换为UTF-16(例如,使用mbstowcs_s()函数)。

发生这种情况的原因是TCHAR是Microsoft char类型,它是char或wchar_t,具体取决于项目是否配置为unicode。请注意,lpFile和friends的类型是LPCTCHAR(指向const TCHAR的长指针),这意味着如果你使用(默认)unicode配置,它最终会成为const wchar_t *,而char []不能是隐含地投向它。

答案 1 :(得分:1)

另一种解决方案:如果您使用的是C ++ / CLI,您也可以使用托管方法启动进程:

System::Diagnostics::Process::Start("C:\\Windows\\notepad.exe");

这也应该避免字符集问题 这并不意味着你应该忽略它们,因为了解底层问题是件好事。 Roee Shenberg在他的回答中表达了这一点。