Created
May 21, 2020 17:07
-
-
Save ChunMinChang/66c1e9c8c87b21580b22bd2a2193fdf2 to your computer and use it in GitHub Desktop.
Test sample for HString stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// References: | |
// https://docs.microsoft.com/en-us/cpp/cppcx/wrl/how-to-activate-and-use-a-windows-runtime-component-using-wrl?view=vs-2019 | |
// https://chromium.googlesource.com/chromium/src/+/master/base/win/hstring_reference_unittest.cc | |
// This is compiled by: `$HOME/.mozbuild/clang/bin/clang-cl.exe HStringReference.cpp` | |
// or `cl.exe HStringReference.cpp runtimeobject.lib` in `Developer Command Prompt` | |
#include <assert.h> // assert | |
#include <wchar.h> // wcscmp | |
#include <wrl\wrappers\corewrappers.h> // HStringReference | |
#pragma comment(lib, "runtimeobject.lib") | |
// To shorten the Microsoft::WRL::Wrappers::HStringReference: | |
using namespace Microsoft::WRL::Wrappers; | |
constexpr wchar_t kTestString[] = L"123"; | |
constexpr wchar_t kEmptyString[] = L""; | |
int main() { | |
unsigned int length = 0; | |
const wchar_t* raw = nullptr; | |
// normal string | |
const HStringReference string(kTestString); | |
assert(string.Get() != nullptr); // HSTRING | |
raw = string.GetRawBuffer(&length); | |
wprintf(L"kTestString(%s): raw: %s, length: %d\n", kTestString, raw, length); | |
assert(raw != nullptr); | |
assert(wcscmp(raw, kTestString) == 0); | |
assert(length == 3); | |
// empty string | |
const HStringReference empty_string(kEmptyString); | |
assert(empty_string.Get() == nullptr); // HSTRING | |
raw = empty_string.GetRawBuffer(&length); | |
wprintf(L"kEmptyString(%s): raw: %s, length: %d\n", kEmptyString, raw, length); | |
assert(raw != nullptr); | |
assert(wcscmp(raw, kEmptyString) == 0); | |
assert(length == 0); | |
// null string | |
// const HStringReference null_string(nullptr); // Segmentation fault | |
// HSTRING null_string(nullptr); // This is ok. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment