/
Win32Process_CPP.cpp
149 lines (131 loc) · 5.14 KB
/
Win32Process_CPP.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "stdafx.h"
using namespace std;
using namespace concurrency;
using namespace Platform;
using namespace Windows::ApplicationModel::AppService;
using namespace Windows::ApplicationModel::DataTransfer;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::UI::Notifications;
using namespace Windows::Data::Xml::Dom;
AppServiceConnection^ _connection = nullptr;
void RequestReceived(AppServiceConnection^ connection, AppServiceRequestReceivedEventArgs^ args);
void ServiceClosed(AppServiceConnection^ connection, AppServiceClosedEventArgs^ args);
/// <summary>
/// Creates the app service connection
/// </summary>
IAsyncAction^ ConnectToAppServiceAsync()
{
return create_async([]
{
// Get the package family name
Windows::ApplicationModel::Package^ package = Windows::ApplicationModel::Package::Current;
Platform::String^ packageFamilyName = package->Id->FamilyName;
// Create and set the connection
auto connection = ref new AppServiceConnection();
connection->PackageFamilyName = packageFamilyName;
connection->AppServiceName = "CommunicationService";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (FOREGROUND_GREEN));
cout << "Opening connection..." << endl;
// Open the connection
create_task(connection->OpenAsync()).then([connection](AppServiceConnectionStatus status)
{
if (status == AppServiceConnectionStatus::Success)
{
_connection = connection;
wcout << "Successfully opened connection." << endl;
_connection->RequestReceived += ref new TypedEventHandler<AppServiceConnection^, AppServiceRequestReceivedEventArgs^>(RequestReceived);
_connection->ServiceClosed += ref new TypedEventHandler<AppServiceConnection^, AppServiceClosedEventArgs^>(ServiceClosed);
}
else if (status == AppServiceConnectionStatus::AppUnavailable || status == AppServiceConnectionStatus::AppServiceUnavailable)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (FOREGROUND_RED));
cout << "AppService Unavailable" << endl;
}
});
});
}
/// <summary>
/// Creates an app service thread
/// </summary>
int main(Platform::Array<Platform::String^>^ args)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (FOREGROUND_RED | FOREGROUND_GREEN));
wcout << "*********************************" << endl;
wcout << "**** Classic desktop C++ app ****" << endl;
wcout << "*********************************" << endl << endl;
wcout << L"Creating app service connection" << endl << endl;
ConnectToAppServiceAsync();
string result;
cin >> result;
return 0;
}
/// <summary>
/// Creates a toast with the recived message from the UWP
/// </summary>
void SendNewToast(PCWSTR XMLString)
{
// Use LoadXml to load the input xml string, which should be formatted as expected by the Toasts schema
String^ toastXMLString = ref new String(XMLString);
XmlDocument^ toastPayloadXML = ref new XmlDocument();
toastPayloadXML->LoadXml(toastXMLString);
ToastNotification^ toast = ref new ToastNotification(toastPayloadXML);
ToastNotificationManager::CreateToastNotifier()->Show(toast);
}
/// <summary>
/// Creates aa toast with the recived message from the UWP
/// </summary>
void ShowToast(PCWSTR data)
{
wchar_t* buffer = new wchar_t[512];
PCWSTR toast = LR"(<toast>
<visual>
<binding template="ToastGeneric">
<text>Request received:</text>
<text> %s</text>
</binding>
</visual>
<audio src="ms-winsoundevent:Notification.IM"/>
</toast>)";
wsprintf(buffer, toast, data);
SendNewToast(buffer);
delete []buffer;
}
/// <summary>
/// Receives message from UWP app and sends a response back
/// </summary>
void RequestReceived(AppServiceConnection^ connection, AppServiceRequestReceivedEventArgs^ args)
{
auto deferral = args->GetDeferral();
auto message = args->Request->Message;
auto method = message->Lookup(L"request")->ToString();
PCWSTR data = method->Data();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (FOREGROUND_RED | FOREGROUND_GREEN));
wcout << data;
wcout << L" - request received" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (FOREGROUND_GREEN));
ShowToast(data);
auto response = ref new ValueSet();
response->Insert("response", "ack");
create_task(args->Request->SendResponseAsync(response)).then([deferral](AppServiceResponseStatus status)
{
deferral->Complete();
});
}
/// <summary>
/// Occurs when the other endpoint closes the connection to the app service
/// </summary>
void ServiceClosed(AppServiceConnection^ connection, AppServiceClosedEventArgs^ args)
{
}