Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Resources
Strings
String Reference
Functions
 LoadString Function
LoadString Function

The LoadString function loads a string resource from the executable file associated with a specified module, copies the string into a buffer, and appends a terminating NULL character.

Syntax

int LoadString(      
    HINSTANCE hInstance,     UINT uID,     LPTSTR lpBuffer,     int nBufferMax );

Parameters

hInstance
[in] Handle to an instance of the module whose executable file contains the string resource. To get the handle to the application itself, use GetModuleHandle(NULL).
uID
[in] Specifies the integer identifier of the string to be loaded.
lpBuffer
[out] Pointer to the buffer to receive the string.
nBufferMax
[in] Specifies the size of the buffer, in TCHARs. This refers to bytes for ANSI versions of the function or WCHARs for Unicode versions. The string is truncated and NULL terminated if it is longer than the number of characters specified. If this parameter is zero, then lpBuffer receives a read-only pointer to the resource itself.

Return Value

If the function succeeds, the return value is the number of TCHARs copied into the buffer, not including the terminating NULL character, or zero if the string resource does not exist. To get extended error information, call GetLastError.

Remarks

security note Security Alert  

Using this function incorrectly can compromise the security of your application. Incorrect use includes specifying the wrong size in the nBufferMax parameter. For example, if lpBuffer points to a buffer szBuffer which is declared as TCHAR szBuffer[100], then sizeof(szBuffer) gives the size of the buffer in bytes, which could lead to a buffer overflow for the Unicode version of the function. Buffer overflow situations are the cause of many security problems in applications. In this case, using sizeof(szBuffer)/sizeof(TCHAR) or sizeof(szBuffer)/sizeof(szBuffer[0]) would give the proper size of the buffer.

Windows 95/98/Me: LoadStringW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example

For an example, see Creating a Child Window

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as ANSI and Unicode versions.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
nBufferMax == 0      Igd ... locomotiv   |   Edit   |   Show History
The special behaviour when nBufferMax == 0 applies only to LoadStringW, and not to LoadStringA.
sizeof(szBuffer)/sizeof(szBuffer[0])      Motti Lanzkron   |   Edit   |   Show History
The security warning suggests using sizeof(szBuffer)/sizeof(szBuffer[0]) but _countof is safer and should be used when available.
Tags What's this?: Add a tag
Flag as ContentBug
LoadString can load strings with embedded nulls, but your wrapper function might not      Giovanni Dicanio   |   Edit   |   Show History
Interesting note:

http://blogs.msdn.com/oldnewthing/archive/2009/10/09/9904648.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Loading std::wstring from resources      Giovanni Dicanio   |   Edit   |   Show History
A C++ function to load std::wstring from resources can be found here (it uses the convenient behaviour of nBufferMax == 0).




Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker