forked from maraf/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsstring_com.cpp
More file actions
91 lines (73 loc) · 2.68 KB
/
sstring_com.cpp
File metadata and controls
91 lines (73 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ---------------------------------------------------------------------------
// SString_COM.cpp
// ---------------------------------------------------------------------------
#include "stdafx.h"
#include "sstring.h"
#include "ex.h"
#include "holder.h"
#define DEFAULT_RESOURCE_STRING_SIZE 255
//----------------------------------------------------------------------------
// Load the string resource into this string.
//----------------------------------------------------------------------------
BOOL SString::LoadResource(CCompRC::ResourceCategory eCategory, int resourceID)
{
return SUCCEEDED(LoadResourceAndReturnHR(eCategory, resourceID));
}
HRESULT SString::LoadResourceAndReturnHR(CCompRC::ResourceCategory eCategory, int resourceID)
{
CONTRACT(HRESULT)
{
INSTANCE_CHECK;
NOTHROW;
}
CONTRACT_END;
HRESULT hr = E_FAIL;
#ifndef FEATURE_UTILCODE_NO_DEPENDENCIES
CCompRC* pResourceDLL = CCompRC::GetDefaultResourceDll();
if (pResourceDLL != NULL)
{
int size = 0;
EX_TRY
{
if (GetRawCount() == 0)
Resize(DEFAULT_RESOURCE_STRING_SIZE, REPRESENTATION_UNICODE);
while (TRUE)
{
// First try and load the string in the amount of space that we have.
// In fatal error reporting scenarios, we may not have enough memory to
// allocate a larger buffer.
hr = pResourceDLL->LoadString(eCategory, resourceID, GetRawUnicode(), GetRawCount()+1, &size);
if (hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
{
if (FAILED(hr))
{
Clear();
break;
}
// Although we cannot generally detect truncation, we can tell if we
// used up all the space (in which case we will assume truncation.)
if (size < (int)GetRawCount())
{
break;
}
}
// Double the size and try again.
Resize(size*2, REPRESENTATION_UNICODE);
}
if (SUCCEEDED(hr))
{
Truncate(Begin() + (COUNT_T) u16_strlen(GetRawUnicode()));
}
Normalize();
}
EX_CATCH
{
hr = E_FAIL;
}
EX_END_CATCH(SwallowAllExceptions);
}
#endif //!FEATURE_UTILCODE_NO_DEPENDENCIES
RETURN hr;
} // SString::LoadResourceAndReturnHR