473,394 Members | 1,746 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,394 software developers and data experts.

C++ Visual Studios 2003 GUI Help - error C2664: 'TextOutA'

Hi I am creating a program in C++ using visual studios 2003, and for a project I am required to create a application. In this application for testing purposes I am outputing a number to show that a certain key was pressed. I am getting

error C2664: 'TextOutA' : cannot convert parameter 4 from 'std::string' to 'LPCSTR'

As the error.

My code is

Expand|Select|Wrap|Line Numbers
  1.  /*    Trim fat from windows*/ 
  2. #define WIN32_LEAN_AND_MEAN    
  3. #pragma comment(linker, "/subsystem:windows")
  4. /*    Pre-processor directives*/
  5. //#include "stdafx.h"
  6. #include <windows.h>
  7. #include <iostream>
  8. #include <String>
  9. #include <cString>
  10. using namespace std;
  11. /*
  12. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  13. #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  14. */
  15.  
  16. /*    Windows Procedure Event Handler*/
  17. string keyin="0";
  18. void GamePaint(HDC hDc);
  19.  
  20. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  21. {
  22.  
  23.     PAINTSTRUCT ps;
  24.     /*    Device Context*/
  25.     HDC hDC; 
  26.     /*    Text for display*/
  27.     /*    Switch message, condition that is met will execute*/
  28.     string trst;
  29.     switch(message)
  30.     {
  31.         /*    Window is being created*/
  32.         case WM_CREATE: 
  33.             return 0;
  34.             break;
  35.         /*    Window is closing*/
  36.         case WM_DESTROY: 
  37.             PostQuitMessage(0);
  38.             return 0;
  39.             break;
  40.  
  41.         /*    Window needs update*/
  42.         case WM_KEYDOWN:
  43.         {
  44.             // get virtual key code and data bits
  45.             int virtual_code = (int)wParam;
  46.             int key_state = (int)lParam;
  47.  
  48.             // switch on the virtual_key code to be clean
  49.             switch(virtual_code)
  50.             {
  51.             case VK_RIGHT:
  52.                 {
  53.                     keyin="1"; 
  54.                 } break;
  55.             case VK_LEFT: 
  56.                 { 
  57.                     keyin="2";
  58.                 } break;
  59.             case VK_UP: 
  60.                 {
  61.                     keyin="3";
  62.                 } break;
  63.             case VK_DOWN: 
  64.                 {
  65.                     keyin="4";
  66.                 } break;
  67.  
  68.             // more cases...
  69.  
  70.             default: break;
  71.         } // end switch
  72.  
  73.         // tell windows that you processed the message
  74.         return(0);
  75.         } break;
  76.  
  77.         case WM_PAINT: 
  78.             hDC = BeginPaint(hwnd,&ps);
  79.             GamePaint(hDC);
  80.             /*    Set txt color to blue*/
  81.             trst="Hi";
  82.             SetTextColor(hDC, RGB(0,0,255));
  83.             TextOut(hDC,150,150,(trst),2);
  84. //            TextOut(hDC,0,0,keyin,strlen(keyin));
  85.             EndPaint(hwnd, &ps);
  86.             return 0;
  87.             break;
  88.         default:
  89.             break;
  90.     }
  91.     return (DefWindowProc(hwnd,message,wParam,lParam));
  92. }
  93.  
  94.  
  95.  
  96. void GamePaint(HDC hDC)
  97. {
  98.     HPEN hBluePen = CreatePen(PS_SOLID, 2, RGB(0,0,255));
  99.     HBRUSH hPurpleBrush = CreateSolidBrush(RGB(255,0,255));
  100.     HBRUSH hRedBrush = CreateSolidBrush(RGB(255,0,0));
  101.     RECT rect={80,80,129,129};
  102.  
  103. //    HPEN hPen = SelectObject(hDC, hBluePen); 
  104. //    Image image(L"test.jpg");
  105. //    graphics.DrawImage(&image, 60, 10);
  106.  
  107.     MoveToEx(hDC, 10,40,NULL);
  108.     LineTo(hDC, 44, 10);
  109.     LineTo(hDC, 78, 40);
  110.     SelectObject(hDC, hRedBrush);
  111.     Ellipse(hDC, 150, 150, 180, 180);
  112.  
  113.     SelectObject(hDC, hBluePen);
  114.  
  115.     SetTextColor(hDC, RGB(0,0,255));
  116.  
  117.     Rectangle(hDC, 80, 80, 130, 130);
  118.     FillRect(hDC, &rect,hPurpleBrush);
  119.     DeleteObject(hBluePen);
  120.  
  121.  
  122. }
  123. /*    Main function*/
  124. int APIENTRY WinMain(HINSTANCE hInstance,
  125. HINSTANCE hPrevInstance,
  126. LPSTR lpCmdLine,
  127. int nCmdShow)
  128. {
  129.     WNDCLASSEX windowClass;        //window class
  130.     HWND        hwnd;                //window handle
  131.     MSG            msg;                //message
  132.     bool        done;                //flag saying when app is complete
  133.     /*    Fill out the window class structure*/
  134.     windowClass.cbSize = sizeof(WNDCLASSEX);
  135.     windowClass.style = CS_HREDRAW | CS_VREDRAW;
  136.     windowClass.lpfnWndProc = WndProc;
  137.     windowClass.cbClsExtra = 0;
  138.     windowClass.cbWndExtra = 0;
  139.     windowClass.hInstance = hInstance;
  140.     windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  141.     windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  142.     windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  143.     windowClass.lpszMenuName = NULL;
  144.     windowClass.lpszClassName = "MyClass";
  145.     windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
  146.     /*    Register window class*/
  147.     if (!RegisterClassEx(&windowClass))
  148.     {
  149.         return 0;
  150.     }
  151.     /*    Class registerd, so now create window*/
  152.     hwnd = CreateWindowEx(NULL,        //extended style
  153.         "MyClass",            //class name
  154.         "A Real Win App",        //app name
  155.         WS_OVERLAPPEDWINDOW |        //window style
  156.         WS_VISIBLE |
  157.         WS_SYSMENU,
  158.         100,100,            //x/y coords
  159.         400,400,            //width,height
  160.         NULL,                //handle to parent
  161.         NULL,                //handle to menu
  162.         hInstance,            //application instance
  163.         NULL);                //no extra parameter's
  164.     /*    Check if window creation failed*/
  165.     if (!hwnd)
  166.         return 0;
  167.     done = false; //initialize loop condition variable
  168.     /*    main message loop*/
  169.     while(!done)
  170.     {
  171.         PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
  172.         if (msg.message == WM_QUIT) //check for a quit message
  173.         {
  174.             done = true; //if found, quit app
  175.         }
  176.         else
  177.         {
  178.             /*    Translate and dispatch to event queue*/
  179.             TranslateMessage(&msg); 
  180.             DispatchMessage(&msg);
  181.         }
  182.     }
  183.     return msg.wParam;
  184. }
  185.  
  186.  
