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
Expand file tree
/
Copy pathDefaultInterfacesTests.cpp
More file actions
112 lines (86 loc) · 3.22 KB
/
DefaultInterfacesTests.cpp
File metadata and controls
112 lines (86 loc) · 3.22 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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 <xplatform.h>
#include <cassert>
#include <Server.Contracts.h>
// COM headers
#include <objbase.h>
#include <combaseapi.h>
#define COM_CLIENT
#include <Servers.h>
#define THROW_IF_FAILED(exp) { hr = exp; if (FAILED(hr)) { ::printf("FAILURE: 0x%08x = %s\n", hr, #exp); throw hr; } }
#define THROW_FAIL_IF_FALSE(exp) { if (!(exp)) { ::printf("FALSE: %s\n", #exp); throw E_FAIL; } }
template<COINIT TM>
struct ComInit
{
const HRESULT Result;
ComInit()
: Result{ ::CoInitializeEx(nullptr, TM) }
{ }
~ComInit()
{
if (SUCCEEDED(Result))
::CoUninitialize();
}
};
using ComMTA = ComInit<COINIT_MULTITHREADED>;
void ActivateClassWithDefaultInterfaces();
void FailToActivateDefaultInterfaceInstance();
void FailToQueryInterfaceForDefaultInterface();
int __cdecl main()
{
ComMTA init;
if (FAILED(init.Result))
return -1;
try
{
CoreShimComActivation csact{ W("NetServer.DefaultInterfaces"), W("DefaultInterfaceTesting") };
ActivateClassWithDefaultInterfaces();
FailToActivateDefaultInterfaceInstance();
FailToQueryInterfaceForDefaultInterface();
}
catch (HRESULT hr)
{
::printf("Test Failure: 0x%08x\n", hr);
return 101;
}
return 100;
}
void ActivateClassWithDefaultInterfaces()
{
::printf("Activate class using default interfaces via IUnknown...\n");
HRESULT hr;
// Validate a class that has an interface with function definitions can be activated
{
ComSmartPtr<IUnknown> unknown;
THROW_IF_FAILED(::CoCreateInstance(CLSID_DefaultInterfaceTesting, nullptr, CLSCTX_INPROC, IID_IUnknown, (void**)&unknown));
THROW_FAIL_IF_FALSE(unknown != nullptr);
}
{
ComSmartPtr<IClassFactory> classFactory;
THROW_IF_FAILED(::CoGetClassObject(CLSID_DefaultInterfaceTesting, CLSCTX_INPROC, nullptr, IID_IClassFactory, (void**)&classFactory));
ComSmartPtr<IUnknown> unknown;
THROW_IF_FAILED(classFactory->CreateInstance(nullptr, IID_IUnknown, (void**)&unknown));
THROW_FAIL_IF_FALSE(unknown != nullptr);
}
}
const int COR_E_INVALIDOPERATION = 0x80131509;
void FailToActivateDefaultInterfaceInstance()
{
::printf("Fail to activate class via a default interface...\n");
HRESULT hr;
ComSmartPtr<IDefaultInterfaceTesting> defInterface;
hr = ::CoCreateInstance(CLSID_DefaultInterfaceTesting, nullptr, CLSCTX_INPROC, IID_IDefaultInterfaceTesting, (void**)&defInterface);
THROW_FAIL_IF_FALSE(hr == COR_E_INVALIDOPERATION);
}
void FailToQueryInterfaceForDefaultInterface()
{
::printf("Fail to QueryInterface() for default interface...\n");
HRESULT hr;
ComSmartPtr<IDefaultInterfaceTesting2> defInterface2;
THROW_IF_FAILED(::CoCreateInstance(CLSID_DefaultInterfaceTesting, nullptr, CLSCTX_INPROC, IID_IDefaultInterfaceTesting2, (void**)&defInterface2));
ComSmartPtr<IDefaultInterfaceTesting> defInterface;
hr = defInterface2->QueryInterface(&defInterface);
THROW_FAIL_IF_FALSE(hr == COR_E_INVALIDOPERATION);
}