The RSA300API.DLL might have dependencies in the folder so prior to using it, use os.chdir to set the working directory, for example:

import os

os.chdir(r'C:\Tektronix\RSA306 API\lib\x64')
rsa300 = ctypes.WinDLL(r"C:\Tektronix\RSA306 API\lib\x64\RSA300API.dll")

Checking one of their samples, this appears to be the recommended way to access it.

Alternatively, as @eryksub has mentioned, LoadLibraryEx can be used. win32api could be used to get the handle and pass it to WinDLL as follows:

import ctypes
import win32api
import win32con

dll_name = r'C:\Tektronix\RSA306 API\lib\x64\RSA300API.dll'
dll_handle = win32api.LoadLibraryEx(dll_name, 0, win32con.LOAD_WITH_ALTERED_SEARCH_PATH)
rsa300 = ctypes.WinDLL(dll_name, handle=dll_handle)
Answer from Martin Evans on Stack Overflow
🌐
Python
docs.python.org › 3 › library › ctypes.html
ctypes — A foreign function library for Python
ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries.
🌐
Lonami
lonami.dev › blog › ctypes-and-windows
Python ctypes and Windows | Lonami's Blog
June 19, 2019 - While the post focuses on Windows' API, the code here probably applies to unix-based systems with little modifications. First of all, let's learn how to load a library. Let's say we want to load User32.dll: ... Yes, it's that simple. When you access an attribute of windll, said library will load.
Discussions

