Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,81 @@ jobs:
ninja


windows-cmake-no-crt:

name: windows no-CRT
runs-on: windows-latest

steps:
- uses: actions/checkout@v7
with:
path: hidapisrc
- name: Set up MSVC developer environment
uses: TheMrMilchmann/setup-msvc-dev@v4
with:
arch: x64
- name: Install Ninja
shell: cmd
run: choco install ninja -y --no-progress
- name: Build and check all no-CRT compilers
shell: pwsh
run: |
@'
#include <windows.h>
#include <hidapi.h>

void __cdecl mainCRTStartup(void)
{
if (hid_init() == 0)
hid_exit();
ExitProcess(0);
}
'@ | Set-Content -Encoding ascii no_crt_probe.c

$ok = $true
foreach ($compiler in @('cl', 'clang-cl', 'gcc')) {
Write-Host "`n===== $compiler ====="
$build = "build\no-crt-$compiler"

cmake -G Ninja -B $build -S hidapisrc `
"-DCMAKE_C_COMPILER=$compiler" -DCMAKE_BUILD_TYPE=Release `
-DBUILD_SHARED_LIBS=OFF -DHIDAPI_NO_CRT=ON `
-DHIDAPI_WITH_TESTS=OFF -DHIDAPI_BUILD_HIDTEST=OFF
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

cmake --build $build
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

$lib = Get-ChildItem "$build\src\windows" -File |
Where-Object { $_.Name -eq 'hidapi.lib' -or $_.Name -eq 'libhidapi.a' } |
Select-Object -First 1
if (-not $lib) {
Write-Host "::error title=$compiler::no-CRT static library not found"
$ok = $false
continue
}

if ($compiler -eq 'gcc') {
gcc -c -DHID_API_NO_EXPORT_DEFINE -Ihidapisrc\hidapi no_crt_probe.c -o no_crt_probe.o
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
gcc -nostdlib "-Wl,--entry=mainCRTStartup" "-Wl,--subsystem,console" `
-o no_crt_probe.exe no_crt_probe.o "$($lib.FullName)" -lkernel32 -luser32 -lshlwapi -lgcc
} else {
& $compiler /nologo /c /GS- /Gs9999999 /DHID_API_NO_EXPORT_DEFINE /Ihidapisrc\hidapi no_crt_probe.c
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
link /nologo /NODEFAULTLIB /ENTRY:mainCRTStartup /SUBSYSTEM:CONSOLE `
/OUT:no_crt_probe.exe no_crt_probe.obj "$($lib.FullName)" kernel32.lib user32.lib shlwapi.lib
}
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

$deps = dumpbin /nologo /dependents no_crt_probe.exe | Out-String
if ($deps -match '(?i)(vcruntime|ucrtbase|msvcrt|api-ms-win-crt|libgcc|libwinpthread)') {
Write-Host "::error title=$compiler::CRT dependency found in the no-CRT probe"
$ok = $false
}
}
if (-not $ok) { exit 1 }

windows-cmake-mingw:

runs-on: windows-latest
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ endif()

if(WIN32)
option(HIDAPI_BUILD_PP_DATA_DUMP "Build small Windows console application pp_data_dump.exe" ${IS_DEBUG_BUILD})
option(HIDAPI_NO_CRT "Build the Windows backend without the C runtime" OFF)
endif()

add_subdirectory(src)
Expand Down
19 changes: 19 additions & 0 deletions windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ set(SOURCES
hidapi_hidclass.h
hidapi_hidpi.h
hidapi_hidsdi.h
hidapi_winapi_crt.h
)

if(HIDAPI_NO_CRT)
list(APPEND SOURCES hidapi_winapi_crt.c)
endif()

if(BUILD_SHARED_LIBS)
list(APPEND SOURCES hidapi.rc)
endif()
Expand All @@ -32,6 +37,20 @@ target_compile_definitions(hidapi_winapi
PRIVATE HIDAPI_CMAKE_V0_BUILD
)

if(HIDAPI_NO_CRT)
target_compile_definitions(hidapi_winapi PRIVATE HIDAPI_NO_CRT)
target_link_libraries(hidapi_winapi PUBLIC shlwapi)
if(MSVC)
# The descriptor reconstructor has a large stack frame. Disable stack
# probes and the security cookie so the static library does not need
# __chkstk or __security_cookie from the CRT.
target_compile_options(hidapi_winapi PRIVATE /GS- /Gs9999999)
elseif(MINGW)
# GCC's automatic stack probe would need ___chkstk_ms from the CRT.
target_compile_options(hidapi_winapi PRIVATE -mno-stack-arg-probe)
endif()
endif()

if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(hidapi_winapi
# prevent marking functions as __declspec(dllexport) for static library build
Expand Down
16 changes: 16 additions & 0 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,17 @@ typedef LONG NTSTATUS;
#include "hidapi_hidclass.h"
#include "hidapi_hidsdi.h"

#if !defined(HIDAPI_NO_CRT)
#include <stdio.h>
#endif
#include <stdlib.h>
#include <string.h>

#if defined(HIDAPI_NO_CRT)
#include "hidapi_winapi_crt.h"
#define HIDAPI_SWPRINTF(dest, count, ...) wnsprintfW((dest), (int)(count), __VA_ARGS__)
#define HIDAPI_WCSNCPY(dest, dest_count, src) lstrcpynW((dest), (src), (int)(dest_count))
#else
/* MSVC secure CRT (VS2005+) provides swprintf_s/wcsncpy_s.
Older MSVC and GCC/MinGW/Cygwin use the classic variants. */
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
Expand All @@ -69,6 +76,7 @@ typedef LONG NTSTATUS;
#define HIDAPI_SWPRINTF swprintf
#define HIDAPI_WCSNCPY(dest, dest_count, src) wcsncpy((dest), (src), (dest_count))
#endif
#endif

#ifdef MIN
#undef MIN
Expand Down Expand Up @@ -464,7 +472,11 @@ static void* hid_internal_get_device_interface_property(const wchar_t* interface

static void hid_internal_towupper(wchar_t* string)
{
#if defined(HIDAPI_NO_CRT)
CharUpperW(string);
#else
for (wchar_t* p = string; *p; ++p) *p = towupper(*p);
#endif
}

static int hid_internal_extract_int_token_value(wchar_t* string, const wchar_t* token)
Expand All @@ -477,7 +489,11 @@ static int hid_internal_extract_int_token_value(wchar_t* string, const wchar_t*
return -1;

startptr += wcslen(token);
#if defined(HIDAPI_NO_CRT)
token_value = hid_internal_wcstol_hex(startptr, &endptr);
#else
token_value = wcstol(startptr, &endptr, 16);
#endif
if (endptr == startptr)
return -1;

Expand Down
4 changes: 4 additions & 0 deletions windows/hidapi_descriptor_reconstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
********************************************************/
#include "hidapi_descriptor_reconstruct.h"

#if defined(HIDAPI_NO_CRT)
#include "hidapi_winapi_crt.h"
#endif

/**
* @brief References to report descriptor buffer.
*
Expand Down
70 changes: 70 additions & 0 deletions windows/hidapi_winapi_crt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************
HIDAPI - Multi-Platform library for
communication with HID devices.

libusb/hidapi Team

Copyright 2026, All Rights Reserved.

At the discretion of the user of this library,
this software may be licensed under the terms of the
GNU General Public License v3, a BSD-Style license, or the
original HIDAPI license as outlined in the LICENSE.txt,
LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
files located at the root of the source distribution.
These files may also be found in the public source
code repository located at:
https://github.com/libusb/hidapi .
********************************************************/

#include <windows.h>
#include <stddef.h>

#if defined(HIDAPI_NO_CRT)

#if (defined(_M_IX86) || defined(_M_X64)) && (defined(_MSC_VER) || defined(__clang__))
#include <intrin.h>
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#pragma function(memset, memcpy, memcmp)
#endif

void *__cdecl memset(void *dest, int value, size_t count)
{
#if (defined(_M_IX86) || defined(_M_X64)) && (defined(_MSC_VER) || defined(__clang__))
__stosb((unsigned char *)dest, (unsigned char)value, count);
#else
volatile unsigned char *d = (volatile unsigned char *)dest;
while (count--)
*d++ = (unsigned char)value;
#endif
return dest;
}

void *__cdecl memcpy(void *dest, const void *src, size_t count)
{
#if (defined(_M_IX86) || defined(_M_X64)) && (defined(_MSC_VER) || defined(__clang__))
__movsb((unsigned char *)dest, (const unsigned char *)src, count);
#else
volatile unsigned char *d = (volatile unsigned char *)dest;
const unsigned char *s = (const unsigned char *)src;
while (count--)
*d++ = *s++;
#endif
return dest;
}

int __cdecl memcmp(const void *lhs, const void *rhs, size_t count)
{
const volatile unsigned char *a = (const volatile unsigned char *)lhs;
const volatile unsigned char *b = (const volatile unsigned char *)rhs;
while (count--) {
if (*a != *b)
return *a < *b ? -1 : 1;
++a;
++b;
}
return 0;
}

#endif /* HIDAPI_NO_CRT */
Loading
Loading