Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Anda dapat dengan bebas mencampur kode WRL dengan kode Windows Runtime C++ Template Library (WRL). Dalam unit terjemahan yang sama, Anda dapat menggunakan objek yang dideklarasikan dengan notasi handle-to-object (^) WRL dan notasi smart pointer (ComPtr<T>) WRL. Namun, Anda harus menangani nilai pengembalian secara manual, dan kode kesalahan WRL HRESULT dan pengecualian WRL.
Pengembangan WRL
Untuk informasi selengkapnya tentang penulisan dan penggunaan komponen WRL, lihat Windows Runtime C++ Template Library (WRL).
Contoh
Cuplikan kode berikut menunjukkan penggunaan WRL dan WRL untuk menggunakan kelas Windows Runtime dan memeriksa file metadata.
Contoh diambil dari cuplikan kode di forum Membangun aplikasi Microsoft Store. Penulis cuplikan kode ini menawarkan penafian dan ketentuan berikut:
C++ tidak menyediakan API tertentu untuk dicerminkan pada jenis Windows Runtime, tetapi file metadata Windows (.winmd) untuk jenis sepenuhnya kompatibel dengan file metadata CLR. Windows menyediakan API penemuan metadata baru (RoGetMetaDataFile) untuk masuk ke file .winmd untuk jenis tertentu. Namun, API ini terbatas digunakan untuk pengembang C++ karena Anda tidak dapat membuat instans kelas.
Setelah kode dikompilasi, Anda juga harus meneruskan Runtimeobject.lib dan Rometadata.lib ke Linker.
Cuplikan ini disajikan apa adanya. Meskipun diharapkan berfungsi dengan benar, mungkin dapat berisi kesalahan.
#include <hstring.h>
#include <cor.h>
#include <rometadata.h>
#include <rometadataresolution.h>
#include <collection.h>
namespace ABI_Isolation_Workaround {
#include <inspectable.h>
#include <WeakReference.h>
}
using namespace ABI_Isolation_Workaround;
#include <wrl/client.h>
using namespace Microsoft::WRL;
using namespace Windows::Foundation::Collections;
IVector<String^>^ GetTypeMethods(Object^);
MainPage::MainPage()
{
InitializeComponent();
Windows::Foundation::Uri^ uri = ref new Windows::Foundation::Uri("http://buildwindows.com/");
auto methods = GetTypeMethods(uri);
std::wstring strMethods;
std::for_each(begin(methods), end(methods), [&strMethods](String^ methodName) {
strMethods += methodName->Data();
strMethods += L"\n";
});
wprintf_s(L"%s\n", strMethods.c_str());
}
IVector<String^>^ GetTypeMethods(Object^ instance)
{
HRESULT hr;
HSTRING hStringClassName;
hr = instance->__cli_GetRuntimeClassName(reinterpret_cast<__cli_HSTRING__**>(&hStringClassName)); // internal method name subject to change post BUILD
if (FAILED(hr))
__cli_WinRTThrowError(hr); // internal method name subject to change post BUILD
String^ className = reinterpret_cast<String^>(hStringClassName);
ComPtr<IMetaDataDispenserEx> metadataDispenser; ComPtr<IMetaDataImport2> metadataImport; hr = MetaDataGetDispenser(CLSID_CorMetaDataDispenser, IID_IMetaDataDispenser, (LPVOID*)metadataDispenser.GetAddressOf());
if (FAILED(hr))
__cli_WinRTThrowError(hr); // internal method name subject to change post BUILD
HSTRING hStringFileName;
mdTypeDef typeDefToken;
hr = RoGetMetaDataFile(hStringClassName, metadataDispenser.Get(), &hStringFileName, &metadataImport, &typeDefToken);
if (FAILED(hr))
__cli_WinRTThrowError(hr); // internal method name subject to change post BUILD
String^ fileName = reinterpret_cast<String^>(hStringFileName);
HCORENUM hCorEnum = 0;
mdMethodDef methodDefs[2048];
ULONG countMethodDefs = sizeof(methodDefs);
hr = metadataImport->EnumMethods(&hCorEnum, typeDefToken, methodDefs, countMethodDefs, &countMethodDefs);
if (FAILED(hr))
__cli_WinRTThrowError(hr); // internal method name subject to change post BUILD
wchar_t methodName[1024];
ULONG countMethodName;
std::wstring strMethods;
Vector<String^>^ retVal = ref new Vector<String^>();
for (int i = 0; i < countMethodDefs; ++i)
{
countMethodName = sizeof(methodName);
hr = metadataImport->GetMethodProps(methodDefs[i], nullptr, methodName, countMethodName, &countMethodName, nullptr, nullptr, nullptr, nullptr, nullptr);
if (SUCCEEDED(hr))
{
methodName[ countMethodName ] = 0;
retVal->Append(ref new String(methodName));
}
}
return retVal;
}