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