0

所以在 Win32 API 中,我的 main 函数是这样定义的:

wmain(int argc, WCHAR* argv[])

我正在向它传递一些参数,并且我想根据参数的值执行一个 switch case,就像这样。

wmain(int argc, WCHAR* argv[])
{
    char* temp = argv[];
    switch (temp) {
    case "one": blah blah;
...
}

当然, temp=argv[] 不起作用,我正在寻找转换它的建议。现在我有一个 if-else-if 事情正在发生,而且效率非常低!

我需要转换它的原因是因为我无法在 WCHAR* 上执行 switch case。

感谢您的关注。

4

4 回答 4

2

您也不能在 char* 上执行开关。(但当你真正需要将 WCHAR* 转换为 char* 时,请使用 WideCharToMultiByte)

您需要使用 if/else if 与lstrcmpiCompareString或其他一些字符串比较函数。

或者,使用参数解析器库之一,如argtablegetopt

于 2010-11-18T19:01:57.790 回答
0

我不确定这是否是个好主意。WCHAR* 可能包含无法以有意义的方式映射到 char* 的 unicode 字符。如果您想忽略这一点,http://www.codeguru.com/forum/showthread.php? t=336106 上有一个论坛帖子,其中有一些关于从 WCHAR* 转换为 char* 的建议。

于 2010-11-18T19:01:26.773 回答
0

尝试将它从 std::wstring 转换为 std::string,这很简单,也许有更短的方法。

使用 std::wstring 构造器将 WCHAR* 转换为 std::wstring,然后使用 std::wstring 方法之一转换为 std::String

于 2010-11-18T19:03:12.300 回答
0

这是我前段时间写的一个简单示例。

创建一个新的 win32 控制台应用程序并选择 ATL 支持。添加这个并编译/运行...

#include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
// A _TCHAR is a typedef'd, depending on whether you've got a unicode or MBCS build

// ATL Conversion macros are documented here
// http://msdn.microsoft.com/en-us/library/87zae4a3(VS.80).aspx
// Declare USES_CONVERSION in your function before using the ATL conversion macros
// e.g. T2A(), A2T()    
USES_CONVERSION;

TCHAR* pwHelloWorld = _T("hello world!");
wcout << pwHelloWorld << endl;

// convert to char
char* pcHelloWorld = T2A(pwHelloWorld);
cout << pcHelloWorld << endl;


cin.get();

return 0;
}

当然,您不能打开字符串,但这应该为您提供将 WCHAR 读入 char 所需的信息。从那里,您可以很容易地转换为 int .. 希望这会有所帮助;)

于 2010-11-19T15:42:57.053 回答