wstring -> LPCWSTR in ShellExecute give me error LNK2028 & LNK2019
Hello I'm programming in Visual C++ 2010 (spanish) with UNICODE and /clr. I h开发者_如何学JAVAave 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?
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().
On a side note: you do realize that you do not need to convert from std::string
to std::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:
#include <string>
void callSystem(std::string sCmd)
{
ShellExecuteA(NULL, "open", "c:\\windows\\system32\\cmd.exe /S /C ", sCmd.c_str(), NULL, SW_HIDE);
}
精彩评论