1 // Windows/ResourceString.cpp
2 
3 #include "StdAfx.h"
4 
5 #ifndef _UNICODE
6 #include "../Common/StringConvert.h"
7 #endif
8 
9 #include "ResourceString.h"
10 
11 extern HINSTANCE g_hInstance;
12 #ifndef _UNICODE
13 extern bool g_IsNT;
14 #endif
15 
16 namespace NWindows {
17 
18 #ifndef _UNICODE
19 
MyLoadStringA(HINSTANCE hInstance,UINT resourceID)20 static CSysString MyLoadStringA(HINSTANCE hInstance, UINT resourceID)
21 {
22   CSysString s;
23   int size = 128;
24   int len;
25   do
26   {
27     size <<= 1;
28     len = ::LoadString(hInstance, resourceID, s.GetBuf(size - 1), size);
29   }
30   while (size - len <= 1);
31   s.ReleaseBuf_CalcLen(len);
32   return s;
33 }
34 
35 #endif
36 
37 static const int kStartSize = 256;
38 
MyLoadString2(HINSTANCE hInstance,UINT resourceID,UString & s)39 static void MyLoadString2(HINSTANCE hInstance, UINT resourceID, UString &s)
40 {
41   int size = kStartSize;
42   int len;
43   do
44   {
45     size <<= 1;
46     len = ::LoadStringW(hInstance, resourceID, s.GetBuf(size - 1), size);
47   }
48   while (size - len <= 1);
49   s.ReleaseBuf_CalcLen(len);
50 }
51 
52 // NT4 doesn't support LoadStringW(,,, 0) to get pointer to resource string. So we don't use it.
53 
MyLoadString(UINT resourceID)54 UString MyLoadString(UINT resourceID)
55 {
56   #ifndef _UNICODE
57   if (!g_IsNT)
58     return GetUnicodeString(MyLoadStringA(g_hInstance, resourceID));
59   else
60   #endif
61   {
62     {
63       wchar_t s[kStartSize];
64       s[0] = 0;
65       int len = ::LoadStringW(g_hInstance, resourceID, s, kStartSize);
66       if (kStartSize - len > 1)
67         return s;
68     }
69     UString dest;
70     MyLoadString2(g_hInstance, resourceID, dest);
71     return dest;
72   }
73 }
74 
MyLoadString(HINSTANCE hInstance,UINT resourceID,UString & dest)75 void MyLoadString(HINSTANCE hInstance, UINT resourceID, UString &dest)
76 {
77   dest.Empty();
78   #ifndef _UNICODE
79   if (!g_IsNT)
80     MultiByteToUnicodeString2(dest, MyLoadStringA(hInstance, resourceID));
81   else
82   #endif
83   {
84     {
85       wchar_t s[kStartSize];
86       s[0] = 0;
87       int len = ::LoadStringW(hInstance, resourceID, s, kStartSize);
88       if (kStartSize - len > 1)
89       {
90         dest = s;
91         return;
92       }
93     }
94     MyLoadString2(hInstance, resourceID, dest);
95   }
96 }
97 
MyLoadString(UINT resourceID,UString & dest)98 void MyLoadString(UINT resourceID, UString &dest)
99 {
100   MyLoadString(g_hInstance, resourceID, dest);
101 }
102 
103 }
104