473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Windows Programming/C++ problem.

Hello!!

I am just about to break my computer......

Is a Window Programming issue .....compiler errors are as below :

1>------ Build started: Project: Project 78, Configuration: Debug Win32 ------
1>Compiling...
1>TextOut3.cpp
1>c:\documents and settings\seyed\my documents\game course\cpp2\chapter 5 cpp\projects\project 78\project 78\textout3.cpp(57) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char *' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\seyed\my documents\game course\cpp2\chapter 5 cpp\projects\project 78\project 78\textout3.cpp(97) : error C2440: '=' : cannot convert from 'const char [15]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\seyed\my documents\game course\cpp2\chapter 5 cpp\projects\project 78\project 78\textout3.cpp(105) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [15]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\seyed\my documents\game course\cpp2\chapter 5 cpp\projects\project 78\project 78\textout3.cpp(109) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://c:\Documents and Settings\seyed\My Documents\Game Course\CPP2\Chapter 5 CPP\Projects\Project 78\Project 78\Debug\BuildLog.htm"
1>Project 78 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


The entire program :
Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. //=========================================================
  7. // Globals.
  8.  
  9. HWND      ghMainWnd = 0;
  10. HINSTANCE ghAppInst = 0;
  11.  
  12. struct TextObj
  13. {
  14.     string s; // The string object.
  15.     POINT  p; // The position of the string, relative to the
  16.               // upper-left corner of the client rectangle of
  17.               // the window.
  18. };
  19.  
  20. vector<TextObj> gTextObjs;
  21.  
  22. // Step 1: Define and implement the window procedure.
  23. LRESULT CALLBACK 
  24. WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  25. {    
  26.     // Objects for painting.
  27.     HDC hdc = 0;
  28.     PAINTSTRUCT ps;
  29.  
  30.     TextObj to;
  31.  
  32.     switch( msg )
  33.     {    
  34.     // Handle left mouse button click message.
  35.     case WM_LBUTTONDOWN:
  36.         to.s = "Hello, World.";
  37.  
  38.         // Point that was clicked is stored in the lParam.
  39.         to.p.x = LOWORD(lParam);
  40.         to.p.y = HIWORD(lParam);
  41.  
  42.         // Add to our global list of text objects.
  43.         gTextObjs.push_back( to );
  44.  
  45.         InvalidateRect(hWnd, 0, false);
  46.  
  47.         return 0;    
  48.     case WM_PAINT:
  49.         hdc = BeginPaint(hWnd, &ps);
  50.  
  51.         for(int i = 0; i < (signed)gTextObjs.size(); ++i)
  52.             TextOut(
  53.                 hdc, 
  54.                 gTextObjs[i].p.x, 
  55.                 gTextObjs[i].p.y, 
  56.                 gTextObjs[i].s.c_str(),
  57.                             /*ERROR>>>>*/    (int)gTextObjs[i].s.size()); 
  58.  
  59.         EndPaint(hWnd, &ps);
  60.         return 0;
  61.  
  62.     // Handle key down message.
  63.     case WM_KEYDOWN:    
  64.         if( wParam == VK_ESCAPE )
  65.             DestroyWindow(ghMainWnd);
  66.  
  67.         return 0;    
  68.     // Handle destroy window message.
  69.     case WM_DESTROY:     
  70.         PostQuitMessage(0); 
  71.         return 0;    
  72.     }    
  73.     // Forward any other messages we didn't handle to the
  74.     // default window procedure.
  75.     return DefWindowProc(hWnd, msg, wParam, lParam);
  76. }
  77.  
  78. // WinMain: Entry point for a Windows application.
  79. int WINAPI 
  80. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  81.         PSTR cmdLine, int showCmd)
  82. {
  83.     // Save handle to application instance.
  84.     ghAppInst = hInstance;
  85.  
  86.     // Step 2: Fill out a WNDCLASS instance.
  87.     WNDCLASS wc; 
  88.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  89.     wc.lpfnWndProc   = WndProc;
  90.     wc.cbClsExtra    = 0;
  91.     wc.cbWndExtra    = 0;
  92.     wc.hInstance     = ghAppInst;
  93.     wc.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
  94.     wc.hCursor       = ::LoadCursor(0, IDC_ARROW);
  95.     wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
  96.     wc.lpszMenuName  = 0;
  97.     /*ERROR>>>>*/      wc.lpszClassName = "MyWndClassName";
  98.  
  99.     // Step 3: Register the WNDCLASS instance with Windows.
  100.     RegisterClass( &wc );
  101.  
  102.     // Step 4: Create the window, and save handle in globla
  103.     // window handle variable ghMainWnd.
  104.     ghMainWnd = ::CreateWindow("MyWndClassName", "TextOut Example",   
  105.     /*ERROR>>>>*/    WS_OVERLAPPEDWINDOW, 200, 200, 640, 480, 0, 0, ghAppInst, 0);
  106.  
  107.     if(ghMainWnd == 0)
  108.     {
  109.             /*ERROR>>>>*/MessageBox(0, "CreateWindow - Failed", 0, 0);
  110.         return false;
  111.     }
  112.  
  113.     // Step 5: Show and update the window.
  114.     ShowWindow(ghMainWnd, showCmd);
  115.     UpdateWindow(ghMainWnd);
  116.  
  117.     // Step 6: Enter the message loop and don't quit until
  118.     // a WM_QUIT message is received.
  119.     MSG msg;
  120.     ZeroMemory(&msg, sizeof(MSG));
  121.  
  122.     while( GetMessage(&msg, 0, 0, 0) )
  123.     {
  124.         TranslateMessage(&msg);
  125.         DispatchMessage(&msg);
  126.     }
  127.  
  128.     // Return exit code back to operating system.
  129.     return (int)msg.wParam;
  130. }
  131.  
