最近写一个内存遍历工具,需要读取进程内存地址,发现如果传入的地址是无效的,就发生内存违例错误,翻了一下书、google了一下,终于搞定了。虽然没技术含量,记录下来,以便以后查看。

BOOL CMyListCtrl::ShowData(PVOID lpAdd)
{
    CString strTmp;
    
//lpAdd=(PVOID)0x12345;
    WCHAR  szWideProgID[1024];

    strTmp.Format(TEXT(
"0x%x"),(DWORD)lpAdd);
    
this->InsertItem(0, strTmp);//地址    
    
    __try
//开EHs-c-选项
        strTmp.Format(TEXT("%x"),*(const long *)lpAdd);
        
this->SetItemText(0,1,strTmp);    //整数型

        strTmp.Format(TEXT(
"%e"),*(const float *)lpAdd);
        
this->SetItemText(0,2,strTmp);    //单精度浮点数

        strTmp.Format(TEXT(
"%e"),*(const double *)lpAdd);
        
this->SetItemText(0,3,strTmp);    //双精度浮点数

        
long  lLen = MultiByteToWideChar(CP_ACP,0,(LPCSTR)lpAdd,strlen((LPCSTR)lpAdd),
            szWideProgID,
sizeof(szWideProgID));  
        szWideProgID[lLen] 
= '\0';

        
//strTmp.Format(TEXT("%s"),);
        this->SetItemText(0,4,szWideProgID);    //字符串
        
        strTmp.Format(TEXT(
"%s"),(const WCHAR *)lpAdd);
        
this->SetItemText(0,5,strTmp);    //U_字符串
    }

    __except( EXCEPTION_EXECUTE_HANDLER )
    
{
        
this->SetItemText(0,1,TEXT("??"));    //整数型
        this->SetItemText(0,2,TEXT("??"));    //单精度浮点数
        this->SetItemText(0,3,TEXT("??"));    //双精度浮点数
        this->SetItemText(0,4,TEXT("??"));    //字符串
        this->SetItemText(0,5,TEXT("??"));    //U_字符串
    }


/*    */
    
return 0;
}