This repository was archived by the owner on Jan 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathresourcestring.cpp
74 lines (61 loc) · 2.47 KB
/
resourcestring.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include<stdio.h>
#include<utilcode.h>
#include"resourcestring.h"
staticintCompareNativeStringResources(constvoid *a, constvoid *b)
{
unsignedint resourceIdA = ((NativeStringResource*)a)->resourceId;
unsignedint resourceIdB = ((NativeStringResource*)b)->resourceId;
if (resourceIdA < resourceIdB)
return -1;
if (resourceIdA == resourceIdB)
return0;
return1;
}
intLoadNativeStringResource(const NativeStringResourceTable &nativeStringResourceTable, unsignedint iResourceID, WCHAR* szBuffer, int iMax, int *pcwchUsed)
{
int len = 0;
if (szBuffer && iMax)
{
// Search the sorted set of resources for the ID we're interested in.
NativeStringResource searchEntry = {iResourceID, NULL};
NativeStringResource *resourceEntry = (NativeStringResource*)bsearch(
&searchEntry,
nativeStringResourceTable.table,
nativeStringResourceTable.size,
sizeof(NativeStringResource),
CompareNativeStringResources);
if (resourceEntry != NULL)
{
len = PAL_GetResourceString(NULL, resourceEntry->resourceString, szBuffer, iMax);
if (len == 0)
{
int hr = HRESULT_FROM_GetLastError();
// Tell the caller if the buffer isn't big enough
if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) && pcwchUsed)
*pcwchUsed = iMax;
return hr;
}
}
else
{
// The resource ID wasn't found in our array. Fall back on returning the ID as a string.
len = _snwprintf_s(szBuffer, iMax, _TRUNCATE, W("[Undefined resource string ID:0x%X]"), iResourceID);
if (len < 0)
{
// The only possible failure is that that string didn't fit the buffer. So the buffer contains
// partial string terminated by '\0'
// We could return ERROR_INSUFFICIENT_BUFFER, but we'll error on the side of caution here and
// actually show something (given that this is likely a scenario involving a bug/deployment issue)
len = iMax - 1;
}
}
}
if (pcwchUsed)
{
*pcwchUsed = len;
}
return S_OK;
}