mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-14 02:27:40 -04:00
CWE Directory Reorganization
Rearrange directory structure of CWE to be loosely equivalent to the H'uru Plasma repository. Part 1: Movement of directories and files.
This commit is contained in:
666
Sources/Plasma/PubUtilLib/plWinStrBlock/strblock.c
Normal file
666
Sources/Plasma/PubUtilLib/plWinStrBlock/strblock.c
Normal file
@ -0,0 +1,666 @@
|
||||
/**************************************************************************
|
||||
THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY OF
|
||||
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
|
||||
PARTICULAR PURPOSE.
|
||||
|
||||
Copyright 1998 Microsoft Corporation. All Rights Reserved.
|
||||
**************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
|
||||
File: strblock.c
|
||||
|
||||
Description: Implements the functions that manipulate a string block.
|
||||
|
||||
**************************************************************************/
|
||||
|
||||
#include <windows.h>
|
||||
#include "strblock.h"
|
||||
|
||||
|
||||
// The format of string resources is explained below.
|
||||
//
|
||||
// The smallest granularity of string resource that can be loaded/updated is a block.
|
||||
// Each block is identified by an ID, starting with 1. You need to use the block ID
|
||||
// when calling FindResource(), LoadResource(), UpdateResource().
|
||||
//
|
||||
// A string with ID, nStringID, is in the block with ID, nBlockID, given by the following
|
||||
// formula:
|
||||
// nBlockID = (nStringID / 16) + 1; // Note integer division.
|
||||
//
|
||||
// A block of string resource is laid out as follows:
|
||||
// Each block has NO_OF_STRINGS_PER_BLOCK (= 16) strings. Each string is represented as
|
||||
// an ordered pair, (LENGTH, TEXT). The LENGTH is a WORD that specifies the size, in terms
|
||||
// of number of characters, of the string that follows. TEXT follows the LENGTH and is
|
||||
// a sequence of UNICODE characters, NOT terminated by a NULL character.Any TEXT may be of
|
||||
// zero-length, in which case, LENGTH is zero.
|
||||
//
|
||||
// An executable does not have a string table block with ID, nBlockID, if it does not have any
|
||||
// strings with IDs - ((nBlockID - 1) * 16) thru' ((nBlockID * 16) - 1).
|
||||
//
|
||||
// This format is the same for Windows NT, Windows 95 & Windows 98. Yes, strings in a resource
|
||||
// are internally stored in UNICODE format even in Windows 95 & Windows 98.
|
||||
|
||||
|
||||
// Internal data structure format for a string block.
|
||||
// Our block of strings has as an array of UNICODE string pointers.
|
||||
|
||||
typedef struct tagSTRINGBLOCK
|
||||
{
|
||||
UINT nBlockID; // The ID of the block.
|
||||
WORD wLangID; // The language ID.
|
||||
LPWSTR strArray[NO_OF_STRINGS_PER_BLOCK]; // We maintain the strings
|
||||
// internally in UNICODE.
|
||||
} STRINGBLOCK, * PSTRINGBLOCK;
|
||||
|
||||
|
||||
// A thread-specific error number for the last block operation.
|
||||
__declspec(thread) STRBLOCKERR g_strBlockErr = STRBLOCKERR_OK;
|
||||
|
||||
// Set the error code.
|
||||
void SetBlockError( STRBLOCKERR err ) { g_strBlockErr = err; }
|
||||
|
||||
|
||||
// Forward declarations.
|
||||
|
||||
// Create a string block & return the pointer to the block. Return NULL on failure.
|
||||
// Sets the error code.
|
||||
PSTRINGBLOCK CreateBlock( HINSTANCE hInstLib, UINT nBlockID, WORD wLangID );
|
||||
|
||||
// Parse the string block resource pointed at by, pParse, and fill the strings in pStrBlock.
|
||||
BOOL ParseRes( LPVOID pRes, PSTRINGBLOCK pStrBlock );
|
||||
|
||||
// Get the size of the raw string block resource in the given block.
|
||||
DWORD GetResSize( PSTRINGBLOCK pStrBlock );
|
||||
|
||||
// Update a block of string in the specified library.
|
||||
// hUpdate specifies the update-file handle. This handle is returned by the BeginUpdateResource.
|
||||
// pStrBlock contains the new strings.
|
||||
// nBlockID specifies the ID of the block. Use the same block ID as of pStrBlock if this value is -1.
|
||||
// wlangID specifies the language ID of the block. Use the same language ID as of pStrBlock, if this value is 0.
|
||||
// Returns TRUE on success and FALSE on failure.
|
||||
// Sets the error code.
|
||||
BOOL UpdateBlock( HANDLE hUpdate, PSTRINGBLOCK pStrBlock, int nBlockID, WORD wLangID );
|
||||
|
||||
// Use the strings in the block, pStrBloc, and build a buffer whose format matches that of the
|
||||
// string resource block that can be used to update string resource.
|
||||
// pRes points to a buffer that gets filled. It must be large enough to hold the entire block.
|
||||
// To figure out the size needed, call GetResSize().
|
||||
VOID BuildRes( PSTRINGBLOCK pStrBlock, LPVOID pRes );
|
||||
|
||||
|
||||
// Create a string block.
|
||||
|
||||
HSTRBLOCK WINAPI GetStringBlockA( LPCSTR strAppName, UINT nBlockID, WORD wLangID )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = NULL;
|
||||
HINSTANCE hInstLib = NULL;
|
||||
|
||||
hInstLib = LoadLibraryExA(
|
||||
strAppName,
|
||||
NULL,
|
||||
DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE
|
||||
);
|
||||
|
||||
if( NULL == hInstLib )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_APPLOADFAILED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create the block of strings.
|
||||
pStrBlock = CreateBlock(hInstLib, nBlockID, wLangID);
|
||||
|
||||
// Free the library.
|
||||
FreeLibrary(hInstLib);
|
||||
|
||||
if( pStrBlock )
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return (HSTRBLOCK)pStrBlock;
|
||||
}
|
||||
|
||||
|
||||
// Create a string block.
|
||||
|
||||
HSTRBLOCK WINAPI GetStringBlockW( LPCWSTR strAppName, UINT nBlockID, WORD wLangID )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = NULL;
|
||||
HINSTANCE hInstLib = NULL;
|
||||
|
||||
hInstLib = LoadLibraryExW(
|
||||
strAppName,
|
||||
NULL,
|
||||
DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE
|
||||
);
|
||||
|
||||
if( NULL == hInstLib )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_APPLOADFAILED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create the block of strings.
|
||||
pStrBlock = CreateBlock(hInstLib, nBlockID, wLangID);
|
||||
|
||||
// Free the library.
|
||||
FreeLibrary(hInstLib);
|
||||
|
||||
if( pStrBlock )
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return (HSTRBLOCK)pStrBlock;
|
||||
}
|
||||
|
||||
|
||||
BOOL WINAPI DeleteStringBlock( HSTRBLOCK hStrBlock )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
int i;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
for( i = 0; i < NO_OF_STRINGS_PER_BLOCK; i++ )
|
||||
{
|
||||
if( pStrBlock->strArray[i] )
|
||||
free(pStrBlock->strArray[i]);
|
||||
}
|
||||
free( pStrBlock);
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
int WINAPI GetStringLength( HSTRBLOCK hStrBlock, UINT nIndex )
|
||||
{
|
||||
int nLen;
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
if( (nIndex < 0) || (nIndex >= NO_OF_STRINGS_PER_BLOCK) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDINDEX);
|
||||
return -1;
|
||||
}
|
||||
|
||||
nLen = wcslen(pStrBlock->strArray[nIndex]);
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return nLen;
|
||||
}
|
||||
|
||||
|
||||
BOOL WINAPI GetStringA( HSTRBLOCK hStrBlock, UINT nIndex, LPSTR pszText )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return FALSE;
|
||||
}
|
||||
if( (nIndex < 0) || (nIndex >= NO_OF_STRINGS_PER_BLOCK) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDINDEX);
|
||||
return FALSE;
|
||||
}
|
||||
if( NULL == pszText )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_STRINVALID);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if( !WideCharToMultiByte(CP_ACP, 0, pStrBlock->strArray[nIndex], -1, pszText,
|
||||
wcslen(pStrBlock->strArray[nIndex]) + 1, NULL, NULL) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_UNKNOWN);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
BOOL WINAPI GetStringW( HSTRBLOCK hStrBlock, UINT nIndex, LPWSTR pszText )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return FALSE;
|
||||
}
|
||||
if( (nIndex < 0) || (nIndex >= NO_OF_STRINGS_PER_BLOCK) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDINDEX);
|
||||
return FALSE;
|
||||
}
|
||||
if( NULL == pszText )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_STRINVALID);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
wcscpy(pszText, pStrBlock->strArray[nIndex]);
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
BOOL WINAPI SetStringA( HSTRBLOCK hStrBlock, UINT nIndex, LPCSTR pszText )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
int nLen;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return FALSE;
|
||||
}
|
||||
if( (nIndex < 0) || (nIndex >= NO_OF_STRINGS_PER_BLOCK) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDINDEX);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Delete the current string & reallocate a new one..
|
||||
free(pStrBlock->strArray[nIndex]);
|
||||
|
||||
nLen = strlen(pszText) + 1;
|
||||
pStrBlock->strArray[nIndex] = (LPWSTR)malloc( sizeof(WCHAR) * nLen);
|
||||
|
||||
if( NULL == pStrBlock->strArray[nIndex] )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_NOMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if( !MultiByteToWideChar(CP_ACP, 0, pszText, -1, pStrBlock->strArray[nIndex], nLen) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_UNKNOWN);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
BOOL WINAPI SetStringW( HSTRBLOCK hStrBlock, UINT nIndex, LPCWSTR pszText )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
int nLen;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return FALSE;
|
||||
}
|
||||
if( (nIndex < 0) || (nIndex >= NO_OF_STRINGS_PER_BLOCK) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDINDEX);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Delete the current string & reallocate a new one..
|
||||
free(pStrBlock->strArray[nIndex]);
|
||||
nLen = wcslen(pszText) + 1;
|
||||
|
||||
pStrBlock->strArray[nIndex] = (LPWSTR)malloc( sizeof(WCHAR) * nLen);
|
||||
|
||||
if( NULL == pStrBlock->strArray[nIndex] )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_NOMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
wcscpy(pStrBlock->strArray[nIndex], pszText);
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
int WINAPI GetFirstStringID( HSTRBLOCK hStrBlock )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return (pStrBlock->nBlockID - 1) * NO_OF_STRINGS_PER_BLOCK;
|
||||
}
|
||||
|
||||
|
||||
int WINAPI GetBlockID( HSTRBLOCK hStrBlock )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return pStrBlock->nBlockID;
|
||||
}
|
||||
|
||||
|
||||
WORD WINAPI GetBlockLanguage( HSTRBLOCK hStrBlock )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
return pStrBlock->wLangID;
|
||||
}
|
||||
|
||||
|
||||
BOOL WINAPI UpdateStringBlockA( LPCSTR strAppName, HSTRBLOCK hStrBlock, int nBlockID, WORD wLangID )
|
||||
{
|
||||
HANDLE hUpdate;
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
|
||||
hUpdate = BeginUpdateResourceA(strAppName, FALSE);
|
||||
|
||||
if( NULL == hUpdate )
|
||||
{
|
||||
DWORD dwError = GetLastError();
|
||||
|
||||
switch( dwError )
|
||||
{
|
||||
case ERROR_CALL_NOT_IMPLEMENTED:
|
||||
|
||||
SetBlockError(STRBLOCKERR_UPDATENOTIMPLEMENTED);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
SetBlockError(STRBLOCKERR_UPDATEFAILED);
|
||||
break;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Update the resource.
|
||||
if( !UpdateBlock(hUpdate, pStrBlock, nBlockID, wLangID) )
|
||||
{
|
||||
EndUpdateResource(hUpdate, FALSE);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if( !EndUpdateResource(hUpdate, FALSE) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_UPDATEFAILED);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
BOOL WINAPI UpdateStringBlockW( LPCWSTR strAppName, HSTRBLOCK hStrBlock, int nBlockID, WORD wLangID )
|
||||
{
|
||||
HANDLE hUpdate;
|
||||
PSTRINGBLOCK pStrBlock = (PSTRINGBLOCK)hStrBlock;
|
||||
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_INVALIDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
|
||||
hUpdate = BeginUpdateResourceW(strAppName, FALSE);
|
||||
|
||||
if( NULL == hUpdate )
|
||||
{
|
||||
DWORD dwError = GetLastError();
|
||||
|
||||
switch( dwError )
|
||||
{
|
||||
case ERROR_CALL_NOT_IMPLEMENTED:
|
||||
|
||||
SetBlockError(STRBLOCKERR_UPDATENOTIMPLEMENTED);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
SetBlockError(STRBLOCKERR_UPDATEFAILED);
|
||||
break;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Update the resource.
|
||||
if( !UpdateBlock(hUpdate, pStrBlock, nBlockID, wLangID) )
|
||||
{
|
||||
EndUpdateResource(hUpdate, FALSE);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if( !EndUpdateResource(hUpdate, FALSE) )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_UPDATEFAILED);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
STRBLOCKERR WINAPI GetStringBlockError()
|
||||
{
|
||||
return g_strBlockErr;
|
||||
}
|
||||
|
||||
|
||||
// Create a string block & return the pointer to the block. Return NULL on failure.
|
||||
|
||||
PSTRINGBLOCK CreateBlock( HINSTANCE hInstLib, UINT nBlockID, WORD wLangID )
|
||||
{
|
||||
PSTRINGBLOCK pStrBlock;
|
||||
HRSRC hFindRes;
|
||||
HGLOBAL hLoadRes;
|
||||
LPVOID pRes;
|
||||
|
||||
hFindRes = FindResourceEx(hInstLib, RT_STRING, MAKEINTRESOURCE(nBlockID), wLangID);
|
||||
if( NULL == hFindRes )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_RESNOTFOUND);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hLoadRes = LoadResource(hInstLib, hFindRes);
|
||||
if( NULL == hLoadRes )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_LOADRESFAILED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pRes = LockResource(hLoadRes);
|
||||
if( NULL == pRes )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_LOADRESFAILED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create a new string block, fill the strings based on the resource contents.
|
||||
pStrBlock = (PSTRINGBLOCK)malloc(sizeof(STRINGBLOCK));
|
||||
if( NULL == pStrBlock )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_NOMEMORY);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pStrBlock->nBlockID = nBlockID;
|
||||
pStrBlock->wLangID = wLangID;
|
||||
|
||||
if( !ParseRes(pRes, pStrBlock) )
|
||||
{
|
||||
free(pStrBlock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return pStrBlock;
|
||||
}
|
||||
|
||||
|
||||
// Parse the raw string resource, pRes, and build up the string block, pStrBlock.
|
||||
// The parsing illustrates the format of a string block in an executable.
|
||||
|
||||
BOOL ParseRes( LPVOID pRes, PSTRINGBLOCK pStrBlock )
|
||||
{
|
||||
int i, j;
|
||||
int nLen;
|
||||
WCHAR* pParse = (WCHAR *)pRes;
|
||||
|
||||
// There are NO_OF_STRINGS_PER_BLOCK(=16) strings per block.
|
||||
for( i = 0; i < NO_OF_STRINGS_PER_BLOCK; i++ )
|
||||
{
|
||||
nLen = (int)*pParse++; // The length of the string.
|
||||
pStrBlock->strArray[i] = (LPWSTR)malloc((nLen + 1) * sizeof(WCHAR));
|
||||
|
||||
if( NULL == pStrBlock->strArray[i] )
|
||||
{
|
||||
int k;
|
||||
|
||||
for( k = 0; k < i; k++ ) // Free up the memory allocated so far.
|
||||
free(pStrBlock->strArray[k]);
|
||||
SetBlockError(STRBLOCKERR_NOMEMORY);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
for( j = 0; j < nLen; j++ ) // Copy the string.
|
||||
pStrBlock->strArray[i][j] = *pParse++;
|
||||
pStrBlock->strArray[i][j] = 0;
|
||||
}
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
DWORD GetResSize( PSTRINGBLOCK pStrBlock )
|
||||
{
|
||||
DWORD dwResSize = 0;
|
||||
int i = 0;
|
||||
|
||||
for( i = 0; i < NO_OF_STRINGS_PER_BLOCK; i++ )
|
||||
dwResSize += (wcslen(pStrBlock->strArray[i]) + 1);
|
||||
|
||||
return dwResSize * sizeof(WCHAR);
|
||||
}
|
||||
|
||||
|
||||
// Build a raw resource block, pRes, based on our string block, pStrBlock.
|
||||
// The raw resource block may be used to update a string resource.
|
||||
|
||||
VOID BuildRes( PSTRINGBLOCK pStrBlock, LPVOID pRes )
|
||||
{
|
||||
int i, j;
|
||||
int nLen;
|
||||
WCHAR* pParse = (WCHAR *)pRes;
|
||||
|
||||
// There are NO_OF_STRINGS_PER_BLOCK (= 16) strings per block.
|
||||
for( i = 0; i < NO_OF_STRINGS_PER_BLOCK; i++ )
|
||||
{
|
||||
*pParse++ = nLen = wcslen(pStrBlock->strArray[i]);
|
||||
for( j = 0; j < nLen; j++ )
|
||||
*pParse++ = pStrBlock->strArray[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BOOL UpdateBlock( HANDLE hUpdate, PSTRINGBLOCK pStrBlock, int nBlockID, WORD wLangID )
|
||||
{
|
||||
DWORD dwResSize;
|
||||
LPVOID pRes;
|
||||
DWORD dwRet = 0;
|
||||
WORD wLanguageID = (0 == wLangID) ? pStrBlock->wLangID : wLangID;
|
||||
|
||||
// Get the resource length as required by a raw string resource block.
|
||||
dwResSize = GetResSize(pStrBlock);
|
||||
pRes = malloc(dwResSize);
|
||||
if( NULL == pRes )
|
||||
{
|
||||
SetBlockError(STRBLOCKERR_NOMEMORY);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
BuildRes(pStrBlock, pRes);
|
||||
|
||||
if( !UpdateResource(
|
||||
hUpdate,
|
||||
RT_STRING,
|
||||
MAKEINTRESOURCE(((-1 == nBlockID) ? pStrBlock->nBlockID : nBlockID)),
|
||||
wLanguageID,
|
||||
pRes,
|
||||
dwResSize
|
||||
) )
|
||||
{
|
||||
DWORD dwError = GetLastError();
|
||||
|
||||
switch( dwError )
|
||||
{
|
||||
case ERROR_CALL_NOT_IMPLEMENTED:
|
||||
|
||||
SetBlockError(STRBLOCKERR_UPDATENOTIMPLEMENTED);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
SetBlockError(STRBLOCKERR_UPDATEFAILED);
|
||||
break;
|
||||
}
|
||||
|
||||
free(pRes);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
free(pRes);
|
||||
|
||||
SetBlockError(STRBLOCKERR_OK);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
219
Sources/Plasma/PubUtilLib/plWinStrBlock/strblock.h
Normal file
219
Sources/Plasma/PubUtilLib/plWinStrBlock/strblock.h
Normal file
@ -0,0 +1,219 @@
|
||||
/**************************************************************************
|
||||
THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY OF
|
||||
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
|
||||
PARTICULAR PURPOSE.
|
||||
|
||||
Copyright 1998 Microsoft Corporation. All Rights Reserved.
|
||||
**************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
|
||||
File: strblock.h
|
||||
|
||||
Description: Programmer's interface to manipulate a block of string
|
||||
resources. It defines an API with functions to Create,
|
||||
Access, Modify & Destroy a string block.
|
||||
|
||||
**************************************************************************/
|
||||
|
||||
#ifndef _STRBLOCK_H
|
||||
#define _STRBLOCK_H
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
// The number of strings in a block. The string resource is internally
|
||||
// stored as blocks of NO_OF_STRINGS_PER_BLOCK ( = 16) strings. A block
|
||||
// is the smallest granularity for manipulating a string resource.
|
||||
#define NO_OF_STRINGS_PER_BLOCK 16
|
||||
|
||||
// Error codes for operations on a string block.
|
||||
typedef enum {
|
||||
STRBLOCKERR_OK, // The last operation was successful.
|
||||
STRBLOCKERR_NOMEMORY, // The last operation on the block failed due to inadequate memory.
|
||||
STRBLOCKERR_INVALIDINDEX, // The index passed was incorrect.
|
||||
STRBLOCKERR_UPDATEFAILED, // The specified app could not be updated.
|
||||
STRBLOCKERR_APPLOADFAILED, // The specified app could not be loaded.
|
||||
STRBLOCKERR_RESNOTFOUND, // The specified resource could not be located.
|
||||
STRBLOCKERR_LOADRESFAILED, // The specified resource could not be loaded.
|
||||
STRBLOCKERR_INVALIDBLOCK, // The specified block handle is invalid.
|
||||
STRBLOCKERR_STRINVALID, // The string pointer passed was invalid.
|
||||
STRBLOCKERR_UPDATENOTIMPLEMENTED, // UpdateResource not implemented.
|
||||
STRBLOCKERR_UNKNOWN // An unspecified error.
|
||||
} STRBLOCKERR;
|
||||
|
||||
|
||||
// Handle to a block of string. We hide the internal format we use for manipulating
|
||||
// a string resource by providing access to it through a handle.
|
||||
|
||||
DECLARE_HANDLE(HSTRBLOCK);
|
||||
|
||||
|
||||
// Methods to access a string block.
|
||||
|
||||
// Function: HSTRBLOCK WINAPI GetStringBlockA( LPCSTR strAppName, UINT nBlockID,
|
||||
// WORD wLangID );
|
||||
// Purpose: Get the block of string with the specified ID & language from the
|
||||
// specified application (ANSI version).
|
||||
// LPCSTR strAppName: The name of the application.
|
||||
// UINT nBlockID: The ID of the block.
|
||||
// WORD wLangID: The language identifier. You can create a language
|
||||
// identifier using the macro, MAKELANGID.
|
||||
// Returns: Handle to a string block if successful, NULL otherwise.
|
||||
// Comments: This function creates a string block. Call DeleteStringBlock()
|
||||
// when you no longer need the block.
|
||||
HSTRBLOCK WINAPI GetStringBlockA( LPCSTR strAppName, UINT nBlockID, WORD wLangID );
|
||||
|
||||
|
||||
// Function: HSTRBLOCK WINAPI GetStringBlockW( LPCWSTR strAppName, UINT nBlockID,
|
||||
// WORD wLangID );
|
||||
// Purpose: Get the block of string with the specified ID & language from the
|
||||
// specified application (UNICODE version).
|
||||
// LPCWSTR strAppName: The name of the application.
|
||||
// UINT nBlockID: The ID of the block.
|
||||
// WORD wLangID: The language identifier. You can create a language
|
||||
// identifier using the macro, MAKELANGID.
|
||||
// Returns: Handle to a string block if successful, NULL otherwise.
|
||||
// Comments: This function creates a string block. Call DeleteStringBlock()
|
||||
// when you no longer need the block.
|
||||
HSTRBLOCK WINAPI GetStringBlockW( LPCWSTR strAppName, UINT nBlockID, WORD wLangID );
|
||||
|
||||
|
||||
// Function: BOOL WINAPI DeleteStringBlock( HSTRBLOCK hStrBlock );
|
||||
// Purpose: Delete a block of string.
|
||||
// HSTRBLOCK hStrBlock: The handle to the block.
|
||||
// Returns: TRUE if successful, FALSE on failure.
|
||||
// Comments: Call this function when you no longer need a block. Calling this function
|
||||
// frees the memory occupied by the block. Failure to delete a block
|
||||
// results in memory leaks.
|
||||
BOOL WINAPI DeleteStringBlock( HSTRBLOCK hStrBlock );
|
||||
|
||||
|
||||
// Function: int WINAPI GetStringLength( HSTRBLOCK hStrBlock, UINT nIndex );
|
||||
// Purpose: Get the length of a string in the block.
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// UINT nIndex: The zero-based index of the string in the block. The index can be
|
||||
// any value from 0 thru' (NO_OF_STRINGS_PER_BLOCK - 1).
|
||||
// Returns: If successful, the string length in number of characters, excluding the terminating
|
||||
// NULL character; -1 if the function fails.
|
||||
// Comments: Call this function to get the string length before calling GetString.
|
||||
int WINAPI GetStringLength( HSTRBLOCK hStrBlock, UINT nIndex );
|
||||
|
||||
|
||||
// Function: BOOL WINAPI GetStringA( HSTRBLOCK hStrBlock, UINT nIndex, LPSTR pszStr );
|
||||
// Purpose: Get the string at the specified index in the block (ANSI version).
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// UINT nIndex: The zero-based index of the string in the block. The index can be a value
|
||||
// from 0 through (NO_OF_STRINGS_PER_BLOCK - 1).
|
||||
// LPSTR pszStr: The pointer to the memory to which the string requested will be copied.
|
||||
// This memory should be large enough to contain the string.
|
||||
// Returns: TRUE on success, FALSE otherwise.
|
||||
// Comments: Before calling this function, call GetStringLength() to get the string length.
|
||||
BOOL WINAPI GetStringA( HSTRBLOCK hStrBlock, UINT nIndex, LPSTR pszStr );
|
||||
|
||||
|
||||
// Function: BOOL WINAPI GetStringW( HSTRBLOCK hStrBlock, UINT nIndex, LPWSTR pszStr );
|
||||
// Purpose: Get the string at the specified index in the block (UNICODE version).
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// UINT nIndex: The zero-based index of the string in the block. The index can be a value
|
||||
// from 0 through (NO_OF_STRINGS_PER_BLOCK - 1).
|
||||
// LPSTR pszStr: The pointer to the memory to which the string requested will be copied.
|
||||
// This memory should be large enough to contain the string.
|
||||
// Returns: TRUE on success, FALSE otherwise.
|
||||
// Comments: Before calling this function, call GetStringLength() to get the string length.
|
||||
BOOL WINAPI GetStringW( HSTRBLOCK hStrBlock, UINT nIndex, LPWSTR pszStr );
|
||||
|
||||
|
||||
// Function: BOOL WINAPI SetStringA( HSTRBLOCK hStrBlock, UINT nIndex, LPCSTR pszText );
|
||||
// Purpose: Change the string at the specified index in the block (ANSI version).
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// UINT nIndex: The zero-based index of the string in the block. The index can be a value
|
||||
// from 0 through (NO_OF_STRINGS_PER_BLOCK - 1).
|
||||
// LPCSTR pszText: The pointer to the new string.
|
||||
// Returns: TRUE on success, FALSE otherwise.
|
||||
BOOL WINAPI SetStringA( HSTRBLOCK hStrBlock, UINT nIndex, LPCSTR pszText );
|
||||
|
||||
|
||||
// Function: BOOL WINAPI SetStringW( HSTRBLOCK hStrBlock, UINT nIndex, LPCWSTR pszStr );
|
||||
// Purpose: Change the string at the specified index in the block (UNICODE version).
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// UINT nIndex: The zero-based index of the string in the block. The index can be a value
|
||||
// from 0 through (NO_OF_STRINGS_PER_BLOCK - 1).
|
||||
// LPCWSTR pszText: The pointer to the new string.
|
||||
// Returns: TRUE on success, FALSE otherwise.
|
||||
BOOL WINAPI SetStringW( HSTRBLOCK hStrBlock, UINT nIndex, LPCWSTR pszText );
|
||||
|
||||
|
||||
// Function: int WINAPI GetFirstStringID( HSTRBLOCK hStrBlock );
|
||||
// Purpose: Get the ID of the first string in the block.
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// Returns: The ID of the first string in the block. -1 if the function fails.
|
||||
int WINAPI GetFirstStringID( HSTRBLOCK hStrBlock );
|
||||
|
||||
|
||||
// Function: int WINAPI GetBlockID( HSTRBLOCK hStrBlock );
|
||||
// Purpose: Get the ID of the string block. This is the ID of the string block that was
|
||||
// specified when the block was created.
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// Returns: The block ID, if sucessful, or -1 on failure.
|
||||
int WINAPI GetBlockID( HSTRBLOCK hStrBlock );
|
||||
|
||||
|
||||
// Function: WORD WINAPI GetBlockLanguage( HSTRBLOCK hStrBlock );
|
||||
// Purpose: Get the identifier of the language for the string block. This is the language ID
|
||||
// specified when the block was created.
|
||||
// HSTRBLOCK hStrBlock: The handle to the string block.
|
||||
// Returns: The language ID of the block, if successful; 0 on failure.
|
||||
// Comments: For info on language IDs, refer help of MAKELANGID.
|
||||
WORD WINAPI GetBlockLanguage( HSTRBLOCK hStrBlock );
|
||||
|
||||
|
||||
// Function: BOOL WINAPI UpdateStringBlockA( LPCSTR strAppName, HSTRBLOCK hStrBlock,
|
||||
// int nBlockID, WORD wLangID );
|
||||
// Purpose: Update the block of string resource in the specified application
|
||||
// (ANSI version).
|
||||
// LPCTSTR strAppName: The name of the application whose string resource has to be updated.
|
||||
// HSTRBLOCK hStrBlock: The handle to the block that contains the new strings.
|
||||
// int nBlockID: The ID of the block to update in the app. If it is -1, use the same block
|
||||
// ID as that of hStrBlock.
|
||||
// WORD wLangID: The identifier of the language whaose string block has to be updated. If this
|
||||
// value is 0, use the same language as that of hStrBlock.
|
||||
// Returns: TRUE on success, FALSE on failure.
|
||||
BOOL WINAPI UpdateStringBlockA( LPCSTR strAppName, HSTRBLOCK hStrBlock, int nBlockID, WORD wLangID );
|
||||
|
||||
|
||||
// Function: BOOL WINAPI UpdateStringBlockW( LPCWTR strAppName, HSTRBLOCK hStrBlock,
|
||||
// int nBlockID, WORD wLangID );
|
||||
// Purpose: Update the block of string resource in the specified application
|
||||
// (UNICODE version).
|
||||
// LPCTSTR strAppName: The name of the application whose string resource has to be updated.
|
||||
// HSTRBLOCK hStrBlock: The handle to the block that contains the new strings.
|
||||
// int nBlockID: The ID of the block to update in the app. If it is -1, use the same block
|
||||
// ID as that of hStrBlock.
|
||||
// WORD wLangID: The identifier of the language whaose string block has to be updated. If this
|
||||
// value is 0, use the same language as that of hStrBlock.
|
||||
// Returns: TRUE on success, FALSE on failure.
|
||||
BOOL WINAPI UpdateStringBlockW( LPCWSTR strAppName, HSTRBLOCK hStrBlock, int nBlockID, WORD wLangID );
|
||||
|
||||
|
||||
// Function: STRBLOCKERR WINAPI GetStringBlockError();
|
||||
// Purpose: Get the error status of the last string block action.
|
||||
// Returns: The error status of the last operation on the specified block.
|
||||
// Comments: The error code is maintained on a per-thread basis. Multiple threads do not
|
||||
// overwrite each other's last block error code.
|
||||
STRBLOCKERR WINAPI GetStringBlockError();
|
||||
|
||||
|
||||
#ifdef UNICODE
|
||||
#define GetStringBlock GetStringBlockW
|
||||
#define GetString GetStringW
|
||||
#define SetString SetStringW
|
||||
#define UpdateStringBlock UpdateStringBlockW
|
||||
#else
|
||||
#define GetStringBlock GetStringBlockA
|
||||
#define GetString GetStringA
|
||||
#define SetString SetStringA
|
||||
#define UpdateStringBlock UpdateStringBlockA
|
||||
#endif // UNICODE
|
||||
|
||||
#endif // _STRBLOCK_H
|
Reference in New Issue
Block a user