1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
8 #include "resourcestring.h"
10 static int CompareNativeStringResources(const void *a, const void *b)
12 unsigned int resourceIdA = ((NativeStringResource*)a)->resourceId;
13 unsigned int resourceIdB = ((NativeStringResource*)b)->resourceId;
15 if (resourceIdA < resourceIdB)
18 if (resourceIdA == resourceIdB)
24 int LoadNativeStringResource(const NativeStringResourceTable &nativeStringResourceTable, unsigned int iResourceID, WCHAR* szBuffer, int iMax, int *pcwchUsed)
29 // Search the sorted set of resources for the ID we're interested in.
30 NativeStringResource searchEntry = {iResourceID, NULL};
31 NativeStringResource *resourceEntry = (NativeStringResource*)bsearch(
33 nativeStringResourceTable.table,
34 nativeStringResourceTable.size,
35 sizeof(NativeStringResource),
36 CompareNativeStringResources);
38 if (resourceEntry != NULL)
40 len = PAL_GetResourceString(NULL, resourceEntry->resourceString, szBuffer, iMax);
43 int hr = HRESULT_FROM_GetLastError();
45 // Tell the caller if the buffer isn't big enough
46 if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) && pcwchUsed)
54 // The resource ID wasn't found in our array. Fall back on returning the ID as a string.
55 len = _snwprintf_s(szBuffer, iMax, _TRUNCATE, W("[Undefined resource string ID:0x%X]"), iResourceID);
58 // The only possible failure is that that string didn't fit the buffer. So the buffer contains
59 // partial string terminated by '\0'
60 // We could return ERROR_INSUFFICIENT_BUFFER, but we'll error on the side of caution here and
61 // actually show something (given that this is likely a scenario involving a bug/deployment issue)