aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2022-09-09 17:50:28 +0200
committerToni Uhlig <matzeton@googlemail.com>2022-09-09 18:24:21 +0200
commit51521cb642358770e94b8f9b4f40dd3b4c827cad (patch)
tree4795b8bffcdf2b628d9c9be15a4ebb1150f9f760 /examples
parent3be8cccbdbb548a4538d23470aa20e65b33e7815 (diff)
Repository clean up, renamed ddk-template* to dpp-example*.
* Improved/Added root and examples Makefile * Adapted CIs Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile59
-rwxr-xr-xexamples/dpp-example-cplusplus-EASTL.bat27
-rw-r--r--examples/dpp-example-cplusplus-EASTL.cpp123
-rwxr-xr-xexamples/dpp-example-cplusplus.bat27
-rw-r--r--examples/dpp-example-cplusplus.cpp126
-rwxr-xr-xexamples/dpp-example.bat27
-rw-r--r--examples/dpp-example.c21
7 files changed, 410 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile
new file mode 100644
index 0000000..d260b22
--- /dev/null
+++ b/examples/Makefile
@@ -0,0 +1,59 @@
+ifndef DPP_ROOT
+$(error DPP_ROOT is undefined, use `make DPP_ROOT=$(realpath ..)`)
+endif
+
+include $(DPP_ROOT)/Makefile.inc
+
+1_DRIVER_NAME = dpp-example
+1_OBJECTS = $(1_DRIVER_NAME).o
+1_TARGET = $(1_DRIVER_NAME).sys
+
+2_DRIVER_NAME = dpp-example-cplusplus
+2_OBJECTS = $(2_DRIVER_NAME).opp
+2_TARGET = $(2_DRIVER_NAME).sys
+
+3_DRIVER_NAME = dpp-example-cplusplus-EASTL
+3_OBJECTS = $(3_DRIVER_NAME).opp
+3_TARGET = $(3_DRIVER_NAME).sys
+
+ifdef BUILD_NATIVE
+$(error There is nothing the build as native executable here..)
+endif
+
+all: $(1_TARGET) $(2_TARGET) $(3_TARGET)
+
+include $(DPP_ROOT)/Makefile.inc
+
+%.o: %.c
+ $(call BUILD_C_OBJECT,$<,$@)
+
+%.opp: %.cpp
+ $(call BUILD_CPP_OBJECT,$<,$@)
+
+# simple C driver
+$(1_TARGET): $(1_OBJECTS)
+ $(call LINK_C_KERNEL_TARGET,$(1_OBJECTS),$@)
+
+# C++ driver w/ MT
+$(2_TARGET): $(2_OBJECTS)
+ $(call LINK_CPP_KERNEL_TARGET,$(2_OBJECTS),$@)
+
+# C++ driver w/ EASTL
+$(3_TARGET): $(3_OBJECTS)
+ $(call LINK_CPP_KERNEL_TARGET,$(3_OBJECTS),$@)
+
+install: all
+ $(call INSTALL_EXEC_SIGN,$(1_TARGET))
+ $(call INSTALL_EXEC_SIGN,$(2_TARGET))
+ $(call INSTALL_EXEC_SIGN,$(3_TARGET))
+ $(INSTALL) $(1_DRIVER_NAME).bat $(DESTDIR)
+ $(INSTALL) $(2_DRIVER_NAME).bat $(DESTDIR)
+ $(INSTALL) $(3_DRIVER_NAME).bat $(DESTDIR)
+
+clean:
+ rm -f $(1_OBJECTS) $(1_TARGET) $(1_TARGET).map
+ rm -f $(2_OBJECTS) $(2_TARGET) $(2_TARGET).map
+ rm -f $(3_OBJECTS) $(3_TARGET) $(3_TARGET).map
+
+.PHONY: all install clean
+.DEFAULT_GOAL := all
diff --git a/examples/dpp-example-cplusplus-EASTL.bat b/examples/dpp-example-cplusplus-EASTL.bat
new file mode 100755
index 0000000..9793eee
--- /dev/null
+++ b/examples/dpp-example-cplusplus-EASTL.bat
@@ -0,0 +1,27 @@
+@echo off
+set SERVICE_NAME=DPP-Example-CPlusPlus-EASTL
+set DRIVER="%~dp0\dpp-example-cplusplus-EASTL.sys"
+
+net session >nul 2>&1
+if NOT %ERRORLEVEL% EQU 0 (
+ echo ERROR: This script requires Administrator privileges!
+ pause
+ exit /b 1
+)
+
+echo ---------------------------------------
+echo -- Service Name: %SERVICE_NAME%
+echo -- Driver......: %DRIVER%
+echo ---------------------------------------
+
+sc create %SERVICE_NAME% binPath= %DRIVER% type= kernel
+echo ---------------------------------------
+sc start %SERVICE_NAME%
+echo ---------------------------------------
+sc query %SERVICE_NAME%
+echo [PRESS A KEY TO STOP THE DRIVER]
+pause
+sc stop %SERVICE_NAME%
+sc delete %SERVICE_NAME%
+echo Done.
+timeout /t 3
diff --git a/examples/dpp-example-cplusplus-EASTL.cpp b/examples/dpp-example-cplusplus-EASTL.cpp
new file mode 100644
index 0000000..b57815d
--- /dev/null
+++ b/examples/dpp-example-cplusplus-EASTL.cpp
@@ -0,0 +1,123 @@
+#include <ntddk.h>
+
+#include <cstdint>
+
+#include <EASTL/functional.h>
+#include <EASTL/hash_map.h>
+#include <EASTL/random.h>
+#include <EASTL/scoped_ptr.h>
+#include <EASTL/set.h>
+#include <EASTL/shared_ptr.h>
+#include <EASTL/sort.h>
+#include <EASTL/string.h>
+#include <EASTL/map.h>
+#include <EASTL/unordered_map.h>
+#include <EASTL/unordered_set.h>
+#include <EASTL/vector.h>
+
+// C&P from: https://raw.githubusercontent.com/sidyhe/dxx/ed06aba3b91fe8e101d08c33c26ba73db96acef0/README.md
+void stl_test()
+{
+ eastl::make_unique<DRIVER_OBJECT>();
+ eastl::make_shared<UNICODE_STRING>();
+ eastl::scoped_ptr<double> dptr(new double(3.6));
+
+ eastl::set<int> set_test;
+ set_test.insert(1);
+ set_test.insert(3);
+ set_test.insert(5);
+ set_test.erase(1);
+
+ eastl::map<int, int> map_test;
+ map_test[0] = 1;
+ map_test[10] = 11;
+ map_test[20] = 12;
+ map_test.erase(11);
+
+ eastl::vector<int> vec_test;
+ vec_test.push_back(2);
+ vec_test.push_back(3);
+ vec_test.push_back(1);
+ eastl::stable_sort(vec_test.begin(), vec_test.end(), eastl::less<int>());
+ for (auto e : vec_test)
+ {
+ DbgPrint("%d\n", e);
+ }
+
+ eastl::string s;
+ s = "This a string";
+ s.append(" ");
+ s.append("any");
+ DbgPrint("%s\n", s.c_str());
+
+ eastl::wstring ws;
+ ws = L"wide string";
+ ws.clear();
+
+ eastl::unordered_set<float> us_test;
+ us_test.insert(333);
+
+ eastl::unordered_map<double, eastl::string> um_test;
+ um_test.insert(eastl::make_pair(6.6, "9.9"));
+}
+
+void more_stl_test()
+{
+ eastl::hash_map<int, eastl::string> hm;
+
+ hm[0] = "test1";
+ hm[10] = "test2";
+ hm[20] = "test3";
+ for (auto s : hm)
+ {
+ DbgPrint("%s\n", s.second.c_str());
+ }
+
+ eastl::uniform_int_distribution<std::uint32_t> uid(1, UINT32_MAX);
+ DbgPrint("PRNG: %u\n", uid);
+
+ auto lambda = [] { DbgPrint("Hello lambda!\n"); };
+ eastl::function<void(void)> fn = lambda;
+ fn();
+
+ auto lambda2 = [](int n) {
+ DbgPrint("Hello lambda2, %u!\n", n);
+ return n;
+ };
+ eastl::function<int(int)> fn2 = lambda2;
+ fn2(1337);
+
+ eastl::vector<std::uint32_t> fill_me;
+ for (auto i = UINT16_MAX; i > 0; --i)
+ {
+ fill_me.push_back(i);
+ }
+ DbgPrint("fill_me size: %zu\n", fill_me.size());
+}
+
+extern "C"
+{
+
+ DRIVER_INITIALIZE DriverEntry;
+ DRIVER_UNLOAD DriverUnload;
+
+ NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
+ {
+ (void)DriverObject;
+ (void)RegistryPath;
+
+ DbgPrint("%s\n", "Hello ring0!");
+
+ stl_test();
+ more_stl_test();
+
+ return STATUS_SUCCESS;
+ }
+
+ VOID DriverUnload(PDRIVER_OBJECT DriverObject)
+ {
+ (void)DriverObject;
+
+ DbgPrint("%s\n", "Bye ring0!");
+ }
+}
diff --git a/examples/dpp-example-cplusplus.bat b/examples/dpp-example-cplusplus.bat
new file mode 100755
index 0000000..d19d3bf
--- /dev/null
+++ b/examples/dpp-example-cplusplus.bat
@@ -0,0 +1,27 @@
+@echo off
+set SERVICE_NAME=DPP-Example-CPlusPlus
+set DRIVER="%~dp0\dpp-example-cplusplus.sys"
+
+net session >nul 2>&1
+if NOT %ERRORLEVEL% EQU 0 (
+ echo ERROR: This script requires Administrator privileges!
+ pause
+ exit /b 1
+)
+
+echo ---------------------------------------
+echo -- Service Name: %SERVICE_NAME%
+echo -- Driver......: %DRIVER%
+echo ---------------------------------------
+
+sc create %SERVICE_NAME% binPath= %DRIVER% type= kernel
+echo ---------------------------------------
+sc start %SERVICE_NAME%
+echo ---------------------------------------
+sc query %SERVICE_NAME%
+echo [PRESS A KEY TO STOP THE DRIVER]
+pause
+sc stop %SERVICE_NAME%
+sc delete %SERVICE_NAME%
+echo Done.
+timeout /t 3
diff --git a/examples/dpp-example-cplusplus.cpp b/examples/dpp-example-cplusplus.cpp
new file mode 100644
index 0000000..c1d9b29
--- /dev/null
+++ b/examples/dpp-example-cplusplus.cpp
@@ -0,0 +1,126 @@
+#include <ntddk.h>
+
+#include <DriverThread.hpp>
+
+class TestSmth
+{
+public:
+ TestSmth()
+ {
+ DbgPrint("%s\n", "ctor");
+ }
+ ~TestSmth()
+ {
+ DbgPrint("%s\n", "dtor");
+ }
+ void doSmth(void)
+ {
+ DbgPrint("%s\n", "Hello Class!");
+ }
+};
+static TestSmth * cdtor_test;
+
+class Derived : public TestSmth
+{
+public:
+ Derived()
+ {
+ }
+ ~Derived()
+ {
+ }
+ void doSmth(void)
+ {
+ DbgPrint("%s\n", "Hello Derived!");
+ }
+};
+
+class DerivedWithCDtor : public Derived
+{
+public:
+ explicit DerivedWithCDtor(unsigned int value)
+ {
+ some_value = value;
+ DbgPrint("%s\n", "DerivedWithCDtor-Ctor.");
+ }
+ ~DerivedWithCDtor()
+ {
+ DbgPrint("%s\n", "DerivedWithCDtor-Dtor.");
+ }
+ void doSmth(void)
+ {
+ DbgPrint("SomeValue: %X\n", some_value);
+ }
+
+private:
+ unsigned int some_value = 0;
+};
+
+static DerivedWithCDtor some_static(0xDEADC0DE);
+
+struct threadContext
+{
+ DriverThread::Semaphore sem;
+ DriverThread::Thread dth;
+};
+
+static NTSTATUS threadRoutine(PVOID threadContext)
+{
+ DbgPrint("ThreadRoutine %p, ThreadContext: %p\n", threadRoutine, threadContext);
+ for (size_t i = 3; i > 0; --i)
+ {
+ DbgPrint("ThreadLoop: %zu\n", i);
+ }
+ struct threadContext * const ctx = (struct threadContext *)threadContext;
+ DbgPrint("Fin. ThreadId: %p\n", ctx->dth.GetThreadId());
+ ctx->sem.Release();
+ DbgPrint("Thread WaitForTermination: 0x%X\n", ctx->dth.WaitForTermination()); // must return STATUS_UNSUCCESSFUL;
+
+ return STATUS_SUCCESS;
+}
+
+static void test_cplusplus(void)
+{
+ TestSmth t;
+ t.doSmth();
+ Derived d;
+ d.doSmth();
+
+ struct threadContext ctx;
+ ctx.dth.Start(threadRoutine, (PVOID)&ctx);
+ ctx.sem.Wait();
+ DbgPrint("MainThread semaphore signaled.\n");
+ ctx.dth.WaitForTermination();
+ ctx.dth.WaitForTermination();
+ DbgPrint("MainThread EOF\n");
+
+ some_static.doSmth();
+}
+
+extern "C"
+{
+
+ DRIVER_INITIALIZE DriverEntry;
+ DRIVER_UNLOAD DriverUnload;
+
+ NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
+ {
+ (void)DriverObject;
+ (void)RegistryPath;
+
+ DbgPrint("%s\n", "Hello ring0!");
+ cdtor_test = new TestSmth();
+
+ test_cplusplus();
+
+ return STATUS_SUCCESS;
+ }
+
+ VOID DriverUnload(PDRIVER_OBJECT DriverObject)
+ {
+ (void)DriverObject;
+
+ delete cdtor_test;
+ DbgPrint("%s\n", "Bye ring0!");
+ }
+}
diff --git a/examples/dpp-example.bat b/examples/dpp-example.bat
new file mode 100755
index 0000000..cada015
--- /dev/null
+++ b/examples/dpp-example.bat
@@ -0,0 +1,27 @@
+@echo off
+set SERVICE_NAME=DPP-Example
+set DRIVER="%~dp0\dpp-example.sys"
+
+net session >nul 2>&1
+if NOT %ERRORLEVEL% EQU 0 (
+ echo ERROR: This script requires Administrator privileges!
+ pause
+ exit /b 1
+)
+
+echo ---------------------------------------
+echo -- Service Name: %SERVICE_NAME%
+echo -- Driver......: %DRIVER%
+echo ---------------------------------------
+
+sc create %SERVICE_NAME% binPath= %DRIVER% type= kernel
+echo ---------------------------------------
+sc start %SERVICE_NAME%
+echo ---------------------------------------
+sc query %SERVICE_NAME%
+echo [PRESS A KEY TO STOP THE DRIVER]
+pause
+sc stop %SERVICE_NAME%
+sc delete %SERVICE_NAME%
+echo Done.
+timeout /t 3
diff --git a/examples/dpp-example.c b/examples/dpp-example.c
new file mode 100644
index 0000000..8a14af9
--- /dev/null
+++ b/examples/dpp-example.c
@@ -0,0 +1,21 @@
+#include <ntddk.h>
+
+DRIVER_INITIALIZE DriverEntry;
+DRIVER_UNLOAD DriverUnload;
+
+NTSTATUS DriverEntry(struct _DRIVER_OBJECT * DriverObject, PUNICODE_STRING RegistryPath)
+{
+ (void)DriverObject;
+ (void)RegistryPath;
+
+ DbgPrint("%s\n", "Hello ring0!");
+
+ return STATUS_SUCCESS;
+}
+
+VOID DriverUnload(struct _DRIVER_OBJECT * DriverObject)
+{
+ (void)DriverObject;
+
+ DbgPrint("%s\n", "Bye ring0!");
+}