1 /*
2 * PROJECT: ReactOS VGA Font Editor
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Some miscellaneous resource functions
5 * COPYRIGHT: Copyright 2006-2007 Thomas Weidenmueller (thomas@reactsoft.com)
6 * Copyright 2008 Colin Finck (colin@reactos.org)
7 */
8
9 #include "precomp.h"
10
11 static INT
LengthOfStrResource(IN UINT uID)12 LengthOfStrResource(IN UINT uID)
13 {
14 HRSRC hrSrc;
15 HGLOBAL hRes;
16 LPWSTR lpName, lpStr;
17
18 /* There are always blocks of 16 strings */
19 lpName = (LPWSTR)MAKEINTRESOURCEW((uID >> 4) + 1);
20
21 /* Find the string table block */
22 if ((hrSrc = FindResourceW(hInstance, lpName, (LPWSTR)RT_STRING)) &&
23 (hRes = LoadResource(hInstance, hrSrc)) &&
24 (lpStr = LockResource(hRes)))
25 {
26 UINT x;
27
28 /* Find the string we're looking for */
29 uID &= 0xF; /* position in the block, same as % 16 */
30 for (x = 0; x < uID; x++)
31 {
32 lpStr += (*lpStr) + 1;
33 }
34
35 /* Found the string */
36 return (int)(*lpStr);
37 }
38 return -1;
39 }
40
41 int
AllocAndLoadString(OUT LPWSTR * lpTarget,IN UINT uID)42 AllocAndLoadString(OUT LPWSTR *lpTarget,
43 IN UINT uID)
44 {
45 INT ln;
46
47 ln = LengthOfStrResource(uID);
48 if (ln++ > 0)
49 {
50 (*lpTarget) = (LPWSTR)LocalAlloc(LMEM_FIXED,
51 ln * sizeof(WCHAR));
52 if ((*lpTarget) != NULL)
53 {
54 INT Ret;
55 if (!(Ret = LoadStringW(hInstance, uID, *lpTarget, ln)))
56 {
57 LocalFree((HLOCAL)(*lpTarget));
58 }
59 return Ret;
60 }
61 }
62 return 0;
63 }
64
65 static DWORD
VarListLoadAndFormatString(IN UINT uID,OUT PWSTR * lpTarget,IN va_list * Args)66 VarListLoadAndFormatString(IN UINT uID, OUT PWSTR *lpTarget, IN va_list* Args)
67 {
68 DWORD Ret = 0;
69 PWSTR lpFormat;
70
71 if (AllocAndLoadString(&lpFormat, uID) > 0)
72 {
73 /* let's use FormatMessage to format it because it has the ability to allocate
74 memory automatically */
75 Ret = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
76 lpFormat,
77 0,
78 0,
79 (LPWSTR)lpTarget,
80 0,
81 Args);
82
83 HeapFree(hProcessHeap, 0, lpFormat);
84 }
85
86 return Ret;
87 }
88
89 DWORD
LoadAndFormatString(IN UINT uID,OUT PWSTR * lpTarget,...)90 LoadAndFormatString(IN UINT uID, OUT PWSTR *lpTarget, ...)
91 {
92 DWORD Ret;
93 va_list Args;
94
95 va_start(Args, lpTarget);
96 Ret = VarListLoadAndFormatString(uID, lpTarget, &Args);
97 va_end(Args);
98
99 return Ret;
100 }
101
102 VOID
LocalizedError(IN UINT uID,...)103 LocalizedError(IN UINT uID, ...)
104 {
105 PWSTR pszError;
106 va_list Args;
107
108 va_start(Args, uID);
109 VarListLoadAndFormatString(uID, &pszError, &Args);
110 va_end(Args);
111
112 MessageBoxW(NULL, pszError, szAppName, MB_ICONERROR);
113 HeapFree(hProcessHeap, 0, pszError);
114 }
115