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