any help would be appreciated. Thanks
Apr 1 '06 #1
13 8573
Banfa
9,065 Expert Mod 8TB
To start with post code between [code&#93 and [/code&#93 it will make it easier to read.

The problem is that you are using components from 2 different libraries, namely the C++ STL and the Windows 32 bit API(WIN32). The Windows 32 bit API has no knowledge of the C++ STL and vis versa.

In the line

TextOut(hDC,150,150,(trst),2);

you are trying to pass an STL sting object(trst) to WIN32 function, however the WIN32 function is expecting a variable of type LPCSTR (which boils down to const char * if you care to look through the header files).

The string std object no longer deals in C style strings and has no default operator that can convert a string STL object to the C style string pointer. However it does have a function to do the same.

The MFC also has an alternate string class CString which does know about C style strings as it is a helper class made to integrate with the WIN32 API.

You have 3 options
  1. Keep the stl string, where you need to pass it to the WIN32 API used the c_str member function to convert it to a useable type

    TextOut(hDC,150,150,(trst.c_str()),2);
  2. Use a CString instead, it knows how to convert itself to LPCSTR
    CString trst;
    trst = "Hi";
    TextOut(hDC,150,150,trst,2);
  3. Go back to an old fasioned array of characters
    char trst[10];
    strcpy(trst, "Hi");
    TextOut(hDC,150,150,trst,2);
Apr 3 '06 #2
Thank you very much that worked. I am also working on something were I am getting the user input using the arrow keys which moves a circle. The only problems that I am having is that it is not redrawing the screen is just redraws another circule every time so I end up with a line of circles. Any suggestions on how to fix this. You help is much aprechated.
Apr 4 '06 #3
Banfa
9,065 Expert Mod 8TB
Again 3 options
  1. Redraw your oiginal circle in the background colour before drawing the new one.
  2. If this constitues editing then you can draw the circle by inverting the colour of the bits (using xor) to get them back just invert them again. Once you have the position you want draw the circle in the requrired colour.
  3. Probably the proper way is to redraw the screen. Having decided where you want the circle and set the data invalidate the area of the screen that is being updated (if you invalidate the whole screen then it will work but it will be a lot slower) and call UpdateWindow. Handle the WM_PAINT message to actually draw your window. This will also ensure that if another window is place over the top of your window and then removed it will be properly redrawn
Apr 4 '06 #4
I am still having problems with my code so far. It seems like no matter what i do I ehter louse input from the keyboard and the graphics are steady, or i have input from the keyboard and the graphics are all messed up. my code so far is.


Expand|Select|Wrap|Line Numbers
  1.  /*    Trim fat from windows*/ 
  2. #define WIN32_LEAN_AND_MEAN    
  3. #pragma comment(linker, "/subsystem:windows")
  4. /*    Pre-processor directives*/
  5. //#include "stdafx.h"
  6. #include <windows.h>
  7. #include <iostream>
  8. //#include <Gdiplusheaders.h>
  9. //#include <gdiplus.h>
  10.  
  11. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  12. #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  13.  
  14.  
  15. /*    Windows Procedure Event Handler*/
  16. int keyin=0;
  17. int keyin2=0;
  18. int x=0;
  19. int y=0;
  20. int f=0;
  21. void GamePaint(HDC hDc);
  22. int keyma();
  23. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  24. {
  25.  
  26.     PAINTSTRUCT ps;
  27.     /*    Device Context*/
  28.     HDC hDC; 
  29. //    MSG            msg;
  30.     RECT rect; // rectangle of window
  31.     /*    Text for display*/
  32.     /*    Switch message, condition that is met will execute*/
  33.     switch(message)
  34.     {
  35.         /*    Window is being created*/
  36.         case WM_CREATE: 
  37.             return 0;
  38.             break;
  39.         /*    Window is closing*/
  40.         case WM_DESTROY: 
  41.             PostQuitMessage(0);
  42.             return 0;
  43.             break;
  44.         /*    Window needs update*/
  45.         case WM_KEYDOWN:
  46.         {
  47.             // get virtual key code and data bits
  48.             int virtual_code = (int)wParam;
  49.             int key_state = (int)lParam;
  50.  
  51.             // switch on the virtual_key code to be clean
  52.             if (KEYDOWN(VK_ESCAPE))
  53.                 SendMessage(hwnd,WM_CLOSE,0,0);
  54.  
  55.             switch(virtual_code)
  56.             {
  57.             case VK_RIGHT:
  58.                 {
  59.                     keyin2=5;
  60.                     keyin=0;
  61.                 } break;
  62.             case VK_LEFT: 
  63.                 { 
  64.                     keyin2=-5;
  65.                     keyin=0;
  66.                 } break;
  67.             case VK_UP: 
  68.                 {
  69.                     keyin=-5;
  70.                     keyin2=0;
  71.                 } break;
  72.             case VK_DOWN: 
  73.                 {
  74.                     keyin=5;
  75.                     keyin2=0;
  76.                 } break;
  77.             // more cases...
  78.  
  79.             default: break;
  80.         } // end switch
  81.  
  82.         // tell windows that you processed the message
  83.         return(0);
  84.         } break;
  85.         case WM_PAINT:
  86.              {
  87.  
  88.                 // simply validate the window
  89.                 UpdateWindow(hwnd);
  90.                 hDC = GetDC(hwnd);
  91.  
  92.                 // you would do all your painting here
  93.                 GamePaint(hDC);
  94.  
  95.                 ReleaseDC(hwnd,hDC);
  96.  
  97.  
  98.             /*
  99.                 // get client rectangle of window – use Win32 call
  100.                 GetClientRect(hwnd,&rect);
  101.                 // validate window
  102.                 ValidateRect(hwnd,&rect);
  103.         */
  104.                 // return success
  105.                 return(0);
  106.             } break;
  107.         default:
  108.             break;
  109.     }
  110.     return (DefWindowProc(hwnd,message,wParam,lParam));
  111. }
  112.  
  113.  
  114. void GamePaint(HDC hDC)
  115. {
  116.     HPEN hBluePen = CreatePen(PS_SOLID, 2, RGB(0,0,255));
  117.     HBRUSH hPurpleBrush = CreateSolidBrush(RGB(255,0,255));
  118.     HBRUSH hRedBrush = CreateSolidBrush(RGB(255,0,0));
  119.     RECT rect={80,80,129,129};
  120.  
  121.     x=x+keyin2;
  122.     y=y+keyin;
  123.     keyin=0;
  124.     keyin2=0;
  125.  
  126.  
  127.     MoveToEx(hDC, 10,40,NULL);
  128.     LineTo(hDC, 44, 10);
  129.     LineTo(hDC, 78, 40);
  130.     SelectObject(hDC, hRedBrush);
  131.     Ellipse(hDC, x, y, x+10, y+10);
  132.  
  133.     SelectObject(hDC, hBluePen);
  134.  
  135.     SetTextColor(hDC, RGB(0,0,255));
  136.     Rectangle(hDC, 80, 80, 130, 130);
  137.     FillRect(hDC, &rect,hPurpleBrush);
  138.     DeleteObject(hBluePen);
  139.  
  140.     HPEN hBlackPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
  141.             HBRUSH hBlackBrush = CreateSolidBrush(RGB(0,0,0));
  142.  
  143.             SelectObject(hDC, hBlackPen);
  144.             SelectObject(hDC, hBlackBrush);
  145.             Rectangle(hDC, 0, 0, 600, 600);
  146.             FillRect(hDC, &rect, hBlackBrush);
  147. /*
  148.             HPEN hBluePen = CreatePen(PS_SOLID, 2, RGB(0,0,255));
  149.             HBRUSH hPurpleBrush = CreateSolidBrush(RGB(255,0,255));
  150.             HBRUSH hRedBrush = CreateSolidBrush(RGB(255,0,0));
  151.             RECT rect={80,80,129,129};
  152. */
  153.             //Ellipse(hDC, x, y, x+10, y+10);
  154.  
  155.             x=x+keyin2;
  156.             y=y+keyin;
  157.             keyin=0;
  158.             keyin2=0;
  159.  
  160.  
  161.             MoveToEx(hDC, 10,40,NULL);
  162.             LineTo(hDC, 44, 10);
  163.             LineTo(hDC, 78, 40);
  164.             SelectObject(hDC, hRedBrush);
  165.             Ellipse(hDC, x, y, x+10, y+10);
  166.  
  167.             SelectObject(hDC, hBluePen);
  168.  
  169.             SetTextColor(hDC, RGB(0,0,255));
  170.             Rectangle(hDC, 80, 80, 130, 130);
  171.             FillRect(hDC, &rect,hPurpleBrush);
  172.             DeleteObject(hBluePen);
  173.             DeleteObject(hBlackBrush);
  174.             DeleteObject(hBlackPen);
  175.  
  176.  
  177. }
  178.  
  179. /*    Main function*/
  180. int APIENTRY WinMain(HINSTANCE hInstance,
  181. HINSTANCE hPrevInstance,
  182. LPSTR lpCmdLine,
  183. int nCmdShow)
  184. {
  185.     WNDCLASSEX windowClass;        //window class
  186.     HWND        hwnd;                //window handle
  187.     MSG            msg;                //message
  188.     bool        done;                //flag saying when app is complete
  189.     /*    Fill out the window class structure*/
  190.     windowClass.cbSize = sizeof(WNDCLASSEX);
  191.     windowClass.style = CS_HREDRAW | CS_VREDRAW;
  192.     windowClass.lpfnWndProc = WndProc;
  193.     windowClass.cbClsExtra = 0;
  194.     windowClass.cbWndExtra = 0;
  195.     windowClass.hInstance = hInstance;
  196.     windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  197.     windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  198.     windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  199.     windowClass.lpszMenuName = NULL;
  200.     windowClass.lpszClassName = "MyClass";
  201.     windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
  202.     /*    Register window class*/
  203.     if (!RegisterClassEx(&windowClass))
  204.     {
  205.         return 0;
  206.     }
  207.     /*    Class registerd, so now create window*/
  208.     hwnd = CreateWindowEx(NULL,        //extended style
  209.         "MyClass",            //class name
  210.         "Test Program",        //app name
  211.         WS_OVERLAPPEDWINDOW |        //window style
  212.         WS_VISIBLE |
  213.         WS_SYSMENU,
  214.         100,100,            //x/y coords
  215.         400,400,            //width,height
  216.         NULL,                //handle to parent
  217.         NULL,                //handle to menu
  218.         hInstance,            //application instance
  219.         NULL);                //no extra parameter's
  220.     /*    Check if window creation failed*/
  221.     if (!hwnd)
  222.         return 0;
  223.     done = false; //initialize loop condition variable
  224.  
  225.     while(!done)
  226.     {
  227.         PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
  228.         if (msg.message == WM_QUIT) //check for a quit message
  229.         {
  230.             done = true; //if found, quit app
  231.         }
  232.         else
  233.         {
  234.             /*    Translate and dispatch to event queue*/
  235.             TranslateMessage(&msg); 
  236.             DispatchMessage(&msg);
  237.         }
  238.     }
  239.     return msg.wParam;
  240. }
  241.  
Your help would be very much apreasheated. And i did try the suggested all ready. (also i did try and add those tags on to make the code easier to read but i can't seem to get them to work.)
Apr 5 '06 #5
I with regards to the previous post can any give me an answer since i kind of need it by the end of tonight at the latests before i go to plan B. Any help would be great. Thank you in advance.
Apr 6 '06 #6
Banfa
9,065 Expert Mod 8TB
I do not have a lot of time for this because I am in the middle of a software release but a glance at your code suggests problems in WM_PAINT handler

Expand|Select|Wrap|Line Numbers
  1. case WM_PAINT:
  2.              {
  3.  
  4.                 // simply validate the window
  5.                 UpdateWindow(hwnd);
  6.                 hDC = GetDC(hwnd);
  7.  
  8.                 // you would do all your painting here
  9.                 GamePaint(hDC);
  10.  
  11.                 ReleaseDC(hwnd,hDC);
  12.  
  13.  
  14.             /*
  15.                 // get client rectangle of window – use Win32 call
  16.                 GetClientRect(hwnd,&rect);
  17.                 // validate window
  18.                 ValidateRect(hwnd,&rect);
  19.         */
  20.                 // return success
  21.                 return(0);
  22.             } break;
  23.  
  • Don't call UpdateWindow in WM_PAINT because UpdateWindow sends a WM_PAINT message to the window. Call UpdateWindow from the place that you wish to update the window from (your key handers)
  • Don't use GetDC and ReleaseDC within a WM_PAINT message use BeginPaint and EndPaint
.

You dont Invalidate any bits of the screen in the code you have posted which would be required for an update to occur, remember if you are moving an object (like a circle) you need to Invalidate it's starting location and it's ending Location to redraw it properly.
Apr 6 '06 #7
I owuld first of all like to say that i really really apreashate your help with this so far. I don't know if this is a stupid question or not but how do you invalidate the starting location and ending location or is that what updateWindow does. Sorry that I have to bug you again.
Apr 6 '06 #8
Banfa
9,065 Expert Mod 8TB
No problem :D

UpdateWindow redraws the screen immediately, if you don't call it then Windows will call WM_PAINT in the end but you wont get an immediate update.

To invalidate a section of the screen call one of the functions

InvalidateRect
or
InvalidateRgn

These respectively invalidate a rectangle on the screen or a region of the screen. InvalidateRect can also be used to invalidate the whole screen but if you needlessly invalidate more of the screen than is required you will slow down the redraw.

You can call the invalidate functions more than once before calling UpdateWindow, if you do this the generally windows will redraw the smallest rectangle required to encompass all invalidated areas.

So lets assume that you are moving a circle that has a radius of 100 pixels from x=100, y=100 to x=110, y=100 you might do something like the following

Expand|Select|Wrap|Line Numbers
  1.   RECT rect;
  2.  
  3.   // Setup start location rectangle
  4.   rect.top = 100;
  5.   rect.bottom = 100+(100*2); // y position + 2 times the radius
  6.   rect.left = 100;
  7.   rect.right = 100+(100*2); // x position + 2 times the radius
  8.  
  9.   // invalidate the start location
  10.   InvalidateRect( hWnd, &rect, TRUE );
  11.  
  12.   // Setup finish location rectangle
  13.   rect.top = 100;
  14.   rect.bottom = 100+(100*2); // y position + 2 times the radius
  15.   rect.left = 110;
  16.   rect.right = 110+(100*2); // x position + 2 times the radius
  17.  
  18.   // Invalidate the finish location
  19.   InvalidateRect( hWnd, &rect, TRUE );
  20.  
  21.   UpdateWindow( hWnd ); // Redraw the invalidated areas
  22.  
NOTE: I have set the invalidated areas to be erased to the background colour (3rd parameter TRUE), however doing this can cause some flicker and if you can avoid it then the redraw will look smoother.

What I have sometimes done is to create a bitmap in memory the size of the screen and draw onto the bitmap, the the repaint in WM_PAINT just consists of blting from the bitmap onto the screen without the need to erase to background.
Apr 6 '06 #9
Thank you so much that part of the program works. The only other thing is that when I close the program the window that I am using closes fine but the program is still running in the process tab of task manager. So pretty much it closes the window but does not terminate the program so any thoughts would be nice. This one is not as critical to other parts. Thanks for your time.
Apr 7 '06 #10
Banfa
9,065 Expert Mod 8TB
Your message loop is wrong, you should be using GetMessage not PeekMessage. PeekMessage is normally used when you want to create a subsidurary message loop (for instance to keep processing some messages while performing another operation). I used to use PeekMessage a lot back in the days of Windows 2 which was not multi-tasking when I needed to go into a loop but keep the Window working.

Your message loop should look something like

Expand|Select|Wrap|Line Numbers
  1.     BOOL bRet;
  2.  
  3.     while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
  4.     { 
  5.         if (bRet == -1)
  6.         {
  7.             // handle the error and possibly exit
  8.         }
  9.         else
  10.         {
  11.             TranslateMessage(&msg); 
  12.             DispatchMessage(&msg); 
  13.         }
  14.     }
  15.  
Apr 7 '06 #11
I just want to take this time to say thank you for the time you took to help me. Because of your help I was able to complete my project in the way that I had wanted with out having to resort to doing a console project at the least minute.

Thank you very much.

From Challenger
Apr 11 '06 #12
You have 3 options
  1. Keep the stl string, where you need to pass it to the WIN32 API used the c_str member function to convert it to a useable type

    TextOut(hDC,150,150,(trst.c_str()),2);
  2. Use a CString instead, it knows how to convert itself to LPCSTR
    CString trst;
    trst = "Hi";
    TextOut(hDC,150,150,trst,2);
  3. Go back to an old fasioned array of characters
    char trst[10];
    strcpy(trst, "Hi");
    TextOut(hDC,150,150,trst,2);
You can use your stl string with TextOut. TextOut requires a LPCSTR parameter, which is defined as "typedef const CHAR * LPCSTR". So, when you see a LPCSTR parameter, the function basicly just want the address of the first character in your string.

For a stl string, the address of the first character can easily be obtained:
&myString[0];

So, to ouput your string, you could use the following:

Expand|Select|Wrap|Line Numbers
  1. TextOut( hdc, 150, 150, &trst[0], trst.size() );
  2.  
Aug 23 '06 #13
Banfa
9,065 Expert Mod 8TB
You can use your stl string with TextOut.
I never said you couldn't only that you couldn't use it directly.

For a stl string, the address of the first character can easily be obtained:
&myString[0];

So, to ouput your string, you could use the following:

Expand|Select|Wrap|Line Numbers
  1. TextOut( hdc, 150, 150, &trst[0], trst.size() );
  2.  
This is an extremely bad idea because you have made 2 assumptions about the internal working of the stl class string

1. It uses zero terminators to delimit it strings

2. The memory for the string is contiguous

Now these may well be true on your system but they are not required by the standard, what is required is the interface and either or both of these assumptions could false for any given system in which case you code will produce undefined behaviour (and most likely a crash).

However using trst.c_str() as I suggest will not because this function is defined as returning a standard C zero delimited string.
Aug 26 '06 #14

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

Similar topics

1
by: Ken Janke | last post by:
I am trying to understand something about using visual studios .net 2003. I am trying to find out about the headers in visual c++ .net 2003. Do they have to be without .h at the end? Or should I...
19
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours...
4
by: Scott Chang | last post by:
Hi all I copied the following VC++ 6.0 code (written by someone in the website) to my VC++ .Net 2002-Windows XP Pro PC ////****Solution 'dyndllclass' (2 projects)***/// ///***dynapp****(1st...
5
by: John Tenney | last post by:
If I have a windows form (System.Windows.Forms.Form) open and I push the pin on the toolbox to keep the docked window open, Visual Studios 2003 pegs the processor and basically hangs my computer. I...
6
by: John | last post by:
Hi, I am working on a C++ project which has been developed with Visual Studio.Net 2003. When I compile it with Visual Studio 2005, it gives hundred of errors. Two of the strange errors are ...
0
by: AB | last post by:
Hi, I am trying to convert some crystal reports from Paradox to SQL Server in Visual Studios 2005 by setting database location. I am unable to do that for some reports. It displays an error...
10
by: yansong1990 | last post by:
well ok, im pretty new to c++, i have -a lot- of exp in programming in VBA (visual basic for app) for excel to make macros so i picked up a new course for computing which handles c++ only, i...
0
by: jpturn99 | last post by:
Hello The high school I teach at has recently upgraded to Visual Studios 2005. I am trying to run a simple "hello world" project in C++. I have properly followed all of the steps to setting up a...
3
by: entice | last post by:
When I am compiling in visual c++ 2008 express edition I am receiving these two errors main.cpp(224) : error C2664: '_stricmp' : cannot convert parameter 1 from 'WCHAR ' to 'const char *' Types...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.