Prev: cl options
Next: What is the share mode fopen() uses?
From: mzdude on 16 Nov 2009 17:41 On Nov 11, 9:51 pm, "Ron Francis" <ronfranci(at)adam.com.au> 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? > > Regards, > Ron Franciswww.RonaldFrancis.com Why wouldn't the following work? We compile with the wide string as the default. If you use the narrow string make the necessary adjustments int main() { TCHAR *myBuf = 0; int x = LoadString(0,IDS_STRING101,(LPWSTR)&myBuf,0); if(x) std::wstring s(myBuf, myBuf+x); }
From: Alex Blekhman on 17 Nov 2009 02:41 "mzdude" wrote: > Why wouldn't the following work? > We compile with the wide string as the default. If you use the > narrow string make the necessary adjustments > > int main() > { > TCHAR *myBuf = 0; > int x = LoadString(0,IDS_STRING101,(LPWSTR)&myBuf,0); > if(x) > std::wstring s(myBuf, myBuf+x); > } It won't work because LoadString won't copy anything into myBuf buffer. LoadString returns the number of chars copied into a buffer or zero in case of error. You need to allocate destination buffer upfront, before calling LoadString. Alex
From: Giovanni Dicanio on 17 Nov 2009 04:23 "Alex Blekhman" <tkfx.REMOVE(a)yahoo.com> ha scritto nel messaggio news:#XkXwl1ZKHA.5976(a)TK2MSFTNGP05.phx.gbl... >> TCHAR *myBuf = 0; >> int x = LoadString(0,IDS_STRING101,(LPWSTR)&myBuf,0); >> if(x) >> std::wstring s(myBuf, myBuf+x); [...] > It won't work because LoadString won't copy anything into myBuf buffer. > LoadString returns the number of chars copied into a buffer or zero in > case of error. You need to allocate destination buffer upfront, before > calling LoadString. Alex: I'm not sure because I have not tested that kind of code, but I think that can be correct. In fact, I read in LoadString MSDN documentation: LoadString Function http://msdn.microsoft.com/en-us/library/ms647486%28VS.85%29.aspx about 'nBufferMax' parameter: "If this parameter is zero, then lpBuffer receives a read-only pointer to the resource itself." Giovanni
From: Ron Francis on 17 Nov 2009 04:55 "Giovanni Dicanio" <giovanniDOTdicanio(a)REMOVEMEgmail.com> wrote in message news:uT7Uqf2ZKHA.4920(a)TK2MSFTNGP04.phx.gbl... > "Alex Blekhman" <tkfx.REMOVE(a)yahoo.com> ha scritto nel messaggio > news:#XkXwl1ZKHA.5976(a)TK2MSFTNGP05.phx.gbl... > >>> TCHAR *myBuf = 0; >>> int x = LoadString(0,IDS_STRING101,(LPWSTR)&myBuf,0); >>> if(x) >>> std::wstring s(myBuf, myBuf+x); > [...] >> It won't work because LoadString won't copy anything into myBuf buffer. LoadString returns the >> number of chars copied into a buffer or zero in case of error. You need to allocate destination >> buffer upfront, before calling LoadString. > > Alex: I'm not sure because I have not tested that kind of code, but I think that can be correct. > > In fact, I read in LoadString MSDN documentation: > > LoadString Function > http://msdn.microsoft.com/en-us/library/ms647486%28VS.85%29.aspx > > about 'nBufferMax' parameter: > > "If this parameter is zero, then lpBuffer receives a read-only pointer to the resource itself." > > Giovanni > Yes, it does say that! But I couldn't get it to work. Regards Ron Francis www.RonaldFrancis.com
From: David Lowndes on 17 Nov 2009 04:55 >"If this parameter is zero, then lpBuffer receives a read-only pointer to >the resource itself." I was surprised to notice that comment as well - I've never been aware of it before. I've tried it and it does indeed work! Dave
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: cl options Next: What is the share mode fopen() uses? |