From: Alex Blekhman on
"Giovanni Dicanio" wrote:
> 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."

Cool! I didn't know that LoadString can do that. I checked this
and it indeed brings the pointer to the string resource. The
return value of LoadString is the length of the string resource.

Alex

From: David Wilkinson on
Alex Blekhman wrote:
> Cool! I didn't know that LoadString can do that. I checked this and it
> indeed brings the pointer to the string resource. The return value of
> LoadString is the length of the string resource.

It would be even cooler if Microsoft had provided an interface that did not
violate the type system, and that worked in non-Unicode build also.

--
David Wilkinson
Visual C++ MVP
From: Alexander Grigoriev on
But in this case myBuf should not be TCHAR*, but only WCHAR*

"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
>
>
>


From: Giovanni Dicanio on
"Alexander Grigoriev" <alegr(a)earthlink.net> ha scritto nel messaggio
news:#XLIKm5ZKHA.740(a)TK2MSFTNGP04.phx.gbl...
> But in this case myBuf should not be TCHAR*, but only WCHAR*

Yes, considering the OP's code and the fact that this behaviour of
LoadString is valid only in Unicode builds, I would write something like
this:

<code>

WCHAR * pBuf = NULL;
int x = LoadStringW( 0, IDS_STRING101,
reinterpret_cast<LPWSTR>( &pBuf ), 0 );
if( x )
std::wstring s( pBuf, pBuf + x );

</code>

or better embedd this string loading code in a reusable function something
like this:

<code>

std::wstring LoadStringFromResource(
__in UINT stringID,
__in_opt HINSTANCE instance = NULL )
{
WCHAR * pBuf = NULL;

int len = LoadStringW(
instance,
stringID,
reinterpret_cast<LPWSTR>( &pBuf ), 0 );

if( len )
return std::wstring( pBuf, pBuf + len );
else
return L"";
}

</code>


Giovanni



From: Bo Persson on
Ron Francis wrote:
> "David Lowndes" <DavidL(a)example.invalid> wrote in message
> news:jds4g5pkmcg7g6q1pgsah59rd1218nln69(a)4ax.com...
>>> "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
>
> Ah, there's a catch.
> Added content at MSDN says:
> "The special behaviour when nBufferMax == 0 applies only to
> LoadStringW, and not to LoadStringA." I was compiling with ANSII.
>
> That's a pity as it would have made it simple.
>

The reason of course being that the resource strings are stored as
wide characters.


Bo Persson





First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: cl options
Next: What is the share mode fopen() uses?