-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrblock.c
878 lines (676 loc) · 20.1 KB
/
strblock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/**************************************************************************
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.
**************************************************************************/
#pragma warning( disable: 4995 )
#include "main.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;
}
// Create an empty string block
HSTRBLOCK WINAPI CreateEmptyStringBlock( UINT nBlockID, WORD wLangID )
{
PSTRINGBLOCK pStrBlock = NULL;
WORD i;
pStrBlock = (PSTRINGBLOCK)GlobalAlloc(GMEM_FIXED, sizeof(STRINGBLOCK));
if( NULL == pStrBlock )
{
SetBlockError(STRBLOCKERR_NOMEMORY);
return NULL;
}
pStrBlock->nBlockID = nBlockID;
pStrBlock->wLangID = wLangID;
for ( i = 0; i < NO_OF_STRINGS_PER_BLOCK; i++ ) {
pStrBlock->strArray[ i ] = (LPWSTR)GlobalAlloc(GMEM_FIXED, 1 * sizeof(WCHAR) );
if ( pStrBlock->strArray[ i ] )
pStrBlock->strArray[ i ][ 0 ] = UNICODE_NULL;
}
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] )
GlobalFree(pStrBlock->strArray[i]);
}
GlobalFree( 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 >= NO_OF_STRINGS_PER_BLOCK )
{
SetBlockError(STRBLOCKERR_INVALIDINDEX);
return -1;
}
nLen = lstrlenW(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 >= 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,
lstrlenW(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 >= NO_OF_STRINGS_PER_BLOCK )
{
SetBlockError(STRBLOCKERR_INVALIDINDEX);
return FALSE;
}
if( NULL == pszText )
{
SetBlockError(STRBLOCKERR_STRINVALID);
return FALSE;
}
lstrcpyW(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 >= NO_OF_STRINGS_PER_BLOCK )
{
SetBlockError(STRBLOCKERR_INVALIDINDEX);
return FALSE;
}
// Delete the current string & reallocate a new one..
GlobalFree(pStrBlock->strArray[nIndex]);
nLen = lstrlenA(pszText) + 1;
pStrBlock->strArray[nIndex] = (LPWSTR)GlobalAlloc(GMEM_FIXED, 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 >= NO_OF_STRINGS_PER_BLOCK )
{
SetBlockError(STRBLOCKERR_INVALIDINDEX);
return FALSE;
}
// Delete the current string & reallocate a new one..
GlobalFree(pStrBlock->strArray[nIndex]);
nLen = lstrlenW(pszText) + 1;
pStrBlock->strArray[nIndex] = (LPWSTR)GlobalAlloc(GMEM_FIXED, sizeof(WCHAR) * nLen);
if( NULL == pStrBlock->strArray[nIndex] )
{
SetBlockError(STRBLOCKERR_NOMEMORY);
return FALSE;
}
lstrcpyW(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)GlobalAlloc(GMEM_FIXED, sizeof(STRINGBLOCK));
if( NULL == pStrBlock )
{
SetBlockError(STRBLOCKERR_NOMEMORY);
return NULL;
}
pStrBlock->nBlockID = nBlockID;
pStrBlock->wLangID = wLangID;
if( !ParseRes(pRes, pStrBlock) )
{
GlobalFree(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)GlobalAlloc(GMEM_FIXED, (nLen + 1) * sizeof(WCHAR));
if( NULL == pStrBlock->strArray[i] )
{
int k;
for( k = 0; k < i; k++ ) // Free up the memory allocated so far.
GlobalFree(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 += (lstrlenW(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 = lstrlenW(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;
WORD wLanguageID = (0 == wLangID) ? pStrBlock->wLangID : wLangID;
// Get the resource length as required by a raw string resource block.
dwResSize = GetResSize(pStrBlock);
if ( dwResSize == 32 )
{
// The string block is empty. In this case we'll delete the block altogether
pRes = NULL;
dwResSize = 0;
} else {
// Build the binary resource
pRes = GlobalAlloc(GMEM_FIXED, 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;
}
GlobalFree(pRes);
return FALSE;
}
GlobalFree(pRes);
SetBlockError(STRBLOCKERR_OK);
return TRUE;
}
//++ [exported] ReadResourceString
// ----------------------------------------------------------------------
//+ Example:
// NSutils::ReadResourceString "$INSTDIR\Test.exe" 100 1033
// Pop $0
// ${If} $0 != ""
// ;Success: $0 contains the valid string
// ${Else}
// ;Error
// ${EndIf}
void __declspec(dllexport) ReadResourceString(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store an NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof(TCHAR) * string_size );
if ( pszBuf ) {
TCHAR szPath[MAX_PATH];
int iStringId;
int iStringLang;
/// Param1: Executable file path
*szPath = _T('\0');
if ( popstring( pszBuf ) == 0 ) {
lstrcpyn( szPath, pszBuf, ARRAYSIZE( szPath ));
}
/// Param2: String ID
iStringId = popint();
/// Param3: String Lang
iStringLang = popint();
if ( iStringLang == 0 )
iStringLang = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
/// Read from the string table
*pszBuf = _T('\0');
if ( *szPath && ( iStringId > 0 )) {
WORD iBlockId, iStringIndex;
HSTRBLOCK hBlock;
/// Compute the block ID based on the string ID
/// Note: The string tables are stored in blocks of 16 stings each.
iStringIndex = iStringId % 16;
iBlockId = iStringId / 16 + 1;
hBlock = GetStringBlock( szPath, iBlockId, iStringLang );
if ( hBlock ) {
GetString( hBlock, iStringIndex, pszBuf );
DeleteStringBlock( hBlock );
}
}
// Return the string (on the stack)
pushstring( pszBuf );
/// Free memory
GlobalFree( pszBuf );
}
}
//++ [exported] WriteResourceString
// ----------------------------------------------------------------------
//+ Example:
// NSutils::WriteResourceString "$INSTDIR\Test.exe" 100 1033 "The string"
// Pop $0
// ${If} $0 != ""
// ;Success: $0 contains the valid string
// ${Else}
// ;Error
// ${EndIf}
void __declspec(dllexport) WriteResourceString(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store an NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof(TCHAR) * string_size );
if ( pszBuf ) {
TCHAR szPath[MAX_PATH];
int iStringId;
int iStringLang;
BOOLEAN bSuccess = FALSE;
/// Param1: Executable file path
*szPath = _T('\0');
if ( popstring( pszBuf ) == 0 ) {
lstrcpyn( szPath, pszBuf, ARRAYSIZE( szPath ));
}
/// Param2: String ID
iStringId = popint();
/// Param3: String Lang
iStringLang = popint();
if ( iStringLang == 0 )
iStringLang = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
/// Param4: The string
*pszBuf = _T('\0');
popstring( pszBuf );
/// Write to string table
if ( *szPath && ( iStringId > 0 )) {
WORD iBlockId, iStringIndex;
HSTRBLOCK hBlock;
ULONG iAttr;
/// Compute the block ID based on the string ID
/// Note: The string tables are stored in blocks of 16 stings each.
iStringIndex = iStringId % 16;
iBlockId = iStringId / 16 + 1;
/// Temporarily remove R/O file attribute
iAttr = GetFileAttributes( szPath );
if ((iAttr != INVALID_FILE_ATTRIBUTES) && (iAttr & FILE_ATTRIBUTE_READONLY))
SetFileAttributes( szPath, iAttr & ~FILE_ATTRIBUTE_READONLY );
/// Read the string block
hBlock = GetStringBlock( szPath, iBlockId, iStringLang );
if ( hBlock ) {
if ( SetString( hBlock, iStringIndex, pszBuf )) {
bSuccess = UpdateStringBlock( szPath, hBlock, iBlockId, iStringLang );
}
} else {
/// The containing string block does not exist. We'll create one
hBlock = CreateEmptyStringBlock( iBlockId, iStringLang );
if ( hBlock ) {
if ( SetString( hBlock, iStringIndex, pszBuf )) {
bSuccess = UpdateStringBlock( szPath, hBlock, iBlockId, iStringLang );
}
}
}
/// Destroy the string block when no longer needed
DeleteStringBlock( hBlock );
/// Restore R/O file attribute
if ((iAttr != INVALID_FILE_ATTRIBUTES) && (iAttr & FILE_ATTRIBUTE_READONLY))
SetFileAttributes( szPath, iAttr );
}
// Return the result on the stack
pushint( bSuccess );
/// Free memory
GlobalFree( pszBuf );
}
}