wstring-> ShellExecute 中的 LPCWSTR 给我错误 LNK2028 & LNK2019
您好,我正在使用 UNICODE 和 /clr 使用 Visual C++ 2010(西班牙语)进行编程。我有一个名为“fileFuncs.h”的头文件:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <string>
using namespace std;
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
void callSystem(string sCmd){
std::wstring stemp = s2ws(sCmd);
LPCWSTR params = stemp.c_str();
ShellExecute(NULL,L"open",L"c:\\windows\\system32\\cmd.exe /S /C ",params,NULL,SW_HIDE);
}
但是当我编译时给出以下错误:
错误LNK2028:指的是 未解析的符号(令牌)(0A0004A5) “外部”C“结构 HINSTANCE__ * stdcall ShellExecuteW(struct HWND *,wchar_t const *,wchar_t const *,wchar_t const *,wchar_t const *,int)" (?ShellExecuteW@@$$J224YGPAUHINSTANCE_@@PAUHWND_@@PB_W111H@Z) 在函数“void __cdecl 调用系统(类 std::basic_string,类 std::分配器>)" (?callSystem@@$$FYAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@
错误 LNK2019: 外部符号 “外部”C“结构 HINSTANCE__ * stdcall ShellExecuteW(struct HWND *,wchar_t const *,wchar_t const *,wchar_t const *,wchar_t const *,int)" (?ShellExecuteW@@$$J224YGPAUHINSTANCE_@@PAUHWND_@@PB_W111H@Z) “无效”中提到的未解决 __cdecl callSystem(类 std::basic_string,classstd::allocator)" 功能 (?callSystem@@$$FYAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
是某种类型的配置吗?
Hello I'm programming in Visual C++ 2010 (spanish) with UNICODE and /clr. I have a header file called "fileFuncs.h":
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <string>
using namespace std;
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
void callSystem(string sCmd){
std::wstring stemp = s2ws(sCmd);
LPCWSTR params = stemp.c_str();
ShellExecute(NULL,L"open",L"c:\\windows\\system32\\cmd.exe /S /C ",params,NULL,SW_HIDE);
}
But when I compile give me these errors:
error LNK2028: refers to the
unresolved symbol (token) (0A0004A5)
"extern "C" struct HINSTANCE__ *
stdcall ShellExecuteW(struct HWND *,wchar_t const *,wchar_t const *,wchar_t const *,wchar_t const *,int)" (?ShellExecuteW@@$$J224YGPAUHINSTANCE_@@PAUHWND_@@PB_W111H@Z)
in the function "void __cdecl
callSystem(class
std::basic_string,class
std::allocator >)"
(?callSystem@@$$FYAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@error LNK2019: external symbol
"extern "C" struct HINSTANCE__ *
stdcall ShellExecuteW(struct HWND *,wchar_t const *,wchar_t const *,wchar_t const *,wchar_t const *,int)" (?ShellExecuteW@@$$J224YGPAUHINSTANCE_@@PAUHWND_@@PB_W111H@Z)
unresolved referred to in "void
__cdecl callSystem(class std::basic_string,classstd::allocator)"
function
(?callSystem@@$$FYAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
Is some type of configuration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在“解决方案资源管理器”、“属性”、“链接器”、“输入”中右键单击您的项目。将 shell32.lib 添加到“附加依赖项”设置。
请注意,使用 /clr 选项编译此代码没有什么意义,您没有编写任何托管代码。 ShellExecute() 函数的等效项是 Process::Start()。
Right-click your project in the Solution Explorer, Properties, Linker, Input. Add shell32.lib to the Additional Dependencies setting.
Beware that there's little point in compiling this code with the /clr option, you didn't write any managed code. The equivalent of ShellExecute() function is Process::Start().
附带说明:您确实意识到在这种情况下不需要手动从
std::string
转换为std::wstring
,对吗?与大多数带有字符串参数的 API 函数一样,ShellExecute() 具有 Ansi 和 Unicode 风格。让操作系统为您进行转换:On a side note: you do realize that you do not need to convert from
std::string
tostd::wstring
manually in this situation, right? Like most API functions with string parameters, ShellExecute() has both Ansi and Unicode flavors available. Let the OS do the conversion for you: