MFC LPWSTR problem....

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I copied this code from a book.
Header:

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // gethostbyipMFCDlg.h : header file
<span style="color:Green; //

#pragma once


<span style="color:Green; // CgethostbyipMFCDlg dialog
<span style="color:Blue; class CgethostbyipMFCDlg : <span style="color:Blue; public CDialogEx
{


u_long m_ulIPAddress;
<span style="color:Blue; char m_lpszMessage[100];
<span style="color:Blue; char m_lpszIP[16];
PHOSTENT m_phostent;
IN_ADDR m_in;
HANDLE m_hGetHostByAddr;
<span style="color:Blue; char m_lpszHostEntryBuf[MAXGETHOSTSTRUCT];


<span style="color:Green; // Construction
<span style="color:Blue; public:



CgethostbyipMFCDlg(CWnd* pParent = NULL); <span style="color:Green; // standard constructor

<span style="color:Green; // Dialog Data
<span style="color:Blue; enum { IDD = IDD_GETHOSTBYIPMFC_DIALOG };

<span style="color:Blue; protected:
<span style="color:Blue; virtual <span style="color:Blue; void DoDataExchange(CDataExchange* pDX); <span style="color:Green; // DDX/DDV support


<span style="color:Green; // Implementation
<span style="color:Blue; protected:
HICON m_hIcon;

<span style="color:Green; // Generated message map functions
<span style="color:Blue; virtual BOOL OnInitDialog();
afx_msg <span style="color:Blue; void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg <span style="color:Blue; void OnPaint();
afx_msg <span style="color:Blue; void OnDoAsyncGetHostByAddr();
afx_msg <span style="color:Blue; long OnAsyncGetHostByAddr(WPARAM wParam,LPARAM lParam);
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
<span style="color:Blue; public:
afx_msg <span style="color:Blue; void OnBnClickedOk();
};

[/code]

And the cpp file:

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // gethostbyipMFCDlg.cpp : implementation file
<span style="color:Green; //

#include <span style="color:#A31515; "stdafx.h"
#include <span style="color:#A31515; "gethostbyipMFC.h"
#include <span style="color:#A31515; "gethostbyipMFCDlg.h"
#include <span style="color:#A31515; "afxdialogex.h"
#include <WinSock2.h>
#include <iostream>
#pragma comment(lib, <span style="color:#A31515; "Ws2_32.lib")


#ifdef _DEBUG
#define <span style="color:Blue; new DEBUG_NEW
#endif

#define WM_USER_ASYNCGETHOSTBYADDR (WM_USER +2)

<span style="color:Green; // CAboutDlg dialog used for App About

