代码如下:void SearchPhone::OnPaint()
{
CPaintDC dc(this); // device context for painting
RECT rect;
memcpy(&rect,&this->m_rcDesktop,sizeof(RECT));
this->MoveWindow(&rect);

if(this->m_pDevList && 
this->m_pLockDevList && 
this->m_pDevList->size() > 0
)
{

//刷新显示
if(this->m_pDevListCtrl == 0)
this->m_pDevListCtrl = (CListBox*)this->GetDlgItem(IDC_LIST1);
if(this->m_pDevListCtrl != 0)
{
for(int j = 0; j < this->m_pDevListCtrl->GetCount() + 1; ++j)
this->m_pDevListCtrl->DeleteString(0);

this->m_pLockDevList->Lock();
for(int i = this->m_pDevList->size() - 1; i >= 0 ; --i)
{
[color=#000000] this->m_pDevListCtrl->InsertString(0,(*this->m_pDevList)[i].name);[/color]
}
this->m_pLockDevList->UnLock();
}


}
}输出的错误是:error C2664: 'InsertString' : cannot convert parameter 2 from 'unsigned short [260]' to 'const char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast  .师傅告诉我是宽字符没有转化的问题,需要进行unicode转化,我查了MSDN还有google一些资料,知道用MultiByteToWideChar,可总是下不了手。哪位达人帮帮忙,给修改一下...

解决方案 »

  1.   

    MSDN例子
    #include <windows.h>
    #include <lm.h>
    #include <stdio.h>BOOL GetFullName( char *UserName, char *Domain, char *dest )
    {
        WCHAR wszUserName[UNLEN+1];          // Unicode user name
        WCHAR wszDomain[256]; 
        LPBYTE ComputerName;    struct _SERVER_INFO_100 *si100;  // Server structure
        struct _USER_INFO_2 *ui;         // User structure// Convert ANSI user name and domain to Unicode    MultiByteToWideChar( CP_ACP, 0, UserName,
            strlen(UserName)+1, wszUserName,   
         sizeof(wszUserName)/sizeof(wszUserName[0]) );
        MultiByteToWideChar( CP_ACP, 0, Domain,
            strlen(Domain)+1, wszDomain, sizeof(wszDomain)/sizeof(wszDomain[0]) );// Get the computer name of a DC for the domain.    NetGetDCName( NULL, wszDomain, &ComputerName );// Look up the user on the DC.    if( NetUserGetInfo( (LPWSTR) ComputerName,
            (LPWSTR) &wszUserName, 2, (LPBYTE *) &ui ) )
        {
            printf( "Error getting user information.\n" );
            return( FALSE );
        }// Convert the Unicode full name to ANSI.    WideCharToMultiByte( CP_ACP, 0, ui->usri2_full_name, -1,
            dest, 256, NULL, NULL );    return (TRUE);
    }
      

  2.   

    cannot convert parameter 2 from 'unsigned short [260]' to 'const char *'unsigned short是宽字符,lz应该用WideCharToMultiByte
    //This is an example
    CHAR szToWrite[MAX_PATH];
    WCHAR szTemp[MAX_PATH];//...DWORD dwSize = WideCharToMultiByte(CP_OEMCP, NULL, tchar, -1, NULL, 0, NULL, FALSE);
    WideCharToMultiByte (CP_OEMCP, NULL, tchar, -1, szToWrite, dwSize, NULL, FALSE);
      

  3.   


    你那表达式好复杂,看不懂;不过,转换倒是有简单偷懒的方法。
    看样子,你的工程不是 UNICODE 工程,这样就可以利用 CString 帮你转换。this->m_pDevListCtrl->InsertString(0,(*this->m_pDevList)[i].name);其中的 (*this->m_pDevList)[i].name改为:CString( (*this->m_pDevList)[i].name )=======你的表达式太复杂,我真的看不懂,也不知道那样写对不对。总之,CString 是会帮你转换;上面如果不对,你稍微研究一下。
      

  4.   

    谢各位达人,CString就能解决。但是问题反过来了该用什么了?比如这一句:
    while (ERROR_SUCCESS == WSALookupServiceNext (hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pwsaResults))
    {
    ASSERT (pwsaResults->dwNumberOfCsAddrs == 1);
    bHaveName = pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName);
      OutputDebugStringW(pwsaResults->lpszServiceInstanceName);
      OutputDebugStringW(L"\n");

    }
    错误是:error C2664: 'OutputDebugStringW' : cannot convert parameter 1 from 'char *' to 'const unsigned short *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    貌似是相反的了...