void ShowText(int p_iStringHandle)
{
char c_Buffer[25];
for(int i = 0; i < sizeof(c_Buffer); i++)
{
c_Buffer = '_';
}
//Set text attributes - color, background, mode, and allignment
SetTextColor(g_hMainDC, RGB(255, 255, 255)); //color - white
SetBkColor(g_hMainDC, RGB(0, 0, 0)); //background - black
SetBkMode(g_hMainDC, TRANSPARENT); //transparent
SetTextAlign(g_hMainDC, TA_LEFT | TA_TOP | TA_UPDATECP); //allignment
//Load the string
//NOTE: p_iStringHandle is the handle found within the stringtable in the .rc file
switch(p_iStringHandle)
{
/*HINSTANCE hInstance = GetModuleHandle(NULL);*/
case 2:
LoadString(GetModuleHandle(NULL), p_iStringHandle, c_Buffer, sizeof(c_Buffer));
break;
case 3:
LoadString(GetModuleHandle(NULL), p_iStringHandle, c_Buffer, sizeof(c_Buffer));
break;
}
//Display the Text
TextOut(g_hMainDC, NULL, NULL, c_Buffer, sizeof(c_Buffer));
}
(Win32 API) Displaying Text string issue
I’m currently in the process of learning Win32 programming using the windows API. The main tutorial I am using is Game Programming Genesis by Joseph Farrel found here on GameDev.
Here’s my set up (note: this will most likely make much more sense when you look at the code at the end of this post):
I load a string from a stringtable located within an .rc file. When I load the string it needs to go into a buffer (I’m sure you know this but I’ll say it anyway just in case). As I was told, it goes into a char array. Thing is, the strings in the stringtable aren’t the same size as the array (they are smaller). So when c_Buffer is displayed, it shows up as [string from stringtable]|____________. If c_Buffer isn’t initialized within a for loop, it shows up as: [stirng from stringtable]||||||||||||||||||.
My problem is that I would like to get ride of those extra characters (____ & |||||) that come after the sought after string. I’ve tried using a simple string variable from <string.h> but the compiler wasn’t having any of that because of conflicting data types.
I’ve looked through MSDN for awhile, but I’ve yet to find something that would help with this situation. So I turn to the GameDev community (as I always due with a programming problem).
If anyone has any ideas on what they suggest I should do, I would appreciate to hear them. Thanks for your time.
Code (C/C++):
main.cpp – well the function to show the text anyway
If you wish for me to post the .rc and resource.h files I will.
Take care.
The first problem here is that you haven't been properly introduced to the standard library provided by C++, which includes a string type. I suggest you do some research on std::string, as it will be much easier to use than raw char arrays.
For an answer more closely related to your problem, I see two things here. First, you are using sizeof(c_Buffer) for passing the length of your string. This may work in your individual case, but it is not gauranteed to, since the functions you are calling want the length in characters, not bytes. So if you happen to run on a system that does not treat each character as a byte, you will run into issues.
Second, the length of a string is not determined by the length of the containing char array, but by the location of a NULL character. Setting any character in the array to NULL effectively terminates the string at that point. In your case, you can simply set each character of c_Buffer to NULL instead of '_', and then when the LoadString method has finished filling your buffer, the extra NULL characters will terminate the string for you.
However, you should really look into using std::string whenever possible.
For an answer more closely related to your problem, I see two things here. First, you are using sizeof(c_Buffer) for passing the length of your string. This may work in your individual case, but it is not gauranteed to, since the functions you are calling want the length in characters, not bytes. So if you happen to run on a system that does not treat each character as a byte, you will run into issues.
Second, the length of a string is not determined by the length of the containing char array, but by the location of a NULL character. Setting any character in the array to NULL effectively terminates the string at that point. In your case, you can simply set each character of c_Buffer to NULL instead of '_', and then when the LoadString method has finished filling your buffer, the extra NULL characters will terminate the string for you.
However, you should really look into using std::string whenever possible.
Thanks for the reply Mike. I'll look further into the std::string library. As stated before, I tried using the string type from std::string, but I ran into issues reguarding converting problems from string type to...I think it was something like LPCTSTR or LPSTR.
As for setting all of the different elements of c_Buffer to NULL, that hasn't seemed to do much. I still get [desired string]||||||||||||| displayed on the screen.
Reguarding your comment about using sizeof(), I don't know which other function to use. I tried to use strlen() but I get an error during runtime stating that c_Buffer has been corrupted. Any suggestions?
Thanks again for the speedy reply Mike.
EDIT: I just tried to use the string datatype. I just made c_Buffer to the type string. Here are the errors I'm getting from the compiler (which is by the way Visual C++ 2008 Express):
main.cpp(200) : error C2664: 'LoadStringA' : cannot convert parameter 3 from 'std::string' to 'LPSTR'
main.cpp(204) : error C2664: 'LoadStringA' : cannot convert parameter 3 from 'std::string' to 'LPSTR'
main.cpp(209) : error C2664: 'TextOutA' : cannot convert parameter 4 from 'std::string' to 'LPCSTR'
As for setting all of the different elements of c_Buffer to NULL, that hasn't seemed to do much. I still get [desired string]||||||||||||| displayed on the screen.
Reguarding your comment about using sizeof(), I don't know which other function to use. I tried to use strlen() but I get an error during runtime stating that c_Buffer has been corrupted. Any suggestions?
Thanks again for the speedy reply Mike.
EDIT: I just tried to use the string datatype. I just made c_Buffer to the type string. Here are the errors I'm getting from the compiler (which is by the way Visual C++ 2008 Express):
main.cpp(200) : error C2664: 'LoadStringA' : cannot convert parameter 3 from 'std::string' to 'LPSTR'
main.cpp(204) : error C2664: 'LoadStringA' : cannot convert parameter 3 from 'std::string' to 'LPSTR'
main.cpp(209) : error C2664: 'TextOutA' : cannot convert parameter 4 from 'std::string' to 'LPCSTR'
You can't use std::string with LoadString, you'll have to resort to using char arrays (or allocated pointers) for that. You can however use the method .c_str() to get a const char pointer to the string data which you can pass to TextOutA.
Also, if you use char arrays, NEVER use sizeof for the length of the string, always strlen.
Something along this lines:
Also, if you use char arrays, NEVER use sizeof for the length of the string, always strlen.
Something along this lines:
char Buffer[32];std::string Text;memset( Buffer, 0, sizeof( Buffer ) );LoadString( GetModuleHandle( NULL ), p_iStringHandle, c_Buffer, sizeof( c_Buffer ) );// the following will only work cleanly if the string is zero terminated (it should be)Text = Buffer;TextOutA( g_hMainDC, NULL, NULL, Text.c_str(), (int)Text.length() );
Well I'll be, that did the trick Endurion. I'll have to remember about c_str(). I'm guessing it comes in handy in other places. memset() is a new one for me. Much better than using a for loop to initialize a char array. ^_^
Thanks again Endurion and Mike for the help. It's much appreciated. Expect more issues from me in the future. :)
Take care.
Thanks again Endurion and Mike for the help. It's much appreciated. Expect more issues from me in the future. :)
Take care.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement