Have you ever struggled with hosting your own DLLs as Windows services using svchost.exe? Many developers face challenges due to the undocumented and restrictive nature of svchost.exe. That’s where SvcHostify comes in—a lightweight, open-source tool designed to make hosting custom DLL services easier than ever.
Why SvcHostify?
SvcHostify eliminates the complexity of writing svchost-compatible services. Whether you’re coding in Java, C#, or C++, this tool handles the heavy lifting, letting you focus on your application logic. No more worries about C-style exports or system quirks.
Support for multiple languages: Build services in Java, C#, or C++ without worrying about low-level plumbing.
Two hosting modes:
SvcHost Mode: For academic/research purposes.
Standalone Mode: Run services using rundll32.exe—perfect for production.
Streamlined JSON configuration: Define service metadata, runtime behavior, and logging in one simple file.
What Can You Do with SvcHostify?
Host Java applications via JVM integration.
Run .NET DLLs as in-process COM servers.
Manage lightweight C++ services by exporting minimalistic C-style functions.
Features You’ll Love
Effortless Service Management: Install and uninstall services in seconds with simple commands.
A large number of open-source C++ projects choose Modern CMake as a powerful tool used for managing the building process across multiple platforms. Modern CMake refers to the practices, features, and methodologies introduced in CMake 3.x (and beyond) that simplify, improve, and modernize the build system configuration for C++ and other languages. It emphasizes clarity, reusability, and portability, and aims to make CMake easier to maintain while leveraging its full potential.
For the most part, the CMAKE_<LANG>_FLAGS CMake variable is provided for consumers to set additional compiler flags for the <LANG> programming language. For example, CMAKE_CXX_FLAGS impacts the current C++ compiler and CMAKE_C_FLAGS will take effect when building a C target in the subsequent tasks.
The CMAKE_<LANG>_FLAGS variable will always override both Release and Debug targets. To set flags separately for one single build type, extra variables like CMAKE_<LANG>_FLAGS_RELEASE and CMAKE_<LANG>_FLAGS_DEBUG are introduced for such kind of usage.
On Windows, Visual Studio 2015 and later versions of Visual Studio all use one Universal Visual C Runtime Library, called the Universal CRT (UCRT). The UCRT is a Microsoft Windows operating system component. It’s included as part of the operating system in Windows 10 or later, and Windows Server 2016 or later. The Visual C++ Runtime Library is usually distributed with the corresponding MSVC tooling or installed independently with third-party software. The VC++ Runtime Library depends on the UCRT in the operating system.
The UCRT and the VC++ Runtime have static and dynamic libraries within MSVC and thereby the MSVC’s compiler cl.exe provides several options for linkage mode control of the two runtimes simultaneously. The options are:
Option (Release)
Option (Debug)
Description
/MD
/MDd
Linking to UCRT & VC++ Runtime statically
/MT
/MTd
Linking to UCRT & VC++ Runtime dynamically
For GCC on Linux or Unix platform, this compiler is designed to use options, such as -static-libstdc++ and -static-libgcc, to enable the static libraries of libstdc++.a, libgcc.a, or libc++.a. The libgcc is known as the GCC Low-level Runtime Library, which exists on some platform and GCC generates calls to routines in this library automatically, whenever it needs to perform some operation that is too complicated to emit inline code for.
For Clang on these platforms, it contains the same options as GCC has for compatibility considerations. The options in Clang are aliases to -static-libc++ and -static-compiler-rt, which are identical to those of GCC.
Compiler
Option
Description
GCC
-static-libstdc++
Linking to libstdc++ statically
GCC
-static-libgcc
Linking to libgcc statically
Clang
-static-libc++ -static-libstdc++
Linking to libc++ statically
Clang
-static-libgcc -static-compiler-rt
Linking to libcompiler-rt statically.
In a C++ project, it is recommended to keep the same configuration of compilation for all dependencies. Because of different compilers and option names, it is a bit complex to write CMake code in a portable way and users may set the options via -DCMAKE_<LANG>_FLAGS=xxx in the command line, that could lead to incorrect building configurations or an unexpected behavior.
A solution is to define a CMake option in convention, e.g. WITH_STATIC_RUNTIME, indicating that it is the official method to modify the runtime linkage mode.
option(WITH_STATIC_RUNTIME OFF"Linking to the C++ runtime statically.")
A user-defined CMake function is constructed here to generate flags for different compilers correspondingly, complying the aforementioned rules.
The es_ensure_parameters function raises a fatal error when the mandatory parameters do not exist. Here is an example showing how to use this function:
Another design goal is exception safety, that is whatever users set the flags to never corrupts the building process. A workaround is to clear the old runtime flags first and set the new flags after that. A possible implementation is to iterate all the variables to remove conflicting flags and then append the generated flags to these variables. The string(REPLACE ...) function is capable of text replacement operations.
In CMake, a list is actually strings separated by semicolons, so an ordinary string can be transformed to a list after inserting semicolons within it. The CMAKE_<LANG>_FLAGS and CMAKE_<LANG>_<BUILD_TYPE> variables use spaces as delimiters and can be passed to the foreach statement by replacing all spaces with semicolons. It is viable to retrieve conflicting flags by reversing the WITH_STATIC_RUNTIME option.
If all the above code are located inside a CMake function, which has its own inner scope of variables. Extra set statements are necessary to copy the inside variables to the outer ones.
Well done! Everything might be OK now. The full application can be checked at my personal repo named “Cpp Essence” on GitHub. I will appreciate your stars and contributions there! Big thanks.
If the installed host GCC by default on common Linux distributions has a version not fitting to your requirements, you can build from the source by yourself and make it coexist with the installed one.
The list of released GNU source packages can be checked on the GNU FTP Site. You can see several directories named gcc-x.x.x refering to specific versions of GCC source packages.
wget or curl are recommended commands on Linux to download the package from the website. For example, you can type the following command to retrieve the GCC 14.2.0 package and decompress it to the current directory.
The compilation of the newer version of GCC depends on the installed GCC on the host operating system, so it is necessary to ensure the whole build system has been equipped correctly. For instance, just use apt on Ubuntu to install the dependencies.
There are a few prerequisites e.g. GMP, MPFR, MPC and ISL needed to be installed before compiling the GCC. Just cd into the source directory and run the script below to download and get them all geared up. The http_proxy and https_proxy environment variables are mandatory if the machine’s internet connection is restricted for some reason.
The next step is to configure the project to generate Makefile and the build tree. Then run make in the build directory and finally make install. The target folder of the installation is able to be set by the --prefix option. The -j$(nproc) option of the make command means building the project with N threads in parallel where N equals the number of processor cores.
After a success installation, the GCC binaries may be located at bin that is relative to your designated directory by the --prefix option such as /opt/$GUNC above. A user may create a symbolic link to the GCC binaries in the /usr/local/bin directory as follows:
The Windows NT kernel (from WinNT to Windows 11) internally uses UTF-16 strings by default, including Windows Drivers, Native Applications and COM clients and servers, etc. All other common encodings like UTF-8, GBK, GB18030, BIG-5 should always be converted to UTF-16 before invocations to the kernel functions within ntdll.dll.
There is a compatible layer upon the kernel layer, which is called Win32 API, a huge heritage left by the Win9X series. Win32 API is a family of functions for programmers to communicate with the operating system and hardware conveniently. Microsoft keeps this compatibility on the Windows NT Kernel so that most of the Win32 functions are still remaining unchanged and ABI-compatible. For example the CreateFile function does exist from Windows 98 to Windows 11. That is unbelievable because a Linux distribution may break any API in a minor update!
Some challenges started to occur. The Unicode Standard was then generally accepted by OS vendors after Win9X was released while the Win9X was still using the ANSI encoding or some multi-byte encoding in the terminal country like GB2312 and BIG-5. The Windows NT Kernel chose the UTF-16 as its kernel string representations that hindered the working progress of compatibility.
To resolve this issue, Microsoft’s talented engineers decided to create duplicates of the corresponding old Win32 APIs. The only difference of these two versions is the string types: LPCSTR vs LPCWSTR, the aliases of const char* and const wchar_t* in C++. The former represents the ANSI, and the latter stores a UTF-16 encoded string. To distinguish the mangled names at the C ABI level, the developers simply added a single-word suffix for the function: -A for the ANSI version and -W for the Unicode version. It provides much flexibility for users to call any of them in their projects.
Generally speaking, the encoding API MultibyteToWideChar and WideCharToMultiByte are the usual way to reach the goal of interoperability for user-mode programs using different internal string representations on Windows.
It requires two calls to each function for one single conversion, the first call to calculate the buffer size and the second to perform the actual conversion. There is a simpler method to behave equivalently, that is to say, via the __bstr_t class that is supplied within the compiler’s COM support.
The COM always uses UTF-16 strings as mentioned above and the BSTR type (that is wchar_t* with some extra header) is the standard string type of COM. MSVC has native support for BSTR called __bstr_t, an encapsulation of the BSTR data type, providing a simplified version compared with the general approach.
A faster way to do encoding conversions is to instantiate an object of _bstr_t using the constructor based on the signature const char*. This overload takes an ANSI string and converts it to a UTF-16 string immediately and the _bstr_t has a const wchar_t* operator() to do the implicit cast and vice versa.
Since C++20, the new revolutionary standard has introduced a meta class named std::source_location which provides information about the current line number, file name and function name of the executing context. This new class is included in a standalone header <source_location> and also a better alternative to the old methods like __LINE__ and __FILE__.
std::source_location shows some magic mechanism to make an access to the code context without any macro expansions, as if it ‘knows’ the compile-time names and numbers where it is placed, as follows:
The output of the function name is implementation-defined, due to the different name mangling rules among GCC, Clang and MSVC. Both GCC and Clang respect the Itanium ABI, and however Microsoft maintains its unique and uniform ABI on Windows known as the MSVC ABI or Windows COM ABI.
File: /app/example.cpp, Position: (23,13), Function: int main()File: /app/example.cpp, Position: (25,10), Function: int main()
GCC is licensed under the GNU General Public License (GPL), specifically the GPLv3 with a special exception. Microsoft open-sourced its STL implementation on GitHub, with an Apache-2.0-with-LLVM-exception license. The latest GCC codebase defines a new built-in function named __builtin_source_location to generate information of the call site directly. In addition, MSVC’s STL continues to use existing built-in functions such as __builtin_LINE, __builtin_COLUMN, __builtin_FILE, __builtin_FUNCTION/__builtin_FUNCSIG that has been introduced in older versions.
When using an early version of the compiler that does not contain <source_location>, writing a custom source_location class with similar functions whose values refer to the evaluation results of these built-in functions is an acceptable consideration. Literally, GCC does provide the same functions as MSVC in GCC 10 without full support of the C++20 Standard.