What I learned and coded for a function that opens a special Windows folder in Python3 ctypes

I learned the code for a function that opens a special Windows folder using ctypes in Python 3 and the process of writing it.

Open font folder


import os
import ctypes
from ctypes import oledll, wintypes


def getshellfolderpath(csidl: int) -> str:
    if not isinstance(csidl, int):
        raise TypeError
    SHGetFolderPathW = oledll.shell32.SHGetFolderPathW
    SHGetFolderPathW.argtypes = [
        wintypes.HWND, ctypes.c_int, wintypes.HANDLE,
        wintypes.DWORD, wintypes.LPWSTR]
    path = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
    SHGetFolderPathW(0, csidl, 0, 0, path)
    return path.value


CSIDL_FONTS = 0x0014
os.startfile(getshellfolderpath(CSIDL_FONTS))

--In Python 3, you can use the standard library ctypes to work with the Win32 API. --General types of Win32 API (HWND, DWORD, etc.) are defined in ctypes.wintypes. --ctypes exports cdll, pydll, windll, ʻoledllfor loading the dynamically linked library.cdll and pydllare the cdecl calling convention, andwindll and ctypes.oledllare the stdcall calling convention.ctypes.oledll assumes a return value of HRESULT and throws ʻOSError on failure. Official documentation. --Create a WCHAR type buffer with the ctypes.create_unicode_buffer function. You can get the string with the return value value. Official documentation. --You can use the ʻos.startfile` function to open files / folders by association.

Recommended Posts

What I learned and coded for a function that opens a special Windows folder in Python3 ctypes
What I learned in Python
What I learned by writing a Python Pull Request for the first time in my life
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
A memo that I wrote a quicksort in Python
I wrote a class in Python3 and Java
[Beginner] What happens if I write a program that runs in php in Python?
I made a familiar function that can be used in statistics with Python
What I thought and learned to study for 100 days at a programming school
I tried to implement what seems to be a Windows snipping tool in Python
I made a VM that runs OpenCV for Python
What does the last () in a function mean in Python?
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
I also tried to imitate the function monad and State monad with a generator in Python
A function that divides iterable into N pieces in Python
What I learned about AI and machine learning using Python (4)
I tried "a program that removes duplicate statements in Python"
To return char * in a callback function using ctypes in Python
Create code that outputs "A and pretending B" in python
I created a class in Python and tried duck typing
Create a function in Python
Create a Python environment for professionals in VS Code on Windows
[Python3] I made a decorator that declares undefined functions and methods.
I wrote a function to load a Git extension script in Python
A function that measures the processing time of a method in python
Create a CGH for branching a laser in Python (laser and SLM required)
[Python] What is a zip function?
Hit a command in Python (Windows)
I learned about processes in Python
Function synthesis and application in Python
I created a template for a Python project that can be used universally
Introducing a library that was not included in pip on Python / Windows
I made a web application in Python that converts Markdown to HTML
I made a Discord bot in Python that translates when it reacts
I tried to develop a Formatter that outputs Python logs in JSON
[Python] I forcibly wrote a short Perlin noise generation function in Numpy.
The eval () function that calculates a string as an expression in python
Quickly build a python environment for deep learning and data science (Windows)
A python script for Mac that zips without garbled characters on Windows
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
[Python] A game that uses regular expressions when, where, who, and what
Build a lightweight server in Python and listen for Scratch 2 HTTP extensions
[Python] A function that aligns the width by inserting a space in text that has both full-width and half-width characters.
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
I made a payroll program in Python!
Precautions when pickling a function in python
[Python] [Windows] Take a screen capture in Python
Get a token for conoha in python
What I did with a Python array
I searched for prime numbers in python
I created a password tool in Python.
I wrote a graph like R glmnet in Python for sparse modeling in Lasso
Put Docker in Windows Home and run a simple web server with Python
What I was addicted to when creating a web application in a windows environment
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
I tried to explain what a Python generator is for as easily as possible.
What I learned by implementing how to create a Default Box for SSD
Note that I understand the least squares algorithm. And I wrote it in Python.
[Python] I made a LINE Bot that detects faces and performs mosaic processing.