<span style="color:Blue; class CAboutDlg : <span style="color:Blue; public CDialogEx
{


<span style="color:Blue; public:





CAboutDlg();

<span style="color:Green; // Dialog Data
<span style="color:Blue; enum { IDD = IDD_ABOUTBOX };

<span style="color:Blue; protected:
<span style="color:Blue; virtual <span style="color:Blue; void DoDataExchange(CDataExchange* pDX); <span style="color:Green; // DDX/DDV support

<span style="color:Green; // Implementation
<span style="color:Blue; protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

<span style="color:Blue; void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)

END_MESSAGE_MAP()


<span style="color:Green; // CgethostbyipMFCDlg dialog




CgethostbyipMFCDlg::CgethostbyipMFCDlg(CWnd* pParent <span style="color:Green; /*=NULL*/)
: CDialogEx(CgethostbyipMFCDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

<span style="color:Blue; void CgethostbyipMFCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CgethostbyipMFCDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()

ON_MESSAGE(WM_USER_ASYNCGETHOSTBYADDR,OnAsyncGetHostByAddr)

ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CgethostbyipMFCDlg::OnDoAsyncGetHostByAddr)
END_MESSAGE_MAP()


<span style="color:Green; // CgethostbyipMFCDlg message handlers

BOOL CgethostbyipMFCDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

WORD wVersionRequested;
WSADATA wsaData;
<span style="color:Blue; int err;

wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
<span style="color:Blue; if (err != 0) {
<span style="color:Green; /* Tell the user that we could not find a usable */
<span style="color:Green; /* Winsock DLL. */
MessageBox((LPCTSTR)<span style="color:#A31515; "Error",(LPCTSTR)<span style="color:#A31515; "Error");

}
<span style="color:Blue; else
MessageBox((LPCTSTR)<span style="color:#A31515; "The Winsock 2.2 dll was found okayn",(LPCTSTR)<span style="color:#A31515; "Done");

<span style="color:Green; // Add "About..." menu item to system menu.

<span style="color:Green; // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
<span style="color:Blue; if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
<span style="color:Blue; if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

<span style="color:Green; // Set the icon for this dialog. The framework does this automatically
<span style="color:Green; // when the applications main window is not a dialog
SetIcon(m_hIcon, TRUE); <span style="color:Green; // Set big icon
SetIcon(m_hIcon, FALSE); <span style="color:Green; // Set small icon

<span style="color:Green; // TODO: Add extra initialization here

<span style="color:Blue; return TRUE; <span style="color:Green; // return TRUE unless you set the focus to a control
}


<span style="color:Blue; void CgethostbyipMFCDlg::OnDoAsyncGetHostByAddr()
{
<span style="color:Green; //get the binary IP address
m_ulIPAddress = inet_addr(<span style="color:#A31515; "168.78.16.201");

<span style="color:Green; //get the host entry structure
<span style="color:Blue; if((m_hGetHostByAddr = WSAAsyncGetHostByAddr(m_hWnd,
WM_USER_ASYNCGETHOSTBYADDR,(<span style="color:Blue; char*)&m_ulIPAddress,4,
PF_INET,m_lpszHostEntryBuf,MAXGETHOSTSTRUCT)) == 0)
{
wsprintf(m_lpszMessage,<span style="color:#A31515; "WSAAsyncGetHostByAddr() generated error %d",
WSAGetLastError());
MessageBox(m_lpszMessage,<span style="color:#A31515; "Info");
}
}

<span style="color:Blue; long CgethostbyipMFCDlg::OnAsyncGetHostByAddr(WPARAM wParam,LPARAM lParam)
{



<span style="color:Green; //check for an error
<span style="color:Blue; if(WSAGETASYNCERROR(lParam) != 0)
MessageBox(<span style="color:#A31515; "WSAAsyncGetHostByAddr() had an error", <span style="color:#A31515; "Info");
<span style="color:Blue; else
{
<span style="color:Green; //assign a hostent host entry pointer to the buffer
m_phostent = (PHOSTENT)m_lpszHostEntryBuf;

<span style="color:Green; //copy the four byte IP address into a Internet address structure
memcpy(&m_in,m_phostent->h_addr,4);

<span style="color:Green; //format the results,converting the ip address into a string
wsprintf(m_lpszMessage,<span style="color:#A31515; "Host %s has IP address ", m_phostent->h_name);
wsprintf(m_lpszIP,<span style="color:#A31515; "%s",inet_ntoa(m_in));
lstrcat(m_lpszMessage,m_lpszIP);
MessageBox(m_lpszMessage,<span style="color:#A31515; "Info");
}
<span style="color:Blue; return 0L;
}
<span style="color:Blue; void CgethostbyipMFCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
<span style="color:Blue; if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
<span style="color:Blue; else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

<span style="color:Green; // If you add a minimize button to your dialog, you will need the code below
<span style="color:Green; // to draw the icon. For MFC applications using the document/view model,
<span style="color:Green; // this is automatically done for you by the framework.

<span style="color:Blue; void CgethostbyipMFCDlg::OnPaint()
{
<span style="color:Blue; if (IsIconic())
{
CPaintDC dc(<span style="color:Blue; this); <span style="color:Green; // device context for painting

SendMessage(WM_ICONERASEBKGND, <span style="color:Blue; reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

<span style="color:Green; // Center icon in client rectangle
<span style="color:Blue; int cxIcon = GetSystemMetrics(SM_CXICON);
<span style="color:Blue; int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
<span style="color:Blue; int x = (rect.Width() - cxIcon + 1) / 2;
<span style="color:Blue; int y = (rect.Height() - cyIcon + 1) / 2;

<span style="color:Green; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
<span style="color:Blue; else
{
CDialogEx::OnPaint();
}
}

<span style="color:Green; // The system calls this function to obtain the cursor to display while the user drags
<span style="color:Green; // the minimized window.
HCURSOR CgethostbyipMFCDlg::OnQueryDragIcon()
{
<span style="color:Blue; return <span style="color:Blue; static_cast<HCURSOR>(m_hIcon);
}



<span style="color:Blue; void CgethostbyipMFCDlg::OnBnClickedOk()
{
<span style="color:Green; // TODO: Add your control notification handler code here

CgethostbyipMFCDlg::OnDoAsyncGetHostByAddr();
}

[/code]
<br/>
<br/>

Now the problem: on every wsprintf use I get: argument of type char* is incompatible with parameter of type "LPCWSTR"
The same error appears on every MessageBox use.
I tried to cast everything to LPCWSTR but when I run the program,and the first dialog box appears telling me if Winsock started with succes or it was an error,instead of normal text,chinese characters appear.
I saw that wsprintf shouldnt be used anymore,however I some of this code on a win32 app and it worked perfectly.
Whats wrong?


View the full article
 
Back
Top