Prev: cl options
Next: What is the share mode fopen() uses?
From: Ron Francis on 11 Nov 2009 21:51 I'm finally starting to use the string class rather than char* and I'm wondering how I would load an LPSTR via LoadString( ) into a string variable? Regards, Ron Francis www.RonaldFrancis.com
From: Alex Blekhman on 12 Nov 2009 04:10 "Ron Francis" wrote: > I'm finally starting to use the string class rather than char* > and I'm wondering how I would load an LPSTR via LoadString( ) > into a string variable? Do you mean `std::string' string class or something else? In any way, loading strings from resources is pain. There are two common scenarios to accomplish this: 1. Make char/wchar_t buffer big enough, call LoadString and hope for the best. This is most common method because it is the easiest one. However, if string resource is big enough you'll miss some characters. Otherwise, you need to prepare temporary buffer too big for most of the cases, which is waste and ugly. 2. Adopt MFC/ATL CString approach. CString doesn�t call LoadString. It opens the resource with FindResource, LoadResource, etc calls and then figures out how long the string is. Then CString allocates internal buffer of necessary length and copies the resource into it. This approach involves more work, but can handle string resources of any length without additional waste. If you have an opportunity to use CString for your project, then use it, don�t reinvent the wheel. Latest versions of VS made CSTring independent of MFC/ATL, so you can include it in your project with minimal overhead. HTH Alex
From: Ron Francis on 14 Nov 2009 19:19 "Alex Blekhman" <tkfx.REMOVE(a)yahoo.com> wrote in message news:%23SVfIg3YKHA.4992(a)TK2MSFTNGP02.phx.gbl... > "Ron Francis" wrote: >> I'm finally starting to use the string class rather than char* and I'm wondering how I would load >> an LPSTR via LoadString( ) into a string variable? > > Do you mean `std::string' string class or something else? In any way, loading strings from > resources is pain. There are two common scenarios to accomplish this: > > 1. Make char/wchar_t buffer big enough, call LoadString and hope for the best. This is most common > method because it is the easiest one. However, if string resource is big enough you'll miss some > characters. Otherwise, you need to prepare temporary buffer too big for most of the cases, which > is waste and ugly. > > 2. Adopt MFC/ATL CString approach. CString doesn�t call LoadString. It opens the resource with > FindResource, LoadResource, etc calls and then figures out how long the string is. Then CString > allocates internal buffer of necessary length and copies the resource into it. This approach > involves more work, but can handle string resources of any length without additional waste. > > If you have an opportunity to use CString for your project, then use it, don�t reinvent the wheel. > Latest versions of VS made CSTring independent of MFC/ATL, so you can include it in your project > with minimal overhead. > > HTH > Alex Alex, Thanks for the clarification. My apologies, I did mean std::string I have been working with char * for quite some time now, and really I was looking for good reasons to use std::string because it has been recommended so often here. It has some very useful features but often its like trying to fit a square peg into a round hole. LoadString would have been better if it could return the number of characters with a call something like this : LoadString (ghInstance, IDD_STRING, NULL, nBufferMax); (returning in nBufferMax) Your response was much appreciated. Cheers. -- Regards Ron Francis www.RonaldFrancis.com
From: Tim Roberts on 15 Nov 2009 17:35 "Ron Francis" <ronfrancis(a)adam.com.au> wrote: > >Thanks for the clarification. >My apologies, I did mean std::string > >I have been working with char * for quite some time now, and really I was looking for good reasons >to use std::string because it has been recommended so often here. >It has some very useful features but often its like trying to fit a square peg into a round hole. Yes. At the risk of speaking blasphemy, std::string has its place in pure code, but if your program has to interact with the ugly real world, including the Win32 API, then you will find the going much easier if you use CString instead. Much of the philosophy is the same, but then you get to say things like: CString sz; sz.LoadString( IDD_STRING ); -- Tim Roberts, timr(a)probo.com Providenza & Boekelheide, Inc.
From: Ben Voigt [C++ MVP] on 16 Nov 2009 14:40
"Alex Blekhman" <tkfx.REMOVE(a)yahoo.com> wrote in message news:#SVfIg3YKHA.4992(a)TK2MSFTNGP02.phx.gbl... > "Ron Francis" wrote: >> I'm finally starting to use the string class rather than char* and I'm >> wondering how I would load an LPSTR via LoadString( ) into a string >> variable? > > Do you mean `std::string' string class or something else? In any way, > loading strings from resources is pain. There are two common scenarios to > accomplish this: > > 1. Make char/wchar_t buffer big enough, call LoadString and hope for the > best. This is most common method because it is the easiest one. However, > if string resource is big enough you'll miss some characters. Otherwise, > you need to prepare temporary buffer too big for most of the cases, which > is waste and ugly. > > 2. Adopt MFC/ATL CString approach. CString doesn�t call LoadString. It > opens the resource with FindResource, LoadResource, etc calls and then > figures out how long the string is. Then CString allocates internal buffer > of necessary length and copies the resource into it. This approach > involves more work, but can handle string resources of any length without > additional waste. FindResource+LoadResource is not so complicated, one could easily write a wrapper that does this with a std::string instead of CString. > > If you have an opportunity to use CString for your project, then use it, > don�t reinvent the wheel. Latest versions of VS made CSTring independent > of MFC/ATL, so you can include it in your project with minimal overhead. > > HTH > Alex |