Skip to content

Commit

Permalink
Working on issuse 35
Browse files Browse the repository at this point in the history
  • Loading branch information
malja committed Jun 13, 2018
1 parent 8e28bd2 commit 3b76aeb
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 3 deletions.
31 changes: 31 additions & 0 deletions WinToast.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2002
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinToast", "WinToast\WinToast.vcxproj", "{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Debug|x64.ActiveCfg = Debug|x64
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Debug|x64.Build.0 = Debug|x64
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Debug|x86.ActiveCfg = Debug|Win32
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Debug|x86.Build.0 = Debug|Win32
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Release|x64.ActiveCfg = Release|x64
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Release|x64.Build.0 = Release|x64
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Release|x86.ActiveCfg = Release|Win32
{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4A73BDC8-677E-4994-AD40-D19AC5F7A245}
EndGlobalSection
EndGlobal
135 changes: 135 additions & 0 deletions WinToast/WinToast.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{B83F8D9B-7888-437D-A8C6-52F4D2A0179A}</ProjectGuid>
<RootNamespace>WinToast</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>C:\Users\janma\Documents\GitHub\WinToast\src;C:\Users\janma\Documents\GitHub\WinToast\WinToast;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;runtimeobject.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\src\wintoastlib.h" />
<ClInclude Include="activator_helper.h" />
<ClInclude Include="iactivator.h" />
<ClInclude Include="event_handler.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\wintoastlib.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
39 changes: 39 additions & 0 deletions WinToast/WinToast.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Zdrojové soubory">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Hlavičkové soubory">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Soubory zdrojů">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\wintoastlib.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
<ClInclude Include="event_handler.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
<ClInclude Include="iactivator.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
<ClInclude Include="activator_helper.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\wintoastlib.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions WinToast/WinToast.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
81 changes: 81 additions & 0 deletions WinToast/activator_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#define UNICODE //< This define is required for GetModuleFileName to work with wchar_t
#include <Windows.h>
#include <wrl.h>
#include <string>

/*!
* A parameter application is run with when activated from notification
*/
#define TOAST_ACTIVATED_LAUNCH_ARG L"-ToastActivated"

/*!
* Macro for checking return value of HRESULT type for failure.
*/
#define RETURN_IF_FAILED(hr) do { HRESULT _hrTemp = hr; if (FAILED(_hrTemp)) { return _hrTemp; } } while (false)

using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

// Following functions were taken from https://raw.githubusercontent.com/WindowsNotifications/desktop-toasts/master/CPP-WRL/DesktopToastsCppWrlApp/DesktopNotificationManagerCompat.cpp
// mostly without any change.
// In the future, they should be embeded into WinToastLib::Util namespace

HRESULT RegisterComServer(GUID clsid, const wchar_t exePath[]) {

// Turn the GUID into a string
OLECHAR* clsidOlechar;
StringFromCLSID(clsid, &clsidOlechar);
std::wstring clsidStr(clsidOlechar);
::CoTaskMemFree(clsidOlechar);

// Create the subkey
// Something like SOFTWARE\Classes\CLSID\{23A5B06E-20BB-4E7E-A0AC-6982ED6A6041}\LocalServer32
std::wstring subKey = LR"(SOFTWARE\Classes\CLSID\)" + clsidStr + LR"(\LocalServer32)";

// Include -ToastActivated launch args on the exe
std::wstring exePathStr(exePath);
exePathStr = L"\"" + exePathStr + L"\" " + TOAST_ACTIVATED_LAUNCH_ARG;

// We don't need to worry about overflow here as ::GetModuleFileName won't
// return anything bigger than the max file system path (much fewer than max of DWORD).
DWORD dataSize = static_cast<DWORD>((exePathStr.length() + 1) * sizeof(WCHAR));

// Register the EXE for the COM server
return HRESULT_FROM_WIN32(::RegSetKeyValue(
HKEY_CURRENT_USER,
subKey.c_str(),
nullptr,
REG_SZ,
reinterpret_cast<const BYTE*>(exePathStr.c_str()),
dataSize));
}

HRESULT RegisterAumidAndComServer(const wchar_t *aumid, GUID clsid) {

// Get the EXE path
wchar_t exePath[MAX_PATH];
DWORD charWritten = ::GetModuleFileName(nullptr, exePath, ARRAYSIZE(exePath));
RETURN_IF_FAILED(charWritten > 0 ? S_OK : HRESULT_FROM_WIN32(::GetLastError()));

// Register the COM server
RETURN_IF_FAILED(RegisterComServer(clsid, exePath));
return S_OK;
}

HRESULT RegisterActivator() {
// Module<OutOfProc> needs a callback registered before it can be used.
// Since we don't care about when it shuts down, we'll pass an empty lambda here.
Module<OutOfProc>::Create([] {});

// If a local server process only hosts the COM object then COM expects
// the COM server host to shutdown when the references drop to zero.
// Since the user might still be using the program after activating the notification,
// we don't want to shutdown immediately. Incrementing the object count tells COM that
// we aren't done yet.
Module<OutOfProc>::GetModule().IncrementObjectCount();

RETURN_IF_FAILED(Module<OutOfProc>::GetModule().RegisterObjects());

return S_OK;
}

41 changes: 41 additions & 0 deletions WinToast/event_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "wintoastlib.h"

#include <iostream>

class EventHandler : public WinToastLib::IWinToastHandler {

public:

void toastActivated() const override {
std::cout << "Toast Clicked" << std::endl;
}

void toastActivated(int actionIndex) const override {
std::cout << "Action Clicked #" << actionIndex << std::endl;
}

void toastDismissed(WinToastDismissalReason state) const {
switch (state) {

case UserCanceled:
std::cout << "Dissmissed" << std::endl;
break;

case TimedOut:
std::cout << "Timed out" << std::endl;
break;

case ApplicationHidden:
std::cout << "Hidden from app" << std::endl;
break;

default:
std::cout << "Toast not activated" << std::endl;
break;
}
}

void toastFailed() const {
std::cout << "Failed" << std::endl;
}
};
50 changes: 50 additions & 0 deletions WinToast/iactivator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <wrl/implements.h>
#include <NotificationActivationCallback.h>

#include <fstream>
#include <map>
#include <string>

using namespace Microsoft::WRL;

// Following code is an interface for future Activator class.

/*class IWinToastActivator : public RuntimeClass<RuntimeClassFlags<ClassicCom>, INotificationActivationCallback> {
public:
HRESULT STDMETHODCALLTYPE Activate(_In_ LPCWSTR appUserModelId, _In_ LPCWSTR invokedArgs, _In_reads_(dataCount) const NOTIFICATION_USER_INPUT_DATA* data, ULONG dataCount) override {
std::map<std::wstring, std::wstring> params;
for (ULONG i = 0; i < dataCount; i++ ) {
params.insert(std::pair<std::wstring, std::wstring>( (data[i]).Key, (data[i]).Value ));
}
// Call user handler
notificationActivated(std::wstring(appUserModelId), std::wstring(invokedArgs), params);
}
virtual void notificationActivated(const std::wstring &appUserModelId, const std::wstring &parameters, std::map<std::wstring, std::wstring> &user_input) const = 0;
};*/

// This class is just for testing.

class DECLSPEC_UUID("8aa59b6c-1401-42a5-b1cf-3ccb3b360e77") Activator WrlSealed WrlFinal : public RuntimeClass<RuntimeClassFlags<ClassicCom>, INotificationActivationCallback> {
public:

HRESULT STDMETHODCALLTYPE Activate(_In_ LPCWSTR appUserModelId, _In_ LPCWSTR invokedArgs, _In_reads_(dataCount) const NOTIFICATION_USER_INPUT_DATA* data, ULONG dataCount) override {

// Change username
std::wofstream file("C:\\Users\\<uname>\\Desktop");

file << appUserModelId << std::endl;
file << invokedArgs << std::endl;

for (ULONG i = 0; i < dataCount; i++) {
file << (data[i]).Key << " " << (data[i]).Value << std::endl;
}

return S_OK;
}
};

CoCreatableClass(Activator);
Loading

0 comments on commit 3b76aeb

Please sign in to comment.