首页 文章

wstring - > ShellExecute中的LPCWSTR给我错误LNK2028和LNK2019

提问于
浏览
1

您好我正在用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)"extern " C “结构HINSTANCE__ * stdcall ShellExecuteW(struct HWND ,wchar_t的常量,wchar_t的常量*,wchar_t的常量*,wchar_t的常量*,INT)”(ShellExecuteW @@ $$ J224YGPAUHINSTANCE _ @@ PAUHWND _ @@在函数"void __cdecl callSystem(class std::basic_string,class std::allocator >)"(?callSystem @@ $$ FYAXV?$ basic_string的@ DU?PB_W111H @ Z)$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@

  • 错误LNK2019:外部符号"extern " C “结构HINSTANCE__ * stdcall ShellExecuteW(struct HWND ,wchar_t的常量,wchar_t的常量*,wchar_t的常量*,wchar_t的常量*,INT)”(ShellExecuteW @@ $$ J224YGPAUHINSTANCE _ @@ PAUHWND _ @@ PB_W111H @ Z )悬而未决称为在"void __cdecl callSystem(class std::basic_string,classstd::allocator)"函数(?callSystem @@ $$ FYAXV?$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@ Z)

是某种类型的配置?

2 回答

  • 1

    在解决方案资源管理器,属性,链接器,输入中右键单击您的项目 . 将shell32.lib添加到Additional Dependencies设置 .

    请注意,使用/ clr选项编译此代码没有什么意义,您没有编写任何托管代码 . ShellExecute()函数的等价物是Process :: Start() .

  • 1

    在旁注:您确实意识到在这种情况下您不需要手动将 std::string 转换为 std::wstring ,对吧?与大多数带有字符串参数的API函数一样,ShellExecute()同时具有Ansi和Unicode风格 . 让操作系统为您进行转换:

    #include <string> 
    
    void callSystem(std::string sCmd)
    {
        ShellExecuteA(NULL, "open", "c:\\windows\\system32\\cmd.exe /S /C ", sCmd.c_str(), NULL, SW_HIDE);
    }
    

相关问题