1*c2c66affSColin Finck #include "precomp.h"
2*c2c66affSColin Finck 
3*c2c66affSColin Finck static INT
LengthOfStrResource(IN HINSTANCE hInst,IN UINT uID)4*c2c66affSColin Finck LengthOfStrResource(IN HINSTANCE hInst,
5*c2c66affSColin Finck                     IN UINT uID)
6*c2c66affSColin Finck {
7*c2c66affSColin Finck     HRSRC hrSrc;
8*c2c66affSColin Finck     HGLOBAL hRes;
9*c2c66affSColin Finck     LPWSTR lpName, lpStr;
10*c2c66affSColin Finck 
11*c2c66affSColin Finck     if (hInst == NULL)
12*c2c66affSColin Finck     {
13*c2c66affSColin Finck         return -1;
14*c2c66affSColin Finck     }
15*c2c66affSColin Finck 
16*c2c66affSColin Finck     /* There are always blocks of 16 strings */
17*c2c66affSColin Finck     lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
18*c2c66affSColin Finck 
19*c2c66affSColin Finck     /* Find the string table block */
20*c2c66affSColin Finck     if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
21*c2c66affSColin Finck         (hRes = LoadResource(hInst, hrSrc)) &&
22*c2c66affSColin Finck         (lpStr = (WCHAR*) LockResource(hRes)))
23*c2c66affSColin Finck     {
24*c2c66affSColin Finck         UINT x;
25*c2c66affSColin Finck 
26*c2c66affSColin Finck         /* Find the string we're looking for */
27*c2c66affSColin Finck         uID &= 0xF; /* position in the block, same as % 16 */
28*c2c66affSColin Finck         for (x = 0; x < uID; x++)
29*c2c66affSColin Finck         {
30*c2c66affSColin Finck             lpStr += (*lpStr) + 1;
31*c2c66affSColin Finck         }
32*c2c66affSColin Finck 
33*c2c66affSColin Finck         /* Found the string */
34*c2c66affSColin Finck         return (int)(*lpStr);
35*c2c66affSColin Finck     }
36*c2c66affSColin Finck     return -1;
37*c2c66affSColin Finck }
38*c2c66affSColin Finck 
39*c2c66affSColin Finck INT
AllocAndLoadString(OUT LPTSTR * lpTarget,IN HINSTANCE hInst,IN UINT uID)40*c2c66affSColin Finck AllocAndLoadString(OUT LPTSTR *lpTarget,
41*c2c66affSColin Finck                    IN HINSTANCE hInst,
42*c2c66affSColin Finck                    IN UINT uID)
43*c2c66affSColin Finck {
44*c2c66affSColin Finck     INT ln;
45*c2c66affSColin Finck 
46*c2c66affSColin Finck     ln = LengthOfStrResource(hInst,
47*c2c66affSColin Finck                              uID);
48*c2c66affSColin Finck     if (ln++ > 0)
49*c2c66affSColin Finck     {
50*c2c66affSColin Finck         (*lpTarget) = (LPTSTR)LocalAlloc(LMEM_FIXED,
51*c2c66affSColin Finck                                          ln * sizeof(TCHAR));
52*c2c66affSColin Finck         if ((*lpTarget) != NULL)
53*c2c66affSColin Finck         {
54*c2c66affSColin Finck             INT Ret;
55*c2c66affSColin Finck             if (!(Ret = LoadString(hInst, uID, *lpTarget, ln)))
56*c2c66affSColin Finck             {
57*c2c66affSColin Finck                 LocalFree((HLOCAL)(*lpTarget));
58*c2c66affSColin Finck             }
59*c2c66affSColin Finck             return Ret;
60*c2c66affSColin Finck         }
61*c2c66affSColin Finck     }
62*c2c66affSColin Finck     return 0;
63*c2c66affSColin Finck }
64*c2c66affSColin Finck 
65*c2c66affSColin Finck DWORD
LoadAndFormatString(IN HINSTANCE hInstance,IN UINT uID,OUT LPTSTR * lpTarget,...)66*c2c66affSColin Finck LoadAndFormatString(IN HINSTANCE hInstance,
67*c2c66affSColin Finck                     IN UINT uID,
68*c2c66affSColin Finck                     OUT LPTSTR *lpTarget,
69*c2c66affSColin Finck                     ...)
70*c2c66affSColin Finck {
71*c2c66affSColin Finck     DWORD Ret = 0;
72*c2c66affSColin Finck     LPTSTR lpFormat;
73*c2c66affSColin Finck     va_list lArgs;
74*c2c66affSColin Finck 
75*c2c66affSColin Finck     if (AllocAndLoadString(&lpFormat,
76*c2c66affSColin Finck                            hInstance,
77*c2c66affSColin Finck                            uID) > 0)
78*c2c66affSColin Finck     {
79*c2c66affSColin Finck         va_start(lArgs, lpTarget);
80*c2c66affSColin Finck         /* let's use FormatMessage to format it because it has the ability to allocate
81*c2c66affSColin Finck            memory automatically */
82*c2c66affSColin Finck         Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
83*c2c66affSColin Finck                             lpFormat,
84*c2c66affSColin Finck                             0,
85*c2c66affSColin Finck                             0,
86*c2c66affSColin Finck                             (LPTSTR)lpTarget,
87*c2c66affSColin Finck                             0,
88*c2c66affSColin Finck                             &lArgs);
89*c2c66affSColin Finck         va_end(lArgs);
90*c2c66affSColin Finck 
91*c2c66affSColin Finck         LocalFree((HLOCAL)lpFormat);
92*c2c66affSColin Finck     }
93*c2c66affSColin Finck 
94*c2c66affSColin Finck     return Ret;
95*c2c66affSColin Finck }
96*c2c66affSColin Finck 
97*c2c66affSColin Finck BOOL
StatusBarLoadAndFormatString(IN HWND hStatusBar,IN INT PartId,IN HINSTANCE hInstance,IN UINT uID,...)98*c2c66affSColin Finck StatusBarLoadAndFormatString(IN HWND hStatusBar,
99*c2c66affSColin Finck                              IN INT PartId,
100*c2c66affSColin Finck                              IN HINSTANCE hInstance,
101*c2c66affSColin Finck                              IN UINT uID,
102*c2c66affSColin Finck                              ...)
103*c2c66affSColin Finck {
104*c2c66affSColin Finck     BOOL Ret = FALSE;
105*c2c66affSColin Finck     LPTSTR lpFormat, lpStr;
106*c2c66affSColin Finck     va_list lArgs;
107*c2c66affSColin Finck 
108*c2c66affSColin Finck     if (AllocAndLoadString(&lpFormat,
109*c2c66affSColin Finck                            hInstance,
110*c2c66affSColin Finck                            uID) > 0)
111*c2c66affSColin Finck     {
112*c2c66affSColin Finck         va_start(lArgs, uID);
113*c2c66affSColin Finck         /* let's use FormatMessage to format it because it has the ability to allocate
114*c2c66affSColin Finck            memory automatically */
115*c2c66affSColin Finck         Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
116*c2c66affSColin Finck                             lpFormat,
117*c2c66affSColin Finck                             0,
118*c2c66affSColin Finck                             0,
119*c2c66affSColin Finck                             (LPTSTR)&lpStr,
120*c2c66affSColin Finck                             0,
121*c2c66affSColin Finck                             &lArgs);
122*c2c66affSColin Finck         va_end(lArgs);
123*c2c66affSColin Finck 
124*c2c66affSColin Finck         if (lpStr != NULL)
125*c2c66affSColin Finck         {
126*c2c66affSColin Finck             Ret = (BOOL)SendMessage(hStatusBar,
127*c2c66affSColin Finck                                     SB_SETTEXT,
128*c2c66affSColin Finck                                     (WPARAM)PartId,
129*c2c66affSColin Finck                                     (LPARAM)lpStr);
130*c2c66affSColin Finck             LocalFree((HLOCAL)lpStr);
131*c2c66affSColin Finck         }
132*c2c66affSColin Finck 
133*c2c66affSColin Finck         LocalFree((HLOCAL)lpFormat);
134*c2c66affSColin Finck     }
135*c2c66affSColin Finck 
136*c2c66affSColin Finck     return Ret;
137*c2c66affSColin Finck }
138*c2c66affSColin Finck 
139*c2c66affSColin Finck BOOL
StatusBarLoadString(IN HWND hStatusBar,IN INT PartId,IN HINSTANCE hInstance,IN UINT uID)140*c2c66affSColin Finck StatusBarLoadString(IN HWND hStatusBar,
141*c2c66affSColin Finck                     IN INT PartId,
142*c2c66affSColin Finck                     IN HINSTANCE hInstance,
143*c2c66affSColin Finck                     IN UINT uID)
144*c2c66affSColin Finck {
145*c2c66affSColin Finck     BOOL Ret = FALSE;
146*c2c66affSColin Finck     LPTSTR lpStr;
147*c2c66affSColin Finck 
148*c2c66affSColin Finck     if (AllocAndLoadString(&lpStr,
149*c2c66affSColin Finck                            hInstance,
150*c2c66affSColin Finck                            uID) > 0)
151*c2c66affSColin Finck     {
152*c2c66affSColin Finck         Ret = (BOOL)SendMessage(hStatusBar,
153*c2c66affSColin Finck                                 SB_SETTEXT,
154*c2c66affSColin Finck                                 (WPARAM)PartId,
155*c2c66affSColin Finck                                 (LPARAM)lpStr);
156*c2c66affSColin Finck         LocalFree((HLOCAL)lpStr);
157*c2c66affSColin Finck     }
158*c2c66affSColin Finck 
159*c2c66affSColin Finck     return Ret;
160*c2c66affSColin Finck }
161*c2c66affSColin Finck 
162*c2c66affSColin Finck 
163*c2c66affSColin Finck INT
GetTextFromEdit(OUT LPTSTR lpString,IN HWND hDlg,IN UINT Res)164*c2c66affSColin Finck GetTextFromEdit(OUT LPTSTR lpString,
165*c2c66affSColin Finck                 IN HWND hDlg,
166*c2c66affSColin Finck                 IN UINT Res)
167*c2c66affSColin Finck {
168*c2c66affSColin Finck     INT len = GetWindowTextLength(GetDlgItem(hDlg, Res));
169*c2c66affSColin Finck     if(len > 0)
170*c2c66affSColin Finck     {
171*c2c66affSColin Finck         GetDlgItemText(hDlg,
172*c2c66affSColin Finck                        Res,
173*c2c66affSColin Finck                        lpString,
174*c2c66affSColin Finck                        len + 1);
175*c2c66affSColin Finck     }
176*c2c66affSColin Finck     else
177*c2c66affSColin Finck         lpString = NULL;
178*c2c66affSColin Finck 
179*c2c66affSColin Finck     return len;
180*c2c66affSColin Finck }
181*c2c66affSColin Finck 
182*c2c66affSColin Finck 
GetError(DWORD err)183*c2c66affSColin Finck VOID GetError(DWORD err)
184*c2c66affSColin Finck {
185*c2c66affSColin Finck     LPVOID lpMsgBuf;
186*c2c66affSColin Finck 
187*c2c66affSColin Finck     if (err == 0)
188*c2c66affSColin Finck         err = GetLastError();
189*c2c66affSColin Finck 
190*c2c66affSColin Finck     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
191*c2c66affSColin Finck                   FORMAT_MESSAGE_FROM_SYSTEM |
192*c2c66affSColin Finck                   FORMAT_MESSAGE_IGNORE_INSERTS,
193*c2c66affSColin Finck                   NULL,
194*c2c66affSColin Finck                   err,
195*c2c66affSColin Finck                   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
196*c2c66affSColin Finck                   (LPTSTR) &lpMsgBuf,
197*c2c66affSColin Finck                   0,
198*c2c66affSColin Finck                   NULL );
199*c2c66affSColin Finck 
200*c2c66affSColin Finck     MessageBox(NULL, (LPCTSTR) lpMsgBuf, _T("Error!"), MB_OK | MB_ICONERROR);
201*c2c66affSColin Finck 
202*c2c66affSColin Finck     LocalFree(lpMsgBuf);
203*c2c66affSColin Finck }
204*c2c66affSColin Finck 
205