From 6df7fec2b0d39f5b18781176e65eb796007d0363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludwig=20Fr=C3=BChsch=C3=BCtz?= Date: Tue, 23 Mar 2021 00:52:58 +0100 Subject: [PATCH] Created dll project, already including basic AHK interface functionality --- OSC2AHK.sln | 31 ++++++++ OSC2AHK/OSC2AHK.vcxproj | 170 ++++++++++++++++++++++++++++++++++++++++ OSC2AHK/OSC2AHK.vcxproj.filters | 36 +++++++++ OSC2AHK/dllmain.cpp | 81 +++++++++++++++++++ OSC2AHK/dllmain.h | 12 +++ OSC2AHK/framework.h | 5 ++ OSC2AHK/pch.cpp | 5 ++ OSC2AHK/pch.h | 13 +++ msgtest.ahk | 44 +++++++++++ 9 files changed, 397 insertions(+) create mode 100644 OSC2AHK.sln create mode 100644 OSC2AHK/OSC2AHK.vcxproj create mode 100644 OSC2AHK/OSC2AHK.vcxproj.filters create mode 100644 OSC2AHK/dllmain.cpp create mode 100644 OSC2AHK/dllmain.h create mode 100644 OSC2AHK/framework.h create mode 100644 OSC2AHK/pch.cpp create mode 100644 OSC2AHK/pch.h create mode 100644 msgtest.ahk diff --git a/OSC2AHK.sln b/OSC2AHK.sln new file mode 100644 index 0000000..72790ae --- /dev/null +++ b/OSC2AHK.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31112.23 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OSC2AHK", "OSC2AHK\OSC2AHK.vcxproj", "{637B560B-0B6A-4EF8-B45B-61459545F6A3}" +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 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Debug|x64.ActiveCfg = Debug|x64 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Debug|x64.Build.0 = Debug|x64 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Debug|x86.ActiveCfg = Debug|Win32 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Debug|x86.Build.0 = Debug|Win32 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Release|x64.ActiveCfg = Release|x64 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Release|x64.Build.0 = Release|x64 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Release|x86.ActiveCfg = Release|Win32 + {637B560B-0B6A-4EF8-B45B-61459545F6A3}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C78C981F-16A4-4CF8-9342-48C28379B77C} + EndGlobalSection +EndGlobal diff --git a/OSC2AHK/OSC2AHK.vcxproj b/OSC2AHK/OSC2AHK.vcxproj new file mode 100644 index 0000000..0580ecc --- /dev/null +++ b/OSC2AHK/OSC2AHK.vcxproj @@ -0,0 +1,170 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {637b560b-0b6a-4ef8-b45b-61459545f6a3} + OSC2AHK + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;OSC2AHK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;OSC2AHK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + _DEBUG;OSC2AHK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + NDEBUG;OSC2AHK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + + + + + + + Create + Create + Create + Create + + + + + + \ No newline at end of file diff --git a/OSC2AHK/OSC2AHK.vcxproj.filters b/OSC2AHK/OSC2AHK.vcxproj.filters new file mode 100644 index 0000000..0db63f1 --- /dev/null +++ b/OSC2AHK/OSC2AHK.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/OSC2AHK/dllmain.cpp b/OSC2AHK/dllmain.cpp new file mode 100644 index 0000000..bfd8815 --- /dev/null +++ b/OSC2AHK/dllmain.cpp @@ -0,0 +1,81 @@ +// dllmain.cpp : Defines the entry point for the DLL application. +#include "pch.h" +#include "dllmain.h" +#include +#include + +/* Definition and declaration of the OSC Listeners. +* Contains OSC addresses to listen to and OS message ID to send for each +* address */ +struct Listener { + std::string address; + unsigned int message; +}; +std::vector listeners; +/* Handle to the calling window */ +HWND hwnd = NULL; + +BOOL APIENTRY DllMain( HMODULE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved + ) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + break; + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + close(); + break; + } + return TRUE; +} + +DLLEXPORT int open(HWND targetWindowHandle, unsigned int port) +{ + hwnd = targetWindowHandle; + return 0; +} + +DLLEXPORT int close() +{ + hwnd = NULL; + listeners.clear(); + return 0; +} + +DLLEXPORT int addListener(std::string address, unsigned int messageID) +{ + listeners.push_back(Listener{ address, messageID }); + return 0; +} + +DLLEXPORT int removeListener(std::string address) +{ + UINT result = 1; + + //All OSC addresses have to start with a '/' + if (address[0] != '/') address.insert(0, "/"); + + for (UINT i = 0; i < listeners.size(); i++) + { + if (listeners[i].address == address) + { + listeners.erase(listeners.begin() + i); + i--; //one step backward, what previously was i+1 is i after erase(). + result = 0; //Found at least one occurence + } + } + return result; +} + +/* Debugging function to test messaging. Probably will be removed later. */ +DLLEXPORT int testMsg(HWND windowHandle, unsigned int messageID) +{ + if (PostMessage(windowHandle, messageID, 42, 43)) + return 0; + else + return 1; +} diff --git a/OSC2AHK/dllmain.h b/OSC2AHK/dllmain.h new file mode 100644 index 0000000..45597ff --- /dev/null +++ b/OSC2AHK/dllmain.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +#define DLLEXPORT __declspec(dllexport) + +extern "C" DLLEXPORT int open(HWND targetWindowHandle, unsigned int port); +extern "C" DLLEXPORT int close(); +extern "C" DLLEXPORT int addListener(std::string address, unsigned int messageID); +extern "C" DLLEXPORT int removeListener(std::string address); +extern "C" DLLEXPORT int testMsg(HWND windowHandle, unsigned int messageID); diff --git a/OSC2AHK/framework.h b/OSC2AHK/framework.h new file mode 100644 index 0000000..54b83e9 --- /dev/null +++ b/OSC2AHK/framework.h @@ -0,0 +1,5 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +// Windows Header Files +#include diff --git a/OSC2AHK/pch.cpp b/OSC2AHK/pch.cpp new file mode 100644 index 0000000..64b7eef --- /dev/null +++ b/OSC2AHK/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: source file corresponding to the pre-compiled header + +#include "pch.h" + +// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. diff --git a/OSC2AHK/pch.h b/OSC2AHK/pch.h new file mode 100644 index 0000000..885d5d6 --- /dev/null +++ b/OSC2AHK/pch.h @@ -0,0 +1,13 @@ +// pch.h: This is a precompiled header file. +// Files listed below are compiled only once, improving build performance for future builds. +// This also affects IntelliSense performance, including code completion and many code browsing features. +// However, files listed here are ALL re-compiled if any one of them is updated between builds. +// Do not add files here that you will be updating frequently as this negates the performance advantage. + +#ifndef PCH_H +#define PCH_H + +// add headers that you want to pre-compile here +#include "framework.h" + +#endif //PCH_H diff --git a/msgtest.ahk b/msgtest.ahk new file mode 100644 index 0000000..28b3762 --- /dev/null +++ b/msgtest.ahk @@ -0,0 +1,44 @@ +#SingleInstance, Force +SendMode Input +SetWorkingDir, %A_ScriptDir% +hWnd := WinExist() + +stdout := FileOpen("tmp.log", "w") +stdout.WriteLine("START") + +retOnMsg := OnMessage(0x1000, "msghandler") +stdout.WriteLine("OnMessage:") +stdout.WriteLine(retOnMsg) +OnExit("exithandler") + +hModule := DllCall("LoadLibrary", "Str", "x64\Debug\OSC2AHK.dll", "Ptr") ; Avoids the need for DllCall() in the loop to load the library. +stdout.WriteLine("DLL handle:") +stdout.WriteLine(hModule) + +ret := DllCall("OSC2AHK.dll\testMsg", UInt,hWnd, UInt,0x1000) +stdout.WriteLine("DLL call:") +stdout.WriteLine(ret) + +stdout.Close() + +return + +msghandler(wParam, lParam, msg, hwnd) { + stdout := FileOpen("tmp.log", "a") + stdout.WriteLine("Received MSG!") + stdout.WriteLine(wParam) + stdout.WriteLine(lParam) + stdout.Close() +} + +exithandler() { + stdout := FileOpen("tmp.log", "a") + stdout.WriteLine("The End") + stdout.Close() +} + +do_exit: +ExitApp +return ; unnecessary redundancy? + +Esc::GoSub, do_exit \ No newline at end of file