   Using DXGetErrorString9 With std::string
  How can I use the DXGetErrorString9() and DXGetErrorDescription9() functions with std::string so I can display errors? This is how I did it with C-style functions:char msg[1024];sprintf(msg, "Error Creating Mesh\n%s\n\n%s", DXGetErrorString9(hr), DXGetErrorDescription(hr));MessageBox(window, msg, "Error", MB_OK);How would I do this using std::string?   
  I see two ways using std::string.char msg[1024];std::string str;sprintf(msg, "Error Creating Mesh\n%s\n\n%s", DXGetErrorString9(hr), DXGetErrorDescription(hr));str = msg;MessageBox(window, str.c_str(), "Error", MB_OK);orstd::string str;str.resize(1024);sprintf((char*)str.c_str(), "Error Creating Mesh\n%s\n\n%s", DXGetErrorString9(hr), DXGetErrorDescription(hr));MessageBox(window, str.c_str(), "Error", MB_OK);The second may or may not work, but it's worth a try.Here is basic_string, std::string is simply std::basic_string<char> using ascii characters as opposed to unicode (wchar_t)(which would be your std::wstring).   ;  You would probably do something like (not tested):std::stringstream ss( "Error Creating Mesh\n" );ss << DXGetErrorString9( hr ) << "\n\n" << DXGetErrorDescription( hr );MessageBox( window, ss.str().c_str(), "Error", MB_OK );or look at boost::format.jfl.   ;Quote:Original post by PumpkinPiemanstd::string str;str.resize(1024);sprintf((char*)str.c_str(), "Error Creating Mesh\n%s\n\n%s", DXGetErrorString9(hr), DXGetErrorDescription(hr));MessageBox(window, str.c_str(), "Error", MB_OK);The second may or may not work, but it's worth a try.You know, c_str() returns a const char * for a reason. What you have there is likely to blow up in your face at some point. You first method was a lot better, even though it uses c-style string manipulation (and is kind of redundant).jfl.   ;Quote:Original post by jflangloisQuote:Original post by PumpkinPiemanstd::string str;str.resize(1024);sprintf((char*)str.c_str(), "Error Creating Mesh\n%s\n\n%s", DXGetErrorString9(hr), DXGetErrorDescription(hr));MessageBox(window, str.c_str(), "Error", MB_OK);The second may or may not work, but it's worth a try.You know, c_str() returns a const char * for a reason. What you have there is likely to blow up in your face at some point. You first method was a lot better, even though it uses c-style string manipulation.jfl.Yeah, I know. It was kind of a long shot thing, I don't know if it would blow up in your face per-say but it would make sense that it would pass a pointer to it's internal character pointer as opposed to making a senseless copy.Your probably right to a certain degreeQuote:The returned C-style string should not be modified, as this could invalidate the pointer to the string, or deleted, as the string has a limited lifetime and is owned by the class string.From what I gather it does pass it's internal pointer, just make sure you don't do anything nasty with it.   ;Quote:Original post by PumpkinPiemanFrom what I gather it does pass it's internal pointer, just make sure you don't do anything nasty with it.Don't be so sure. c_str() is required to return a pointer to a (c-style character) array of size size() + 1 in which the last element is the null character -- in other words, a null-terminated c-string (in the case of std::string). As far as I know, std::string is not required to hold a null-terminated character array as its internal representation. This means that although it might work with one implementation of the standard library, it might not with another.Whether this is the case or not, two other problems arise:1) You are required to reserve space for sprintf() to work with, which defeats the purpose.2) The size() of your string will probably be incorrect.jfl.[Edited by - jflanglois on June 21, 2006 2:13:07 AM];  The [ostringstream] method detailed above by jflanglois is what I've used since "the dawn of time", works grand.   ;Quote:Original post by jflanglois1) You are required to reserve space for sprintf() to work with, which defeats the purpose.2) The size() of your string will probably be incorrect.The first one was illustrated in the first example I gave using resize, I understand that you can't just copy to a char array of unknown size without problems. (Maybe you thought I didn't know this?)Second one from what I see is true, it does give you the incorrect size.Thirdly, it was a suggestion using std::string. If he wanted to use stringstream I would have given him another example. Granted not a good suggestion or one of good practice (which is why I mentioned that I wasn't sure), but it does work to a certain extent for the purposes he outlined. (Besides when the resize happens the string length\size will default to the resize amount. Checking the size and length will yield the resize amount regardless of what is copied in.)Anyway, I'm done with senseless bickering.[Edited by - PumpkinPieman on June 21, 2006 10:41:33 AM];  Here is what I have:#define V_RETURN(x)    { hr = x; if( FAILED(hr)) { ::MessageBox(0, DXGetErrorString(x), _T("ERROR"), 0); DebugBreak(); return hr; } }MessageBox just needs a LPCSTR, which is what these functions return.  No need to make a string from them first.However, if you want to play with them, you can do this:char* str1;char* str2;str1 = DXGetErrorString9(hr);str2 = DXGetErrorDescription(hr);size_t L1, L2;hr = StringCchLength(str1, 1024, &L1;hr = StringCchLength(str2, 1024, &L2;char* errortxt = new char[L1 + L2 + 2 + lengthofotherstuff];sprintf(errortxt, "Error Creating Mesh\n%s\n\n%s", str1, str2);;Quote:Original post by PumpkinPiemanThe first one was illustrated in the first example I gave using resize, I understand that you can't just copy to a char array of unknown size without problems. (Maybe you thought I didn't know this?)I know that you knew this. I was stating that using std::string in this way is incorrect and, in any case, not particularly useful. Just because you are allowed to cast out the const modifier and your standard library implementation works like you would expect is not an excuse for bad code.Quote:Thirdly, it was a suggestion using std::string. If he wanted to use stringstream I would have given him another example.If the OP knew exactly what he wanted, he probably wouldn't be asking about it here. Using std::stringstream is not a far cry from the requirement that std::string be involved, which I understand as a desire to avoid c-style strings and their drawbacks.Quote:Granted not a good suggestion or one of good practice (which is why I mentioned that I wasn't sure), but it does work to a certain extent for the purposes he outlined. (Besides when the resize happens the string length\size will default to the resize amount. Checking the size and length will yield the resize amount regardless of what is copied in.)See above. There is no reason to use std::string in this way; you would be better off sticking with raw arrays.Quote:Anyway, I'm done with senseless bickering.It's not senseless bickering, it's how one learns. I hope you are sure about your suggestion now.jfl.   