'CreateWindowExW' : cannot convert parameter 2 from 'char [14]' to 'LPCWSTR'

This is a discussion on 'CreateWindowExW' : cannot convert parameter 2 from 'char [14]' to 'LPCWSTR' within the Windows Programming forums, part of the Platform Specific Boards category; Why does this code flag error. (Don't be daunted by the length of the code, the error is simple and ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    'CreateWindowExW' : cannot convert parameter 2 from 'char [14]' to 'LPCWSTR'

    Why does this code flag error. (Don't be daunted by the length of the code, the error is simple and it is in line no.265 (12th line from the bottom, in second parameter of call to CreateWindow()))


    Code:
    #include <SysMets.h>
    BOOL InitInstance(HINSTANCE, int nCmdShow);
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
         MSG          msg ;
    
         InitInstance(hInstance, iCmdShow);
        
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         static int  cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth ;
         HDC         hdc ;
         int         i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd ;
         PAINTSTRUCT ps ;
         SCROLLINFO  si ;
         TCHAR       szBuffer[10] ;
         TEXTMETRIC  tm ;
         
         switch (message)
         {
         case WM_CREATE:
              hdc = GetDC (hwnd) ;
              
              GetTextMetrics (hdc, &tm) ;
              cxChar = tm.tmAveCharWidth ;
              cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
              cyChar = tm.tmHeight + tm.tmExternalLeading ;
              
              ReleaseDC (hwnd, hdc) ;
    
                   // Save the width of the three columns
              
              iMaxWidth = 40 * cxChar + 22 * cxCaps ;
              return 0 ;
    
         case WM_SIZE:
              cxClient = LOWORD (lParam) ;
              cyClient = HIWORD (lParam) ;
    
                   // Set vertical scroll bar range and page size
    
              si.cbSize = sizeof (si) ;
              si.fMask  = SIF_RANGE | SIF_PAGE ;
              si.nMin   = 0 ;
              si.nMax   = NUMLINES - 1 ;
              si.nPage  = cyClient / cyChar ;
              SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
    
                   // Set horizontal scroll bar range and page size
    
              si.cbSize = sizeof (si) ;
              si.fMask  = SIF_RANGE | SIF_PAGE ;
              si.nMin   = 0 ;
              si.nMax   = 2 + iMaxWidth / cxChar ;
              si.nPage  = cxClient / cxChar ;
              SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
              return 0 ;
              
         case WM_VSCROLL:
                   // Get all the vertical scroll bar information
    
              si.cbSize = sizeof (si) ;
              si.fMask  = SIF_ALL ;
              GetScrollInfo (hwnd, SB_VERT, &si) ;
    
                   // Save the position for comparison later on
    
              iVertPos = si.nPos ;
    
              switch (LOWORD (wParam))
              {
              case SB_TOP:
                   si.nPos = si.nMin ;
                   break ;
                   
              case SB_BOTTOM:
                   si.nPos = si.nMax ;
                   break ;
                   
              case SB_LINEUP:
                   si.nPos -= 1 ;
                   break ;
                   
              case SB_LINEDOWN:
                   si.nPos += 1 ;
                   break ;
                   
              case SB_PAGEUP:
                   si.nPos -= si.nPage ;
                   break ;
    
              case SB_PAGEDOWN:
                   si.nPos += si.nPage ;
                   break ;
                   
              case SB_THUMBTRACK:
                   si.nPos = si.nTrackPos ;
                   break ;
                   
              default:
                   break ;         
              }
                   // Set the position and then retrieve it.  Due to adjustments
                   //   by Windows it may not be the same as the value set.
    
              si.fMask = SIF_POS ;
              SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
              GetScrollInfo (hwnd, SB_VERT, &si) ;
    
                   // If the position has changed, scroll the window and update it
    
              if (si.nPos != iVertPos)
    		  {                    
                   ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos), 
                                       NULL, NULL) ;
                   UpdateWindow (hwnd) ;
    		  }
              return 0 ;
              
         case WM_HSCROLL:
                   // Get all the vertical scroll bar information
    
              si.cbSize = sizeof (si) ;
              si.fMask  = SIF_ALL ;
    
                   // Save the position for comparison later on
    
              GetScrollInfo (hwnd, SB_HORZ, &si) ;
              iHorzPos = si.nPos ;
    		  
              switch (LOWORD (wParam))
              {
              case SB_LINELEFT:
                   si.nPos -= 1 ;
                   break ;
                   
              case SB_LINERIGHT:
                   si.nPos += 1 ;
                   break ;
                   
              case SB_PAGELEFT:
                   si.nPos -= si.nPage ;
                   break ;
                   
              case SB_PAGERIGHT:
                   si.nPos += si.nPage ;
                   break ;
                   
              case SB_THUMBPOSITION:
                   si.nPos = si.nTrackPos ;
                   break ;
                   
              default :
                   break ;
              }
                   // Set the position and then retrieve it.  Due to adjustments
                   //   by Windows it may not be the same as the value set.
    
              si.fMask = SIF_POS | SIF_DISABLENOSCROLL ;
              SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
              GetScrollInfo (hwnd, SB_HORZ, &si) ;
              
                   // If the position has changed, scroll the window 
    
              if (si.nPos != iHorzPos)
              {
                   ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0, 
                                 NULL, NULL) ;
              }
              return 0 ;
    
        case WM_PAINT :
             hdc = BeginPaint (hwnd, &ps) ;
    
                   // Get vertical scroll bar position
    
              si.cbSize = sizeof (si) ;
              si.fMask  = SIF_POS ;
              GetScrollInfo (hwnd, SB_VERT, &si) ;
              iVertPos = si.nPos ;
    
                   // Get horizontal scroll bar position
              GetScrollInfo (hwnd, SB_HORZ, &si) ;
              iHorzPos = si.nPos ;
    
                   // Find painting limits
    
              iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
              iPaintEnd = min (NUMLINES - 1,
                               iVertPos + ps.rcPaint.bottom / cyChar) ;
              
              for (i = iPaintBeg ; i <= iPaintEnd ; i++)
              {
                   x = cxChar * (1 - iHorzPos) ;
                   y = cyChar * (i - iVertPos) ;
                   
                   TextOut (hdc, x, y,
                            sysmetrics[i].szLabel,
                            lstrlen (sysmetrics[i].szLabel)) ;
                   
                   TextOut (hdc, x + 22 * cxCaps, y,
                            sysmetrics[i].szDesc,
                            lstrlen (sysmetrics[i].szDesc)) ;
                   
                   SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
                   
                   TextOut (hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer,
                            wsprintf (szBuffer, TEXT ("%5d"),
                                 GetSystemMetrics (sysmetrics[i].iIndex))) ;
    
                   SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
              }
    
              EndPaint (hwnd, &ps) ;
              return 0 ;
              
         case WM_DESTROY :
             PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    
    HINSTANCE hInst ; // current instance
    
    /*   FUNCTION: InitInstance ( HANDLE, int 
         PURPOSE: Saves instance handle and creates main window
         COMMENTS: In this function, we save the instance handle in a global  
                                variable and create and display the main program window.
    */
    BOOL InitInstance ( HINSTANCE hInstance, int nCmdShow )
    {
    	char classname[ ] = "MyWindowClass" ;
    	HWND hWnd ;
    	
    	WNDCLASSEX wcex ;
    	wcex.cbSize			= sizeof ( WNDCLASSEX ) ; 
    	wcex.style			= CS_HREDRAW | CS_VREDRAW ;
    	wcex.lpfnWndProc	= ( WNDPROC ) WndProc ;
    	wcex.cbClsExtra		= 0 ;
    	wcex.cbWndExtra	= 0 ;
    	wcex.hInstance		= hInstance ;
    	wcex.hIcon			= NULL ;
    	wcex.hCursor		= LoadCursor ( NULL, IDC_ARROW ) ;
    	wcex.hbrBackground	= ( HBRUSH )( COLOR_WINDOW + 1 ) ;
    	wcex.lpszMenuName	= NULL ;
    	wcex.lpszClassName	= (LPCWSTR)classname ;
    	wcex.hIconSm		= NULL ;
    
    	if ( !RegisterClassEx ( &wcex ) )
    		return FALSE ;
    
    	hInst = hInstance ; // Store instance handle in our global variable
    
    	hWnd = CreateWindow ( classname, TEXT ("title"), 
    					WS_OVERLAPPEDWINDOW,
    					CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, 
    					NULL, hInstance, NULL ) ;
    	if ( !hWnd )
    	    return FALSE ;
       
    	ShowWindow ( hWnd, nCmdShow ) ;
    	UpdateWindow ( hWnd ) ;
    
    	return TRUE ;
    }
    Last edited by juice; 01-08-2012 at 04:33 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Remove the space between TEXT and ( ... you've made this error in several places... TEXT is a macro not a function call and it's going to be sensitive to the correct spacing. (i.e. the macro's name is TEXT( not TEXT )

    This cannot be your complete code... because in order for it to be trying to use CreateWindowExW ... instead of CreateWindowExA you have to have UNICODE defined someplace... also you are not listing any include files... which is going to lead to TONS of errors in windows.

    Also... DO NOT define separate classes for each window. Define one class for the base window in the application then make windows in that class... i.e. using the same class registration.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by CommonTater View Post
    Remove the space between TEXT and ( ... you've made this error in several places... TEXT is a macro not a function call and it's going to be sensitive to the correct spacing. (i.e. the macro's name is TEXT( not TEXT )
    still the same error. The code compiles with MSVC 6.0 and flags error with MSVC 2010..

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why are you not including windows.h and several other necessary headers?

    Ok, I'm going to break ranks and give you some advice here...

    1) Read this and take it to heart... Trust me, you'll be glad you did on the day you have a REAL problem.

    2) While learning... Pick ONE compiler and stick to it. All you are ever going to accomplish with all these different compilers is to confuse the complete heck out of yourself. I generally recommend Pelles C because it is a standards conforming compiler with an IDE and tool chain that has a complete set of Windows development tools, including resource editors which are absent from other free compilers.

    3) Get a good book/tutorial and stay with it end to end. Read every word, make a very deliberate study of it. Manually type up all the exercises and do all the quizes starting from a blank page... when you finish that, THEN you can start branching out and playing around. As it is now I frequently see you trying to do things with roughly 1/4 of the knowedge you actually need to do a proper job... and expecting us to make up for your poor study habits. That simply isn't going to cut it out in the real world. If you ever expect to work as a programmer, you need to foster a very self-sufficient learning style because, as you're going now, you will very quickly burn up everyone's patience and then you will be faced with the reality you've created... that you never actually learned how to program!

    I mean you no personal offense... I am sincerely trying to get you going down the right path here... The one thing I can promise you is that how you're going at this now isn't going to cut it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  2. How to convert string into LPCWSTR
    By krishnampkkm in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 06:02 AM
  3. Replies: 3
    Last Post: 12-06-2006, 05:59 PM
  4. Replies: 9
    Last Post: 07-07-2006, 11:03 AM
  5. cannot convert parameter 1 from 'char *' to 'long'
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2006, 06:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21