I'm currently writing a game in DirectX without using the MFC structure. I am manually crating the window by including windows.h and manually creating the window by using windows functions.

I am a bit confused when it comes to using strings. I would like to use the CString class, as it seems extremely easy, versatile, and memory-efficient. I have a number of functions that require the LPCSTR type, and I have read the CString class is interchangeable with LPCSTR/const char*. This also seems the best and easiest to work with while coding.

However, it seems CString is only included for MFC projects. In order to use CString, I must include the AFX header, and apparently if I include the windows.h header (which I need to draw my window) I cannot include the AFX header.

I am curious if there is a certian header I can specifically include in order to use the CString class, or if anyone has a written-up header file custom made for the CString class, if I could obtain that and use it in my project. Thanks for the help!

I have tried #include <cstring> which gave me no errors, however when I try to use the CString class is still gives me an error about it being an undeclared identifier.

Recommended Answers

All 8 Replies

>I have tried #include <cstring>
That header has nothing to do with MFC. It's a header with functions and types for working with C-style strings (ie. arrays of char terminated with '\0').

Might I suggest the std::string class? It's easier to work with than CString, and also provides conversions to const char *. Include the <string> header and consult your favorite documentation. The nice thing about std::string is that it's available for any decent C++ compiler.

I have actually tried using the common <string>. However, I have been having problems with conversions...let me further explain my problem.

I am trying to get the directory path using GetCurrentDirectory(DWORD intBufferLength, LPSTR lpBuffer);. Now, what I've read is that CStrings can do this to directly receive a information from this function:

GetCurrentDirectory(myCString.GetLength(), myCString.GetBuffer(myCString.GetLength()));

I would rather do this over creating a LPSTR, using MAX_LENGTH to gather the directory, then input it into a string. Although, that wouldn't be too much of a problem, I do know how to do that.

The true problem lies in this:

My game initializes resources in a \Res\ folder in the same directory as the executable. I would like to gather the current directory information, then add some text onto it in order to load a bitmap. This is what I'm talking about:

// Load the directory into appPath

lpDDSBGSurface = DDLoadBitmap(lpDD, appPath + "\\Res\\Backdrops\\space.bmp");

lpDD is my DirectX variable, and appPath is the path to my directory.

Whenever I try using this with strings I get the following error:

cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'

Now, my guess is because the parameter in my function calls for a const, and I'm trying to add two strings together and use it as the parameter value. However, I do not want to change it as my function requires a const so that the original value has no fear of being changed. I do not want to create an entirely new variable and add the two strings together, then use it as the parameter because that would simply be a hassle as I have multiple textures to load. Also, when I try to do it anyway, I get the same error. My guess is the 'string' type is not a const, or at least the function doesn't recognize it as an equivilant to LPCSTR.

Any ideas? Thanks for the input in advance!


Also, I'm having trouble converting a string to LPSTR. I cannot say:

string myString;
LPSTR myStr = myString;

I get an error about the class myString needing something at the end of it in order to make the assignment, like some member function or something called. Then I tried:

LPSTR newPath = appPath + "\\Res";

And I got an error saying I cannot add two pointers. I understand appPath is a pointer, so I tried using an asterik and add them, and it seemed to compile without errors, yet the newPath variable was empty.

I'm curious how to:
convert from string to LPSTR, I understand I can simply myString = myLPSTR; easily enough, but how can I do it the other way around? All references I've tried to locate are using the CString class, and not the String class, which is why I thought I wanted to use CString instead. How can I convert from String to LPSTR, and how can I add strings and such together? Or, how can I simply use my string type in a fuction that requires an LPSTR? I'm pretty new with strings, so all the help is wonderful. :)

Think I got it now. With a little more digging up, it looks like myString.c_str() returns a c-string character array, which is exactly what functions like this require. Thanks for all the help though!

>Thanks for all the help though!
"Though"? I do believe I told you that std::string provides a conversion to const char * and directed you to your favorite documentation for more details. What part of that answer was insufficient?

No need to get snappy, I said I couldn't find proper documentation (spent a good 2 hours looking) and could only find documentation on the CString class, and not just the String class, where the CString uses something like myString.operator LPSTR

>spent a good 2 hours looking
Not very hard it seems. It took me about three seconds to type "C++ string reference" into google and just about every hit had the information that you were looking for. If I don't give links to reference material then I think you're smart enough (or the information is abundant enough) for you to find it with minimal effort.

On another note, it's very rude to just shrug off help given to you. "Thanks anyway", and all of its variants, has a very insulting tone.

Insulting? No, I did not intend on insulting anyone. Thanks anyway means the exact solution I was looking for was not provided, but some help was. So, since I didn't crown you, you think I'm insulting you, so you snap on me? Excellent moderation.

>So, since I didn't crown you, you think I'm insulting you, so you snap on me?
I know you weren't insulting me, but others might not be so nice about it, so I felt the need to warn you that such a reply might get flames in return. I don't need or want you to "crown" me, so get off your high horse.

>Thanks anyway means the exact solution I was looking for was not provided
Now you have another definition, consider yourself enlightened.

>Excellent moderation.
This has nothing to do with moderation. Believe it or not, my primary function here is to help people with their problems. I helped you, you responded with a statement that could be interpreted as insulting, and I informed you of that interpretation so you could avoid it in the future. If I moderate you, rest assured that there will be no doubt about it.

By the way, since this thread has turned into you bitching at me and me bitching at you, I'm going to moderate it by locking the thread. If you feel this lock has been added in error, please send me a private message with valid reasons for unlocking it.

commented: nice job on handling that thread --alex +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.