Skip to content

Commit

Permalink
bugfix in apifuncs.h made HHOOK atomic, constructor and destructor in…
Browse files Browse the repository at this point in the history
… HookProcDllWrapper no longer auto add and remove hooks. Ready for a new release version.
  • Loading branch information
calebtt committed Jan 17, 2022
1 parent 920d2d2 commit ebc11a3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ Temporary Items
/HookSpeechSharp/bin/Debug/net6.0-windows
/HookSpeechSharp/obj
/.vs
/HookProcDLL/x64/Release
/HookSpeechSharp/bin/Release/net6.0-windows
/HookSpeechSharp/Properties/PublishProfiles
6 changes: 5 additions & 1 deletion HookProcDLL/HookProc DLL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)HookSpeechSharp\bin\Release\net6.0-windows\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand All @@ -103,7 +104,7 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;HOOKPROCDLL_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<CallingConvention>StdCall</CallingConvention>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
</ClCompile>
Expand Down Expand Up @@ -136,6 +137,9 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;HOOKPROCDLL_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
14 changes: 9 additions & 5 deletions HookProcDLL/apifuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
#include "stdafx.h"
#include "SendAlphabet.h"
#include <fstream>
#include <format>
#include <atomic>
extern "C"
{
namespace StaticInstance
{
inline static sds::SendAlphabet sender;
inline static std::string workingAlphabetChars;
inline static std::wstring translationAlphabetChars;
inline static HHOOK hookHandle = NULL;
//inline static HHOOK hookHandle = NULL;
inline static std::atomic<HHOOK> hookHandle = NULL;
inline static constexpr size_t SZ_MODULE_NAME = 512;
}
// Forward declarations section.
Expand Down Expand Up @@ -62,10 +65,11 @@ extern "C"
}
__declspec(dllexport) inline bool RemoveHooks()
{
BOOL res = ::UnhookWindowsHookEx(StaticInstance::hookHandle);
if (res)
StaticInstance::hookHandle = NULL;
return res;
//From Winapi docs, it's more of a fire and forget because it can be in the state of processing
//a key event and not unhook immediately.
::UnhookWindowsHookEx(StaticInstance::hookHandle);
StaticInstance::hookHandle = NULL;
return true;
}
//Can be used in the future, a mouse hook procedure.
inline LRESULT mouseHookProcedure(int code, WPARAM wParam, LPARAM lParam)
Expand Down
1 change: 1 addition & 0 deletions HookSpeechSharp/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions HookSpeechSharp/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public partial class HssMainForm : Form
public HssMainForm()
{
InitializeComponent();
dllWrapper.AddHooks();
UpdateTranslationBox();
}

Expand Down Expand Up @@ -44,5 +45,10 @@ private void btnUpdateAlphabet_Click(object sender, EventArgs e)
}
UpdateTranslationBox();
}

private void HssMainForm_FormClosing(object sender, FormClosingEventArgs e)
{
dllWrapper.RemoveHooks();
}
}
}
19 changes: 16 additions & 3 deletions HookSpeechSharp/HookProcDllWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,44 @@ public class HookProcDllWrapper
{
public HookProcDllWrapper()
{
HookProcDllImports.AddHooks();
}

~HookProcDllWrapper()
{
HookProcDllImports.RemoveHooks();
if (HookProcDllImports.IsHooked())
HookProcDllImports.RemoveHooks();
}

public string GetTranslationAlphabet()
{
bool wasHooked = IsHooked();
if (wasHooked)
RemoveHooks();
IntPtr p = HookProcDllImports.GetTranslationAlphabet();
if (p != IntPtr.Zero)
{
string? retVal = Marshal.PtrToStringUni(p);
if (wasHooked)
AddHooks();
if (retVal != null)
return retVal;
else
return String.Empty;
}
if (wasHooked)
AddHooks();
return String.Empty;
}

public bool SetTranslationAlphabet(string newAlphabet)
{
return HookProcDllImports.SetTranslationAlphabet(newAlphabet);
bool wasHooked = IsHooked();
if (wasHooked)
RemoveHooks();
bool res = HookProcDllImports.SetTranslationAlphabet(newAlphabet);
if (wasHooked)
AddHooks();
return res;
}

public string GetWorkingAlphabet()
Expand Down
15 changes: 9 additions & 6 deletions HookSpeechSharp/HookSpeechSharp.csproj.user
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Update="Form1.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
</Project>
<PropertyGroup>
<_LastSelectedProfileId>C:\Users\caleb\source\repos\HookSpeech\HookSpeechSharp\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
</PropertyGroup>
<ItemGroup>
<Compile Update="Form1.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
</Project>

0 comments on commit ebc11a3

Please sign in to comment.