473,767 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows Programming/C++ problem.

2 New Member
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\cha pter 5 cpp\projects\pr oject 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_cas t, C-style cast or function-style cast
1>c:\documents and settings\seyed\ my documents\game course\cpp2\cha pter 5 cpp\projects\pr oject 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_cas t, C-style cast or function-style cast
1>c:\documents and settings\seyed\ my documents\game course\cpp2\cha pter 5 cpp\projects\pr oject 78\project 78\textout3.cpp (105) : error C2664: 'CreateWindowEx W' : cannot convert parameter 2 from 'const char [15]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cas t, C-style cast or function-style cast
1>c:\documents and settings\seyed\ my documents\game course\cpp2\cha pter 5 cpp\projects\pr oject 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_cas t, C-style cast or function-style cast
1>Build log was saved at "file://c:\Documents and Settings\seyed\ My Documents\Game Course\CPP2\Cha pter 5 CPP\Projects\Pr oject 78\Project 78\Debug\BuildL og.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 2291
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
3014
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 Java language with all of its related technologies. The website is at http://members.dslextreme.com/users/inquirydog, and is a mock-version of the Rosetta stone, which indicates concepts from each "platform"
2
4989
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 when the program is run on the backround and not as the primary task. What I would like to have is a same sort of function that would also work when the program is being run on the background. I'm trying to implement a program that would listen...
383
12229
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 poor man's DBMS, a broken email server and various other /application/ servers to try and crack the Internet and IS markets. In the case where they didn't spend their own money to get companies to
0
1862
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 world-wide Usenet newsgroup dedicated to the discussion of Microsoft Windows 64 bit programming. This is not a Call for Votes (CFV); you cannot vote at this time. Procedural details appear below. All followup discussion should be crossposted to...
0
1591
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 world-wide Usenet newsgroup dedicated to the discussion of Microsoft Windows 64-bit programming. This is not a Call for Votes (CFV); you cannot vote at this time. Procedural details appear below. All followup discussion should be crossposted to...
3
3248
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 world-wide Usenet newsgroup dedicated to the discussion of Microsoft Windows 64-bit programming. This is not a Call for Votes (CFV); you cannot vote at this time. Procedural details appear below. All followup discussion should be crossposted to...
0
1626
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 the MSDN library Under Windows 98 there is no problem and everythin works fine but I have a problem in Windows 2000 and XP as well with the following function : bread = recvfrom(sockRaw
1
294
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 same application have diferent behaviors in both servers. I must say that both servers (or machines) are under the same domain. The problem is that one (server A) of them lets me authenticate and the other no (server B).
7
2068
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 member.txt properly i think. The password and usernames i am using are at the bottom of this post. Each time i go to login on the unix server, it clears the username and password field. I have been attempting to solve the problem, but have been...
23
2226
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 that missing ability for other predefined controls in C#. The radiobutton, checkbox, and combobox controls do not share this ability. I find it difficult to believe that this feature was left out and Microsoft expects everyone to write...
0
9407
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3931
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.