python - Issues using ctypes.WinDLL() - Stack Overflow
Alternatively, as @eryksub has mentioned, LoadLibraryEx can be used. win32api could be used to get the handle and pass it to WinDLL as follows: Copyimport ctypes import win32api import win32con dll_name = r'C:\Tektronix\RSA306 API\lib\x64\RSA300API.dll' dll_handle = win32api.LoadLibraryEx(dll_name, ... More on stackoverflow.com
🌐 stackoverflow.com
python - Ctypes, calling an external windll function - Stack Overflow
I want to call some functions from an external dll using Python3 on Windows. The library and the functions I want to use are as below; MECAB_DLL_EXTERN mecab_t* mecab_new2(const char *arg); More on stackoverflow.com
🌐 stackoverflow.com
Python import dll - Stack Overflow
How would I import a winDLL into python and be able to use all of its functions? It only needs doubles and strings. ... You've tagged the question ctypes and so it sounds like you already know the answer. More on stackoverflow.com
🌐 stackoverflow.com
python - is there any ctypes.windll documentation? - Stack Overflow
Is there a webpage where all functions and classes in ctypes is documented? I would like to read about everything under it. for example ctypes.windll, is there anything like this: http://nullege.com/ More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › working with ctypes.windll is so annoying.
r/learnpython on Reddit: Working with ctypes.windll is so annoying.
June 30, 2024 -

Hello! I recently started on a function that creates some specified amount of text windows and spreads them around the screen.

The problem is that ctypes.windll.user32.MoveWindows and ctypes.windll.user32.SetWindowPos both not work for some reason.

I also found that the creation of the message box blocks the normal flow of the code, so then I put that creation of the box in a thread, but then you cannot specify the hwnd (handle for the window) for the move functions. I tried just putting it normally, but then it blocks the flow and does not let the move function do their thing.

It currently just stacks the windows diagonally like the old windows glitch, which is also cool, but not the wanted effect.

import time
import ctypes
import random
import threading

def message_box(title, message, count):
    try:
        values = 0 | 0x10 | 0x40000
        def show_box():
            screen_width = ctypes.windll.user32.GetSystemMetrics(0)
            screen_height = ctypes.windll.user32.GetSystemMetrics(1)

            x = random.randint(0, (screen_width - 200))
            y = random.randint(0, (screen_height - 100))
            print(f"X: {x}, Y: {y}")

            hwnd = ctypes.windll.user32.MessageBoxW(0, message, title, values)

            ctypes.windll.user32.MoveWindow(hwnd, x, y, 150, 50)
            ctypes.windll.user32.SetWindowPos(hwnd, -1, x, y, 0, 0, 0x0001)

        for i in range(count):
            threading.Thread(target=show_box).start()
            time.sleep(0.5)

    except Exception as e:
        print(f"Error displaying message box: {e}")

if __name__ == '__main__':
    message_box("Hello", "This is a test message", 5)

Here is my code so far if it is any help:

Is this actually even possible?

Having An Error With CTypes Apr 28, 2023
r/learnpython
3y ago
Using a windows sdk with python Feb 17, 2020
r/learnprogramming
6y ago
ctypes MessageBoxW not creating? Feb 4, 2025
r/learnpython
last yr.
Ctypes is driving me crazy (what is 'winmode') Oct 19, 2021
r/learnpython
4y ago
More results from reddit.com
Top answer
1 of 2
3

The RSA300API.DLL might have dependencies in the folder so prior to using it, use os.chdir to set the working directory, for example:

import os

os.chdir(r'C:\Tektronix\RSA306 API\lib\x64')
rsa300 = ctypes.WinDLL(r"C:\Tektronix\RSA306 API\lib\x64\RSA300API.dll")

Checking one of their samples, this appears to be the recommended way to access it.

Alternatively, as @eryksub has mentioned, LoadLibraryEx can be used. win32api could be used to get the handle and pass it to WinDLL as follows:

import ctypes
import win32api
import win32con

dll_name = r'C:\Tektronix\RSA306 API\lib\x64\RSA300API.dll'
dll_handle = win32api.LoadLibraryEx(dll_name, 0, win32con.LOAD_WITH_ALTERED_SEARCH_PATH)
rsa300 = ctypes.WinDLL(dll_name, handle=dll_handle)
2 of 2
3

Tektronix application engineer here.

Martin is correct, our dll does have dependencies in the folder in which it resides. It looks like you're using the old version of our API, which has a whole truckload of dependencies in the C:\Tektronix\RSA306 API\lib\x64 folder. The newer version of the API purges and consolidates many of those dependencies and has renamed many of the functions to improve clarity and consistency. Our software engineers also decided that the old API wouldn't be overwritten when installing newer versions, so your old scripts will still work even if you install the newest API.

FWIW, I use the following code at the beginning of all my RSA control scripts:

"""
################################################################
C:\Tektronix\RSA306 API\lib\x64 needs to be added to the 
PATH system environment variable
################################################################
"""
os.chdir("C:\\Tektronix\\RSA_API\\lib\\x64")
rsa = cdll.LoadLibrary("RSA_API.dll")

Here is a link to download the latest version of the RSA API (as of 11/1/16): http://www.tek.com/model/rsa306-software

Here is a link to download the API documentation (as of 11/1/16). There is an Excel spreadsheet attached to this document that outlines the differences between old functions and new functions: http://www.tek.com/spectrum-analyzer/rsa306-manual-6

🌐
BestDivision
bestdivision.com › spread your knowledge - bestdivision › questions - bestdivision › python questions - categories - bestdivision › what is the ctypes.windll module in python? - bestdivision
What is the ctypes.windll module in Python? | BestDivision
July 14, 2023 - The ctypes.windll module is a part of the ctypes library in Python specifically designed for interfacing with Windows dynamic link libraries (DLLs). It allows Python programs to call functions within DLLs, enabling seamless integration with the Windows API and enhancing the capabilities of ...
🌐
Python
svn.python.org › projects › ctypes › trunk › ctypes › docs › manual › tutorial.html
ctypes tutorial
ctypes exports the cdll, and on Windows also windll and oledll objects to load dynamic link libraries.
🌐
ProgramCreek
programcreek.com › python › example › 59464 › ctypes.WinDLL
Python Examples of ctypes.WinDLL
""" from ctypes import wintypes from ctypes import WinError ext = ".dll" dst = tempfile.mktemp(prefix=dst_prefix, suffix=ext) libs = [x.path for x in psutil.Process().memory_maps() if os.path.splitext(x.path)[1].lower() == ext and 'python' in os.path.basename(x.path).lower() and 'wow64' not in x.path.lower()] src = random.choice(libs) shutil.copyfile(src, dst) cfile = None try: cfile = ctypes.WinDLL(dst) yield dst finally: # Work around OverflowError: # - https://ci.appveyor.com/project/giampaolo/psutil/build/1207/ # job/o53330pbnri9bcw7 # - http://bugs.python.org/issue30286 # - http://stackoverflow.com/questions/23522055 if cfile is not None: FreeLibrary = ctypes.windll.kernel32.FreeLibrary FreeLibrary.argtypes = [wintypes.HMODULE] ret = FreeLibrary(cfile._handle) if ret == 0: WinError() safe_rmpath(dst)
🌐
Python
docs.python.org › 3.9 › library › ctypes.html
ctypes — A foreign function library for Python — Python 3.9.25 documentation
ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries.
Find elsewhere
🌐
Python
docs.python.org › 3.10 › library › ctypes.html
ctypes — A foreign function library for Python — Python 3.10.21 documentation
ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries.
🌐
System Weakness
systemweakness.com › how-to-use-the-win32api-with-python3-3adde999211b
How To Use The win32api with python3 | by cpu0x00 | System Weakness
May 2, 2023 - wintypes contains the vast majority of datatypes that can be used with the win32api and python-ctypes · windll is basically the windows.h file which is used call other header files like kernel32
Top answer
1 of 5
21

You've tagged the question ctypes and so it sounds like you already know the answer.

The ctypes tutorial is excellent. Once you've read and understood that you'll be able to do it easily.

For example:

>>> from ctypes import *
>>> windll.kernel32.GetModuleHandleW(0)
486539264

And an example from my own code:

lib = ctypes.WinDLL('mylibrary.dll')
#lib = ctypes.WinDLL('full/path/to/mylibrary.dll')
func = lib['myFunc']#my func is double myFunc(double);
func.restype = ctypes.c_double
value = func(ctypes.c_double(42.0))
2 of 5
3

I'm posting my experience. First of all despite all the hard work that take me to put all pieces together, importing a C# dll is easy. The way I did it is:

1) Install this nuget package (i'm not owner, is just very useful) in order to build a unmanaged dll: https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

2) Your C# dll code is like this:

using System;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

public class MyClassName
{
   [DllExport("MyFunctionName",CallingConvention = CallingConvention.Cdecl)]
   [return: MarshalAs(UnmanagedType.LPWStr)]
   public static string MyFunctionName([MarshalAs(UnmanagedType.LPWStr)] string iString)
   {
       return "hello world i'm " + iString
   }
}

3) Your python code is like this:

import ctypes
#Here you load the dll into python 
MyDllObject = ctypes.cdll.LoadLibrary("C:\\My\\Path\\To\\MyDLL.dll")
#it's important to assing the function to an object
MyFunctionObject = MyDllObject.MyFunctionName
#define the types that your C# function return
MyFunctionObject.restype = ctypes.c_wchar_p
#define the types that your C# function will use as arguments
MyFunctionObject.argtypes = [ctypes.c_wchar_p]
#That's it now you can test it
print(MyFunctionObject("Python Message"))
🌐
Yizhang82
yizhang82.dev › python-interop-ctypes
Calling C functions from Python - part 1 - using ctypes | yizhang82’s blog
January 8, 2018 - Besides CDLL, there is also windll and oledll. windll by default treat the function as stdcall, and oledll would treat it as a COM function, which means accessing the function by an vtable offset, with stdcall, and returning HRESULT. Let’s take a look at how to define your own struct.
🌐
Stack Overflow
stackoverflow.com › questions › 40850510 › is-there-any-ctypes-windll-documentation
python - is there any ctypes.windll documentation? - Stack Overflow
http://nullege.com/codes/search?cq=ctypes.windll.user32.SendInput ... Unfortunately the only good source I know of apart from the official python doc on ctypes is python.net/crew/theller/ctypes/reference.html
🌐
GitHub
github.com › python › cpython › blob › main › Lib › ctypes › __init__.py
cpython/Lib/ctypes/__init__.py at main · python/cpython
pythonapi = PyDLL(None) · · if _os.name == "nt": windll = LibraryLoader(WinDLL) oledll = LibraryLoader(OleDLL) · GetLastError = windll.kernel32.GetLastError · from _ctypes import get_last_error, set_last_error ·
Author: python
🌐
GitHub
github.com › ctypesgen › ctypesgen › issues › 74
WinDLL vs CDLL method call · Issue #74 · ctypesgen/ctypesgen
October 29, 2019 - def __init__(self, path): self.cdll = ctypes.windll.LoadLibrary(path) # ctypes.cdll.LoadLibrary(path) self.windll = ctypes.windll.LoadLibrary(path)
Author: ctypesgen
🌐
GitHub
gist.github.com › Pagliacii › 774ed5d3ea78a36cdb0754be6a25408d
Use ctypes to call WIndows API · GitHub
Use ctypes to call WIndows API. GitHub Gist: instantly share code, notes, and snippets.
🌐
Python Forum
python-forum.io › thread-5690.html
Error Ctypes in Windows
Hello everyone. I have just begun to program in Python. I have done several scripts and now I am trying to call the Windows API with Ctypes module. I have copied the example code of the web (docs.python.org\ctypes) but it doesn't run. from ctypes ...