字符未处理的异常在Win32 API的命令行解析 [英] Unhandled exception for character parsed from command line in Win32 API

查看:139
本文介绍了字符未处理的异常在Win32 API的命令行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析的命令行参数,并转换某些字符为整数。

I have to parse the command line argument and convert certain char into integer.

我有以下的code:

TCHAR *token1;
token1 = strchr(cmd_line, L'+');
MessageBox(NULL, token1, _T("test"), NULL);

//char *str = "+10 frogs";
TCHAR *endptr =" ";

long n = strtol(token1 + 1, &endptr, 0);

if (!*endptr)
    MessageBox(NULL, "No error", _T("test"), NULL);
else 
    MessageBox(NULL, "error", _T("test"), NULL);

当命令WesOPC.exe +10 -regserver,在命令提示符下被发送,消息框显示+10 -regserver。

When the command "WesOPC.exe +10 -regserver" is sent in command prompt , the message box displays "+10 -regserver".

我需要存储10为整数。所以,我用与strtol但随着错误消息框显示。

I need to store 10 as integer .So, i used strtol but messagebox with error is displayed.

当我试图调试它,在线看到的误差

When i tried to debug it , the error seen at line

long n = strtol(token1 + 1, &endptr, 0);

Unhandled exception at 0x0042d368 in WesOPC.exe: 0xC0000005: Access violation reading location 0x00000001.

N应该是10

推荐答案

您是在内存地址得到一个AV 00000001 。您加1 TOKEN1 。这意味着 TOKEN1 为NULL,但你的code未检查这一条件。事实上,你的拳头来电的MessageBox()应该显示在这种情况下一个空字符串。

You are getting an AV at memory address 0x00000001. You are adding 1 to token1. That means token1 is NULL, but your code is not checking for that condition. In fact, your fist call to MessageBox() should be showing a blank string in that situation.

另一个问题是,你是混合的char * wchar_t的* TCHAR * 在同一code一起。 TOKEN1 使用 TCHAR * L'+'是使用 wchar_t的和strchr()期望的char * 字符。您code甚至不应该编译原样。停止数据类型混合在一起,并选择一个类型来处理一切。

Another problem is that you are mixing char*, wchar_t* and TCHAR* together in the same code. token1 is using TCHAR*, L'+' is using wchar_t, and strchr() expects char* and char. your code should not even compile as-is. Stop mixing data types together and pick one type to handle everything.

试试这个:

// where cmd_line is TCHAR*...
TCHAR *token1 = _tcschr(cmd_line, _T('+'));
if (token1)
{
    MessageBox(NULL, token1, TEXT("test"), NULL);

    //char *str = "+10 frogs";
    TCHAR *endptr = " ";

    ++token1;
    long n = _tcstol(token1, &endptr, 0);

    if ((n == 0) && (endptr == token1))
        MessageBox(NULL, TEXT("error"), TEXT("test"), NULL);
    else 
        MessageBox(NULL, TEXT("No error"), TEXT("test"), NULL);
}
else
    MessageBox(NULL, TEXT("param not found"), TEXT("test"), NULL);

或者停止支持ANSI和只是去完全统一code的一切:

Or, stop supporting Ansi and just go full Unicode on everything:

// where cmd_line is LPWSTR...
LPWSTR token1 = wcschr(cmd_line, L'+');
if (token1)
{
    MessageBoxW(NULL, token1, L"test", NULL);

    //char *str = "+10 frogs";
    LPWSTR *endptr = L" ";

    ++token1;
    long n = wcstol(token1, &endptr, 0);

    if ((n == 0) && (endptr == token1))
        MessageBoxW(NULL, L"error", L"test", NULL);
    else 
        MessageBoxW(NULL, L"No error", L"test", NULL);
}
else
    MessageBoxW(NULL, L"param not found", L"test", NULL);

这篇关于字符未处理的异常在Win32 API的命令行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