- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
据我所知,Windows 运行时是 Windows 公开其 API 的新基础结构。我的问题很简单:如何从纯 C 代码中使用它?我不介意编写更多代码,我只是想了解事物是如何链接在一起的。
我们以微软给出的基本例子为例:https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-enhance .具体来说,“修改 C++ Win32 项目以使用 Windows 运行时 API”,有一个示例展示了如何显示来自应用程序的 toast 通知。我如何翻译该代码以从普通 .c 文件中使用它?
我在C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\winrt
找到了一些头文件,例如windows.ui.notifications.h
我认为可能会有用,但我不知道我应该如何使用那里的东西。在 MSDN 上,没有文章谈论纯 C,只有各种托管语言和 C++ 的预测。
拜托,这更像是一个学术问题。我过去曾成功地使用 C 中的 COM,并且对它非常满意,但为此,我在网上找不到任何关于它的提及或文章。
谢谢。
编辑 现在我有一些成功执行的代码(生成的 HRESULT 为 S_OK)但没有显示 toast。知道如何调试吗?究竟是什么失败了?我还没有实现 COM 激活器,因为我有一个类似的 PowerShell 脚本,它与我用 C 编写的基本相同的东西一起工作。我被困住了,迷路了,也许有人可以提供帮助。
#include <stdio.h>
#include <initguid.h>
#include <roapi.h>
#pragma comment(lib, "runtimeobject.lib")
#include <Windows.ui.notifications.h>
#include <winstring.h>
#include <shobjidl_core.h>
#include <propvarutil.h>
#include <propkey.h>
#include <Psapi.h>
// ABI.Windows.UI.Notifications.IToastNotificationManagerStatics
// 50ac103f-d235-4598-bbef-98fe4d1a3ad4
DEFINE_GUID(UIID_IToastNotificationManagerStatics,
0x50ac103f,
0xd235, 0x4598, 0xbb, 0xef,
0x98, 0xfe, 0x4d, 0x1a, 0x3a, 0xd4
);
// ABI.Windows.Data.Xml.Dom.IXmlDocument
// f7f3a506-1e87-42d6-bcfb-b8c809fa5494
DEFINE_GUID(UIID_IXmlDocument,
0xf7f3a506,
0x1e87, 0x42d6, 0xbc, 0xfb,
0xb8, 0xc8, 0x09, 0xfa, 0x54, 0x94
);
// ABI.Windows.Data.Xml.Dom.IXmlDocumentIO
// 6cd0e74e-ee65-4489-9ebf-ca43e87ba637
DEFINE_GUID(UIID_IXmlDocumentIO,
0x6cd0e74e,
0xee65, 0x4489, 0x9e, 0xbf,
0xca, 0x43, 0xe8, 0x7b, 0xa6, 0x37
);
// ABI.Windows.Notifications.IToastNotificationFactory
// 04124b20-82c6-4229-b109-fd9ed4662b53
DEFINE_GUID(UIID_IToastNotificationFactory,
0x04124b20,
0x82c6, 0x4229, 0xb1, 0x09,
0xfd, 0x9e, 0xd4, 0x66, 0x2b, 0x53
);
// CLSID_ShellLink
// 00021401-0000-0000-C000-000000000046
DEFINE_GUID(CLSID_ShellLink,
0x00021401,
0x0000, 0x0000, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x46
);
// IShellLinkW
// 000214F9-0000-0000-C000-000000000046
DEFINE_GUID(IID_ShellLink,
0x000214f9,
0x0000, 0x0000, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x46
);
// IPropertyStore
// 886d8eeb-8cf2-4446-8d02-cdba1dbdcf99
DEFINE_GUID(IID_IPropertyStore,
0x886d8eeb,
0x8cf2, 0x4446, 0x8d, 0x02,
0xcd, 0xba, 0x1d, 0xbd, 0xcf, 0x99
);
// IPersistFile
// 0000010b-0000-0000-C000-000000000046
DEFINE_GUID(IID_IPersistFile,
0x0000010b,
0x0000, 0x0000, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x46
);
#define APP_ID L"valinet.thunderbirdtoasts"
#define APP_CLSID L"04f9ecea-f0da-4ea2-b2d7-acf208ae30a1"
// APP_UUID
// 04f9ecea-f0da-4ea2-b2d7-acf208ae30a1
/*
DEFINE_GUID(APP_UUID,
0x04f9ecea,
0xf0da, 0x4ea2, 0xb2, 0xd7,
0xac, 0xf2, 0x08, 0xae, 0x30, 0xa1
);
*/
inline HRESULT InitPropVariantFromString(_In_ PCWSTR psz, _Out_ PROPVARIANT* ppropvar)
{
HRESULT hr = psz != NULL ? S_OK : E_INVALIDARG; // Previous API behavior counter to the SAL requirement.
if (SUCCEEDED(hr))
{
SIZE_T const byteCount = (SIZE_T)((wcslen(psz) + 1) * sizeof(*psz));
V_UNION(ppropvar, pwszVal) = (PWSTR)(CoTaskMemAlloc(byteCount));
hr = V_UNION(ppropvar, pwszVal) ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
memcpy_s(V_UNION(ppropvar, pwszVal), byteCount, psz, byteCount);
V_VT(ppropvar) = VT_LPWSTR;
}
}
if (FAILED(hr))
{
PropVariantInit(ppropvar);
}
return hr;
}
HRESULT InstallShortcut(_In_z_ wchar_t* shortcutPath)
{
wchar_t exePath[MAX_PATH];
DWORD charWritten = GetModuleFileNameEx(
GetCurrentProcess(),
NULL,
exePath,
ARRAYSIZE(exePath)
);
HRESULT hr = charWritten > 0 ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
IShellLink* shellLink = NULL;
hr = CoCreateInstance(
&CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
&IID_ShellLink,
&shellLink
);
if (SUCCEEDED(hr))
{
hr = shellLink->lpVtbl->SetPath(shellLink, exePath);
if (SUCCEEDED(hr))
{
hr = shellLink->lpVtbl->SetArguments(shellLink, L"");
if (SUCCEEDED(hr))
{
IPropertyStore* propertyStore;
shellLink->lpVtbl->QueryInterface(
shellLink,
&IID_IPropertyStore,
&propertyStore
);
if (SUCCEEDED(hr))
{
PROPVARIANT appIdPropVar;
hr = InitPropVariantFromString(APP_ID, &appIdPropVar);
if (SUCCEEDED(hr))
{
hr = propertyStore->lpVtbl->SetValue(propertyStore, &PKEY_AppUserModel_ID, &appIdPropVar);
if (SUCCEEDED(hr))
{
PROPVARIANT appClsIdPropVar;
hr = InitPropVariantFromString(APP_CLSID, &appClsIdPropVar);
if (SUCCEEDED(hr))
{
hr = propertyStore->lpVtbl->SetValue(propertyStore, &PKEY_AppUserModel_ToastActivatorCLSID, &appClsIdPropVar);
if (SUCCEEDED(hr))
{
hr = propertyStore->lpVtbl->Commit(propertyStore);
if (SUCCEEDED(hr))
{
IPersistFile* persistFile = NULL;
shellLink->lpVtbl->QueryInterface(
shellLink,
&IID_IPersistFile,
&persistFile
);
if (SUCCEEDED(hr))
{
hr = persistFile->lpVtbl->Save(persistFile, shortcutPath, TRUE);
}
}
}
PropVariantClear(&appClsIdPropVar);
}
}
PropVariantClear(&appIdPropVar);
}
}
}
}
}
}
return hr;
}
HRESULT TryCreateShortcut()
{
wchar_t shortcutPath[MAX_PATH];
DWORD charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath, MAX_PATH);
HRESULT hr = charWritten > 0 ? S_OK : E_INVALIDARG;
if (SUCCEEDED(hr))
{
errno_t concatError = wcscat_s(shortcutPath, ARRAYSIZE(shortcutPath), L"\\Microsoft\\Windows\\Start Menu\\Programs\\Thunderbird Toasts.lnk");
hr = concatError == 0 ? S_OK : E_INVALIDARG;
if (SUCCEEDED(hr))
{
DWORD attributes = GetFileAttributes(shortcutPath);
BOOL fileExists = attributes < 0xFFFFFFF;
if (!fileExists)
{
hr = InstallShortcut(shortcutPath); // See step 2.
}
else
{
hr = S_FALSE;
}
}
}
return hr;
}
HRESULT CreateXmlDocumentFromString(
const wchar_t* xmlString,
__x_ABI_CWindows_CData_CXml_CDom_CIXmlDocument** doc
)
{
HRESULT hr;
HSTRING_HEADER header_;
HSTRING IXmlDocumentHString;
hr = WindowsCreateStringReference(
RuntimeClass_Windows_Data_Xml_Dom_XmlDocument,
wcslen(RuntimeClass_Windows_Data_Xml_Dom_XmlDocument),
&header_,
&IXmlDocumentHString
);
if (FAILED(hr))
{
printf("WindowsCreateStringReference IXmlDocumentHString\n");
return hr;
}
if (IXmlDocumentHString == NULL)
{
return 1;
}
IInspectable* pInspectable;
hr = RoActivateInstance(IXmlDocumentHString, &pInspectable);
if (SUCCEEDED(hr))
{
hr = pInspectable->lpVtbl->QueryInterface(
pInspectable,
&UIID_IXmlDocument,
doc
);
pInspectable->lpVtbl->Release(pInspectable);
}
else
{
printf("RoActivateInstance IXmlDocumentHString\n");
return hr;
}
__x_ABI_CWindows_CData_CXml_CDom_CIXmlDocumentIO* docIO;
(*doc)->lpVtbl->QueryInterface(
(*doc),
&UIID_IXmlDocumentIO,
&docIO
);
if (FAILED(hr))
{
printf("QueryInterface IXmlDocumentIO\n");
return hr;
}
HSTRING XmlString;
hr = WindowsCreateStringReference(
xmlString,
wcslen(xmlString),
&header_,
&XmlString
);
if (FAILED(hr))
{
printf("WindowsCreateStringReference XmlString\n");
return hr;
}
if (XmlString == NULL)
{
return 1;
}
hr = docIO->lpVtbl->LoadXml(docIO, XmlString);
if (FAILED(hr))
{
printf("LoadXml IXmlDocumentIO\n");
return hr;
}
return hr;
}
int main()
{
HRESULT hr = RoInitialize(RO_INIT_MULTITHREADED);
if (FAILED(hr))
{
printf("RoInitialize\n");
return 0;
}
TryCreateShortcut();
HSTRING_HEADER header_;
HSTRING ToastNotificationManagerHString;
hr = WindowsCreateStringReference(
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
wcslen(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager),
&header_,
&ToastNotificationManagerHString
);
if (FAILED(hr))
{
printf("WindowsCreateStringReference ToastNotificationManagerHString\n");
return 0;
}
if (ToastNotificationManagerHString == NULL)
{
return 0;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotificationManagerStatics* toastStatics = NULL;
hr = RoGetActivationFactory(
ToastNotificationManagerHString,
&UIID_IToastNotificationManagerStatics,
(LPVOID*)&toastStatics
);
if (FAILED(hr))
{
printf("RoGetActivationFactory ToastNotificationManagerHString\n");
return 0;
}
__x_ABI_CWindows_CData_CXml_CDom_CIXmlDocument* inputXml = NULL;
hr = CreateXmlDocumentFromString(
L"<toast activationType=\"protocol\" launch=\"imsprevn://0\" duration=\"long\">"
L"<visual><binding template=\"ToastGeneric\"><text>text1</text><text>text2</text><text placement=\"attribution\">attr</text>"
L"</binding></visual><audio src=\"ms-winsoundevent:Notification.Mail\" loop=\"false\" /></toast>"
, &inputXml
);
if (FAILED(hr))
{
printf("CreateXmlDocumentFromString\n");
return 0;
}
HSTRING AppIdHString;
hr = WindowsCreateStringReference(
APP_ID,
wcslen(APP_ID),
&header_,
&AppIdHString
);
if (FAILED(hr))
{
printf("WindowsCreateStringReference AppIdHString\n");
return 0;
}
if (AppIdHString == NULL)
{
return 0;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotifier* notifier;
hr = toastStatics->lpVtbl->CreateToastNotifierWithId(toastStatics, AppIdHString, ¬ifier);
if (FAILED(hr))
{
printf("CreateToastNotifier\n");
return 0;
}
HSTRING ToastNotificationHString;
hr = WindowsCreateStringReference(
RuntimeClass_Windows_UI_Notifications_ToastNotification,
wcslen(RuntimeClass_Windows_UI_Notifications_ToastNotification),
&header_,
&ToastNotificationHString
);
if (FAILED(hr))
{
printf("WindowsCreateStringReference ToastNotificationHString\n");
return 0;
}
if (ToastNotificationHString == NULL)
{
return 0;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotificationFactory* notifFactory = NULL;
hr = RoGetActivationFactory(
ToastNotificationHString,
&UIID_IToastNotificationFactory,
(LPVOID*)¬ifFactory
);
if (FAILED(hr))
{
printf("RoGetActivationFactory ToastNotificationHString\n");
return 0;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotification2* notif = NULL;
hr = notifFactory->lpVtbl->CreateToastNotification(notifFactory, inputXml, ¬if);
if (FAILED(hr))
{
printf("CreateToastNotification\n");
return 0;
}
hr = notif->lpVtbl->put_Tag(notif, AppIdHString);
if (FAILED(hr))
{
printf("put_Tag\n");
return 0;
}
hr = notif->lpVtbl->put_Group(notif, AppIdHString);
if (FAILED(hr))
{
printf("put_Group\n");
return 0;
}
hr = notifier->lpVtbl->Show(notifier, notif);
if (FAILED(hr))
{
printf("Show\n");
return 0;
}
printf("success\n");
return 0;
}
这是 list 文件(IsWindows10OrGreater()
返回 1):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<assemblyIdentity
type="win32"
name="valinet.testapp"
version="1.2.3.4"
processorArchitecture="x86"
/>
<description>TestApp</description>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
</application>
</compatibility>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<!--
UAC settings:
- app should run at same integrity level as calling process
- app does not need to manipulate windows belonging to
higher-integrity-level processes
-->
<requestedExecutionLevel
level="asInvoker"
uiAccess="false"
/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
这是 PowerShell 脚本:
param ($appid, $action, $title, $text, $attr, $duration, $audio)
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$template = @"
<toast activationType="protocol" launch="$action" duration="$duration">
<visual>
<binding template="ToastGeneric">
<text><![CDATA[$title]]></text>
<text><![CDATA[$text]]></text>
<text placement="attribution"><![CDATA[$attr]]></text>
</binding>
</visual>
<audio src="$audio" loop="false" />
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
$toast.Tag = $appid
$toast.Group = $appid
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appid).Show($toast)
最佳答案
我终于明白了。问题是,您必须在终止进程之前稍等片刻,以便 COM 线程有机会实际执行工作并传递通知。上面发布的代码完全有效,稍后我将发布一个只有绝对必要的东西的修订版本。我解决它的方法是剥离所有内容,直到示例应用程序 ( https://github.com/microsoft/Windows-classic-samples/blob/master/Samples/DesktopToasts/CPP/DesktopToastsSample.cpp ) 具有与我的代码相同的代码,唯一的区别是消息队列在“发送”toast 请求后使示例应用程序保持事件状态。
此外,如果您认为通知只能触发一个协议(protocol),并且不包含任何按钮,那么您可以跳过在“开始”中创建快捷方式,而只使用另一个应用程序的 appid。如果您提供的 appid 不属于任何已安装的应用程序,则 toast 将没有图标,应用程序名称将是您提供的任何内容。这对于为当前应用程序开发扩展/附加组件的人来说非常有用,他们实际上不需要用不必要的快捷方式使“开始”菜单变得困惑。按钮需要 COM 激活,因为不幸的是,但至少我们有这个。
是的,您也不需要应用程序 list 。现在的问题是在终止之前等待的正确方法是什么?我的意思是,当然 Sleep(200);
很好,但我很好奇正确的解决方案。
要获取已安装应用的应用 ID,请在 PowerShell 中键入 Get-StartApps
。
编辑:这是工作代码,希望能释放内存等。
// Set project Properties - Configuration Properties - Linker - All Options -
// - Additional Dependencies - runtimeobject.lib
// #pragma comment(lib, "runtimeobject.lib") does not work when compiled
// without default lib for whatever reason
//
// Set project Properties - Configuration Properties - Linker - All Options -
// - SubSystem - Windows or change to Console
// and modify the entry point and the signature of "main"
// the pragma belowis optional
#include <initguid.h>
#include <roapi.h>
#include <Windows.ui.notifications.h>
#define Done(code) ExitProcess(code)
// Choose whether you would like to compile with/without the standard library
#define INCLUDE_DEFAULTLIB
#undef INCLUDE_DEFAULTLIB
#ifdef INCLUDE_DEFAULTLIB
#include <stdio.h>
#define __wcslen wcslen
#else
#pragma comment(linker, "/NODEFAULTLIB")
#pragma comment(linker, "/ENTRY:wWinMain")
void printf(char* b, ...) {}
DWORD __wcslen(WCHAR* pszText)
{
WCHAR* pszCurrent = pszText;
while (pszCurrent[0])
{
pszCurrent++;
}
return pszCurrent - pszText;
}
#endif
// UUIDs obtained from <windows.ui.notifications.h>
//
// ABI.Windows.UI.Notifications.IToastNotificationManagerStatics
// 50ac103f-d235-4598-bbef-98fe4d1a3ad4
DEFINE_GUID(UIID_IToastNotificationManagerStatics,
0x50ac103f,
0xd235, 0x4598, 0xbb, 0xef,
0x98, 0xfe, 0x4d, 0x1a, 0x3a, 0xd4
);
//
// ABI.Windows.Notifications.IToastNotificationFactory
// 04124b20-82c6-4229-b109-fd9ed4662b53
DEFINE_GUID(UIID_IToastNotificationFactory,
0x04124b20,
0x82c6, 0x4229, 0xb1, 0x09,
0xfd, 0x9e, 0xd4, 0x66, 0x2b, 0x53
);
// UUIDs obtained from <windows.data.xml.dom.h>
//
// ABI.Windows.Data.Xml.Dom.IXmlDocument
// f7f3a506-1e87-42d6-bcfb-b8c809fa5494
DEFINE_GUID(UIID_IXmlDocument,
0xf7f3a506,
0x1e87, 0x42d6, 0xbc, 0xfb,
0xb8, 0xc8, 0x09, 0xfa, 0x54, 0x94
);
//
// ABI.Windows.Data.Xml.Dom.IXmlDocumentIO
// 6cd0e74e-ee65-4489-9ebf-ca43e87ba637
DEFINE_GUID(UIID_IXmlDocumentIO,
0x6cd0e74e,
0xee65, 0x4489, 0x9e, 0xbf,
0xca, 0x43, 0xe8, 0x7b, 0xa6, 0x37
);
// This is the AppUserModelId of an application from the Start menu
// If you don't supply a valid entry here, the toast will have no icon
// The ID below is for Mozilla Thunderbird
#define APP_ID L"D78BF5DD33499EC2"
HRESULT CreateXmlDocumentFromString(
const wchar_t* xmlString,
__x_ABI_CWindows_CData_CXml_CDom_CIXmlDocument** doc
)
{
HRESULT hr = S_OK;
HSTRING_HEADER header_IXmlDocumentHString;
HSTRING IXmlDocumentHString;
hr = WindowsCreateStringReference(
RuntimeClass_Windows_Data_Xml_Dom_XmlDocument,
(UINT32)__wcslen(RuntimeClass_Windows_Data_Xml_Dom_XmlDocument),
&header_IXmlDocumentHString,
&IXmlDocumentHString
);
if (FAILED(hr))
{
printf("%s:%d:: WindowsCreateStringReference\n", __FUNCTION__, __LINE__);
return hr;
}
if (IXmlDocumentHString == NULL)
{
return E_POINTER;
}
IInspectable* pInspectable;
hr = RoActivateInstance(IXmlDocumentHString, &pInspectable);
if (SUCCEEDED(hr))
{
hr = pInspectable->lpVtbl->QueryInterface(
pInspectable,
&UIID_IXmlDocument,
doc
);
pInspectable->lpVtbl->Release(pInspectable);
}
else
{
printf("%s:%d:: RoActivateInstance\n", __FUNCTION__, __LINE__);
return hr;
}
__x_ABI_CWindows_CData_CXml_CDom_CIXmlDocumentIO* docIO;
(*doc)->lpVtbl->QueryInterface(
(*doc),
&UIID_IXmlDocumentIO,
&docIO
);
if (FAILED(hr))
{
printf("%s:%d:: QueryInterface\n", __FUNCTION__, __LINE__);
return hr;
}
HSTRING_HEADER header_XmlString;
HSTRING XmlString;
hr = WindowsCreateStringReference(
xmlString,
(UINT32)__wcslen(xmlString),
&header_XmlString,
&XmlString
);
if (FAILED(hr))
{
printf("%s:%d:: WindowsCreateStringReference\n", __FUNCTION__, __LINE__);
docIO->lpVtbl->Release(docIO);
return hr;
}
if (XmlString == NULL)
{
return E_POINTER;
}
hr = docIO->lpVtbl->LoadXml(docIO, XmlString);
docIO->lpVtbl->Release(docIO);
return hr;
}
int WINAPI wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nShowCmd
)
{
#ifdef INCLUDE_DEFAULTLIB
FILE* conout;
AllocConsole();
freopen_s(&conout, "CONOUT$", "w", stdout);
#endif
HRESULT hr = S_OK;
hr = RoInitialize(RO_INIT_MULTITHREADED);
if (FAILED(hr))
{
printf("%s:%d:: RoInitialize\n", __FUNCTION__, __LINE__);
goto exit0;
}
HSTRING_HEADER header_AppIdHString;
HSTRING AppIdHString;
hr = WindowsCreateStringReference(
APP_ID,
(UINT32)__wcslen(APP_ID),
&header_AppIdHString,
&AppIdHString
);
if (FAILED(hr))
{
printf("%s:%d:: WindowsCreateStringReference\n", __FUNCTION__, __LINE__);
goto exit1;
}
if (AppIdHString == NULL)
{
hr = E_POINTER;
goto exit1;
}
__x_ABI_CWindows_CData_CXml_CDom_CIXmlDocument* inputXml = NULL;
hr = CreateXmlDocumentFromString(
L"<toast activationType=\"protocol\" launch=\"imsprevn://0\" duration=\"long\">\r\n"
L" <visual>\r\n"
L" <binding template=\"ToastGeneric\">\r\n"
L" <text><![CDATA[Hello, world]]></text>\r\n"
L" <text><![CDATA[Click me]]></text>\r\n"
L" <text placement=\"attribution\"><![CDATA[Bottom text]]></text>\r\n"
L" </binding>\r\n"
L" </visual>\r\n"
L" <audio src=\"ms-winsoundevent:Notification.Mail\" loop=\"false\" />\r\n"
L"</toast>\r\n"
, &inputXml
);
if (FAILED(hr))
{
printf("%s:%d:: CreateXmlDocumentFromString\n", __FUNCTION__, __LINE__);
goto exit1;
}
HSTRING_HEADER header_ToastNotificationManagerHString;
HSTRING ToastNotificationManagerHString;
hr = WindowsCreateStringReference(
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
(UINT32)__wcslen(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager),
&header_ToastNotificationManagerHString,
&ToastNotificationManagerHString
);
if (FAILED(hr))
{
printf("%s:%d:: WindowsCreateStringReference\n", __FUNCTION__, __LINE__);
goto exit2;
}
if (ToastNotificationManagerHString == NULL)
{
printf("%s:%d:: ToastNotificationManagerHString == NULL\n", __FUNCTION__, __LINE__);
hr = E_POINTER;
goto exit2;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotificationManagerStatics* toastStatics = NULL;
hr = RoGetActivationFactory(
ToastNotificationManagerHString,
&UIID_IToastNotificationManagerStatics,
(LPVOID*)&toastStatics
);
if (FAILED(hr))
{
printf("%s:%d:: RoGetActivationFactory\n", __FUNCTION__, __LINE__);
goto exit2;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotifier* notifier;
hr = toastStatics->lpVtbl->CreateToastNotifierWithId(
toastStatics,
AppIdHString,
¬ifier
);
if (FAILED(hr))
{
printf("%s:%d:: CreateToastNotifierWithId\n", __FUNCTION__, __LINE__);
goto exit3;
}
HSTRING_HEADER header_ToastNotificationHString;
HSTRING ToastNotificationHString;
hr = WindowsCreateStringReference(
RuntimeClass_Windows_UI_Notifications_ToastNotification,
(UINT32)__wcslen(RuntimeClass_Windows_UI_Notifications_ToastNotification),
&header_ToastNotificationHString,
&ToastNotificationHString
);
if (FAILED(hr))
{
printf("%s:%d:: WindowsCreateStringReference\n", __FUNCTION__, __LINE__);
goto exit4;
}
if (ToastNotificationHString == NULL)
{
printf("%s:%d:: ToastNotificationHString == NULL\n", __FUNCTION__, __LINE__);
hr = E_POINTER;
goto exit4;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotificationFactory* notifFactory = NULL;
hr = RoGetActivationFactory(
ToastNotificationHString,
&UIID_IToastNotificationFactory,
(LPVOID*)¬ifFactory
);
if (FAILED(hr))
{
printf("%s:%d:: RoGetActivationFactory\n", __FUNCTION__, __LINE__);
goto exit4;
}
__x_ABI_CWindows_CUI_CNotifications_CIToastNotification* toast = NULL;
hr = notifFactory->lpVtbl->CreateToastNotification(notifFactory, inputXml, &toast);
if (FAILED(hr))
{
printf("%s:%d:: CreateToastNotification\n", __FUNCTION__, __LINE__);
goto exit5;
}
hr = notifier->lpVtbl->Show(notifier, toast);
if (FAILED(hr))
{
printf("%s:%d:: Show\n", __FUNCTION__, __LINE__);
goto exit6;
}
// We have to wait a bit for the COM threads to deliver the notification
// to the system, I think
// Don't know any better, yielding (Sleep(0)) is not enough
Sleep(1);
exit6:
toast->lpVtbl->Release(toast);
exit5:
notifFactory->lpVtbl->Release(notifFactory);
exit4:
notifier->lpVtbl->Release(notifier);
exit3:
toastStatics->lpVtbl->Release(toastStatics);
exit2:
inputXml->lpVtbl->Release(inputXml);
exit1:
RoUninitialize();
exit0:
Done(hr);
}
最新版本:https://gist.github.com/valinet/3283c79ba35fc8f103c747c8adbb6b23
关于c++ - 从纯 C 使用 Windows 运行时 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65387849/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!