I have indicated lines causing compiler error.


so what the hell is going on here??!!!

some one help!

many thanks.....
Jul 2 '07 #1
1 2271
weaknessforcats
9,208 Expert Mod 8TB
Here's your code that compiles and links:
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. //#include <windows.h>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. //==================================================  =======
  8. // Globals.
  9.  
  10. HWND      ghMainWnd = 0;
  11. HINSTANCE ghAppInst = 0;
  12.  
  13. struct TextObj
  14. {
  15.     wstring s; // The string object.
  16.     POINT  p; // The position of the string, relative to the
  17.               // upper-left corner of the client rectangle of
  18.               // the window.
  19. };
  20.  
  21. vector<TextObj> gTextObjs;
  22.  
  23. // Step 1: Define and implement the window procedure.
  24. LRESULT CALLBACK 
  25. WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  26. {   
  27.     // Objects for painting.
  28.     HDC hdc = 0;
  29.     PAINTSTRUCT ps;
  30.  
  31.     TextObj to;
  32.  
  33.     switch( msg )
  34.     {   
  35.     // Handle left mouse button click message.
  36.     case WM_LBUTTONDOWN:
  37.         to.s = TEXT("Hello, World.");
  38.  
  39.         // Point that was clicked is stored in the lParam.
  40.         to.p.x = LOWORD(lParam);
  41.         to.p.y = HIWORD(lParam);
  42.  
  43.         // Add to our global list of text objects.
  44.         gTextObjs.push_back( to );
  45.  
  46.         InvalidateRect(hWnd, 0, false);
  47.  
  48.         return 0;   
  49.     case WM_PAINT:
  50.         hdc = BeginPaint(hWnd, &ps);
  51.  
  52.         for(int i = 0; i < (signed)gTextObjs.size(); ++i)
  53.             TextOut(
  54.                 hdc, 
  55.                 gTextObjs[i].p.x, 
  56.                 gTextObjs[i].p.y, 
  57.                 gTextObjs[i].s.c_str(),
  58.           //                  /*ERROR>>>>*/    (int)gTextObjs[i].s.size()); 
  59.     gTextObjs[i].s.size()
  60.     ); 
  61.  
  62.         EndPaint(hWnd, &ps);
  63.         return 0;
  64.  
  65.     // Handle key down message.
  66.     case WM_KEYDOWN:    
  67.         if( wParam == VK_ESCAPE )
  68.             DestroyWindow(ghMainWnd);
  69.  
  70.         return 0;   
  71.     // Handle destroy window message.
  72.     case WM_DESTROY:    
  73.         PostQuitMessage(0); 
  74.         return 0;   
  75.     }   
  76.     // Forward any other messages we didn't handle to the
  77.     // default window procedure.
  78.     return DefWindowProc(hWnd, msg, wParam, lParam);
  79. }
  80.  
  81. // WinMain: Entry point for a Windows application.
  82. int WINAPI 
  83. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  84.         PSTR cmdLine, int showCmd)
  85. {
  86.     // Save handle to application instance.
  87.     ghAppInst = hInstance;
  88.  
  89.     // Step 2: Fill out a WNDCLASS instance.
  90.     WNDCLASS wc; 
  91.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  92.     wc.lpfnWndProc   = WndProc;
  93.     wc.cbClsExtra    = 0;
  94.     wc.cbWndExtra    = 0;
  95.     wc.hInstance     = ghAppInst;
  96.     wc.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
  97.     wc.hCursor       = ::LoadCursor(0, IDC_ARROW);
  98.     wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
  99.     wc.lpszMenuName  = 0;
  100.     /*ERROR>>>>*/      wc.lpszClassName = TEXT("MyWndClassName");
  101.  
  102.     // Step 3: Register the WNDCLASS instance with Windows.
  103.     RegisterClass( &wc );
  104.  
  105.     // Step 4: Create the window, and save handle in globla
  106.     // window handle variable ghMainWnd.
  107.     ghMainWnd = ::CreateWindow(TEXT("MyWndClassName"), TEXT("TextOut Example"),   
  108.     /*ERROR>>>>*/   WS_OVERLAPPEDWINDOW, 200, 200, 640, 480, 0, 0, ghAppInst, 0);
  109.  
  110.     if(ghMainWnd == 0)
  111.     {
  112.             /*ERROR>>>>*/MessageBox(0, TEXT("CreateWindow - Failed"), 0, 0);
  113.         return false;
  114.     }
  115.  
  116.     // Step 5: Show and update the window.
  117.     ShowWindow(ghMainWnd, showCmd);
  118.     UpdateWindow(ghMainWnd);
  119.  
  120.     // Step 6: Enter the message loop and don't quit until
  121.     // a WM_QUIT message is received.
  122.     MSG msg;
  123.     ZeroMemory(&msg, sizeof(MSG));
  124.  
  125.     while( GetMessage(&msg, 0, 0, 0) )
  126.     {
  127.         TranslateMessage(&msg);
  128.         DispatchMessage(&msg);
  129.     }
  130.  
  131.     // Return exit code back to operating system.
  132.     return (int)msg.wParam;
  133. }
  134.  
