2011-04-16 75 views
1

你好我正在使用UNICODE和/ clr在Visual C++ 2010(西班牙語)編程。我有一個名爲 「fileFuncs.h」 頭文件:wstring - > ShellExecute中的LPCWSTR給我錯誤LNK2028&LNK2019

#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(結構HWND *,wchar_t的常量*,wchar_t的常量*,wchar_t的常量*,wchar_t的常量*,INT)」(?ShellExecuteW @@ $$ J224YGPAUHINSTANCE_ @@ PAUHWND _ @@ PB_W111H @ Z) 在函數 「空隙__cdecl callSystem(類 的std :: basic_string的,類 的std ::分配器>)」 (?callSystem @@ $$ FYAXV?$ basic_string的@ DU?$ char_traits @ d @ @@性病V ?$ @分配器@ d @@ 2 STD @@

  • 錯誤LNK2019:外部符號 「外部的 「C」 結構HINSTANCE__ * STDCALL ShellExecuteW(HWND結構*,wchar_t的常量*,爲wchar_t常量*,爲wchar_t const?,wchar_t const *,int)「(?ShellExecuteW @@ $$ J224YGPAUHINSTANCE_ @@ PAUHWND _ @@ PB_W111H @ Z) 」void __cdecl callSyst em(class std :: basic_string,classstd :: allocator)「 函數 (?callSystem @@ $$ FYAXV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@@ Z)

是一些類型的配置?

+1

您是否在鏈接中包含Shell32.lib? – Jollymorphic 2011-04-16 00:55:39

+0

我是C++中的新手。我如何鏈接Shell32.lib? – Galled 2011-04-16 00:59:40

回答

1

在解決方案資源管理器,屬性,鏈接器,輸入中右鍵單擊項目。將shell32.lib添加到Additional Dependencies設置。

請注意,使用/ clr選項編譯此代碼幾乎沒有意義,您沒有編寫任何託管代碼。 ShellExecute()函數的等價物是Process :: Start()。

+0

現在編譯好。謝謝 – Galled 2011-04-16 01:07:39

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); 
} 
+0

ShellExecute和ShellExecuteA有什麼區別? – Galled 2011-04-20 04:04:23

+1

'ShellExecuteA()'是Ansi版本,'ShellExecuteW()'是Unicode版本。 'ShellExecuteA()'在內部調用'ShellExecuteW()',根據需要將Ansi字符串轉換爲Unicode。 ShellExecute()是一個別名,它映射到ShellExecuteA()或ShellExecuteW(),這取決於項目是否由UNICODE定義或不定義。在你的例子中,它是(或者'ShellExecute()'不會接受'wchar_t *'數據)。大多數使用字符串數據的Win32 API函數都使用這種類型的A/W映射。 – 2011-04-21 00:44:28

+0

感謝您的回答 – Galled 2011-05-03 20:27:51