Skip to main content
GameDev.net gamedev.net
🔒 Locked

LoadString (Win32)

Started by Yohomyth Dec 20, 2005 at 11:06 PM 1 replies 3.6k views
Original Post
Yohomyth
Yohomyth
Is there a way to find the length of a string resource before loading it? I want to know the size before allocating memory to put the string on so I don't allocate too little or too much. If there is no real way then I'll just make some external string table file DLL.
------------------------------------------------------------"Many combilations elizagerth. I hope you see my particles." - Senor Cardgage
rmsimpson
rmsimpson
Quote:
Original post by KrazeIke
SizeofResource


Not quite so simple. String resources are stored in batches in the executable, and always in UNICODE regardless of whether your app is unicode. SizeofResource() will only give you the length of a batch of strings, but not individual strings. Here's my utility functions to do what you're asking:

Oh, it uses some ATL stuff which you can strip out yourself ... Specifically the USES_CONVERSION, W2T() and A2T() macros.

/** An enhanced version of the Win32 API's LoadString() function.  LoadString() can only handle up to    255 characters in a string resource.  String resources can contain much longer strings than this, but    LoadString() can't.  This function handles any size strings.
If lpszBuff is NULL, LoadStringEx() returns the size of the buffer required to hold the string resource.@param hInst [in]
The module handle of the EXE or DLL which contains the string resource@param wID [in]
The ID of the resource to retrieve@param lpszBuff [out]
The resulting string resource@param uLen [in]
The maximum length of lpszBuff@return LPTSTR
The value pointed to by lpszBuff, or if lpszBuff is NULL, returns the length of the string resource.**/LPTSTR LoadStringEx(HINSTANCE hInst,UINT wID, LPTSTR lpszBuff, int uLen, LPCTSTR pszType){ USES_CONVERSION; UINT block; UINT num = 0; UINT i; HRSRC hRC = NULL; HGLOBAL hgl = NULL; LPWSTR str = NULL; if (lpszBuff) lpszBuff[0] = 0; if (pszType == RT_STRING) { block = (wID >> 4) + 1; // Compute block number. num = wID & 0xF; // Compute offset into block. } else block = wID; hRC = FindResourceEx(hInst,pszType,MAKEINTRESOURCE(block),MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)); hgl = LoadResource(hInst, hRC); str = (LPWSTR)LockResource(hgl); if (str) { if (pszType == RT_STRING) { for (i = 0; i < num; i++) str += *str + 1; if (lpszBuff) lstrcpyn(lpszBuff,W2T(str+1),min(*str+1,uLen)); else lpszBuff = (LPTSTR)*str; } else { num = SizeofResource(hInst, hRC); if (lpszBuff) lstrcpyn(lpszBuff, A2T((LPSTR)str), min(num+1, (UINT)uLen)); else lpszBuff = (LPTSTR)num; } } return lpszBuff;}/** Determines the length of a string resource.@param hInst [in]
The HINSTANCE where the resource can be found@param wID [in]
The resource Id@return int
The length of the string resource.**/int lstrlenrc(HINSTANCE hInst, UINT wID, LPCTSTR pszType){ return (int)LoadStringEx(hInst,wID,NULL,0,pszType);}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.