Python won't load C-language DLL in Windows 11 (that loads in Windows 10)

Question:

I’m running Python 3.8.5 on Windows 10 Pro, Version 10.0.19045 Build 19045. I have an application where I have moved part of the processing to a DLL, written in C using Microsoft Visual Studio C++ 2019. I’m loading my library DLL and calling functions within it with no problems.

My colleagues have now started testing, and report the DLL is ‘unable to load’ under Windows 11. Any ideas why?

I have been able to duplicate the bug with the following minimal application:

test.py

import ctypes
import _ctypes

dll = ctypes.WinDLL("./test_dll.dll")
dll.RunTest.restype = ctypes.c_int
dll.RunTest.argtypes = [ctypes.c_int]
print(dll.RunTest(0))
_ctypes.FreeLibrary(dll._handle)

test.h:

#ifndef TEST_H
#define TEST_H

#if defined(_MSC_VER) && _MSC_VER>=1020
#pragma once
#endif

#include <stdint.h>
#include <windows.h>


#if !defined(MYDLL)
# if defined(TESTDLL_EXPORTS)
#  define MYDLL extern "C" __declspec(dllexport)
# else
#  define MYDLL extern "C" __declspec(dllimport)
# endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

    MYDLL int WINAPI RunTest(int seed);

#ifdef __cplusplus
}
#endif

#endif

test.cpp:

#include "pch.h"
#include "test.h"

MYDLL int WINAPI RunTest(int seed) {
    return 42;
}

This prints 42 on Windows 10 and is ‘unable to load’ on Windows 11

Thanks in advance!

Asked By: Max

||

Answers:

The problem is solved! It turned out not to be anything to do with the Windows version, rather it was the use of a Debug build on a machine that didn’t have Visual Studio installed. Aided and abetted by Python’s less-than-completely-informative error message. As soon as I tried calling the test DLL from a C++ program, that told me exactly which library was missing, it became obvious.

Answered By: Max
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.