Basicly, you got trapped in UNICODE vs CHAR. Visual Studio.NET has a default of _UNICODE. You could undefine this. However, with Windows it's just easier to use tchar.h. This header has various macros for converting between char and wchar_t.

The error was not on the line you identified but on the line above.

I just added TEXT macros to convert your literals to whar_t and I change the string in your struct to a wstring. Though you could:
Expand|Select|Wrap|Line Numbers
  1. struct TextObj
  2. {
  3.    #ifdef _UNICODE
  4.     wstring s; // The string object.
  5.    #else
  6.     string s; //The string object
  7.    #endif
  8.     POINT  p; // The position of the string, relative to the
  9.               // upper-left corner of the client rectangle of
  10.               // the window.
  11. };
  12.  
  13.  
This is the stdafx.h that I used:
Expand|Select|Wrap|Line Numbers
  1. // Windows Header Files:
  2. #include <windows.h>
  3.  
  4. // C RunTime Header Files
  5. #include <stdlib.h>
  6. #include <malloc.h>
  7. #include <memory.h>
  8. #include <tchar.h>
  9.  
  10.  
  11. // TODO: reference additional headers your program requires here
  12.  
Hope this helps.
Jul 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate concepts between windows os's, Linux, and the...
2
by: Olli Piepponen | last post by:
Hi, I'm having a little problem catching keystrokes under Windows. I did a little research and found that with mscvrt.getch() one can cath a single key that is pressed. However this doesn't work...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
0
by: Christian McArdle | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.os.ms-windows.programmer.win64 This is a formal Request For Discussion (RFD) to create comp.os.ms-windows.programmer.win64 as an unmoderated...
0
by: Christian McArdle | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.os.ms-windows.programmer.win64 This is a formal Request For Discussion (RFD) to create comp.os.ms-windows.programmer.win64 as an unmoderated...
3
by: Christian McArdle | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.os.ms-windows.programmer.64bit This is a formal Request For Discussion (RFD) to create comp.os.ms-windows.programmer.64bit as an unmoderated...
0
by: Patrice de Boisgrollier | last post by:
Hello I'm having trouble under windows 2000 dealing with programming a ping in a Visual C++ 6 environment My problem is that my code is based on a code sampl from Microsoft that is found in...
1
by: ruca | last post by:
Hi I have this problem: I have a web app in two diferent servers to run. It is exactly the same application. In this app I use the Integrated Windows Authentication. So far so good... But the...
7
by: programming | last post by:
Hi all, i have been having trouble with a login script that works on my windows machine, however when i upload it to the Unix server through VPN, the same script won't work! It won't parse...
23
by: Dan Tallent | last post by:
A textbox has a attribute for ReadOnly. This seems like such a simple concept. When a textbox is set to read only the user cannot change the contents of the field. I have been trying to find...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.