Hack Forums
[Python] How to use Windows DLLs in Python (ctypes example) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] How to use Windows DLLs in Python (ctypes example) (/showthread.php?tid=6267935)



How to use Windows DLLs in Python (ctypes example) - Hackers - Apr 7, 2024

Hi all, this is a super basic example that shows how a Python script can use a Windows module / DLL and call a function.
We'll use user32.dll and create a message box to say hello!




First, make sure to import the package:
Code
import ctypes

Next, set a variable for the dll you want to load:
Code
user32 = ctypes.WinDLL('user32')

Now, use a function that's available from that module:
Code
MessageBox = user32.MessageBoxW

Finally, use the function just like you normally would:
Code
MessageBox(None, "Hello, you!", "nodes Python DLL example", 0)

It really is that simple...
Spoiler (Click to View)




That's it, thank you for coming to my TED talk. I hope this was helpful to you - I personally didn't know I could do this w/ Python.


RE: How to use Windows DLLs in Python (ctypes example) - PulseLink - Apr 9, 2024

Now do one that loads dll from the project resources and executes it. Its an issue ive been running into recently and i dont want to drop the file onto the disk.

It uses loadlibrary then makes the dll calls. This is c++, I know you do both, but this is a great and simple way to get it done in python


RE: How to use Windows DLLs in Python (ctypes example) - Hackers - Apr 9, 2024

(Apr 9, 2024 04:00 PM)PulseLink Wrote:  Now do one that loads dll from the project resources and executes it. Its an issue ive been running into recently and i dont want to drop the file onto the disk.

It uses loadlibrary then makes the dll calls. This is c++, I know you do both, but this is a great and simple way to get it done in python

It's interesting you say that. I actually just did that using my own custom dll from C++ for 32 & 64 to use in Python - and it uses pyinstaller for static builds :)
I am just starting to write my own dlls more often, but the ability to bring my code to any language essentially is very exciting to me. I can basically package complicated logic into functions that others can use... I knew this but I'm finally able to really start doing it and I already know a good few other languages so I think I'll try to do some releases like this...

[Image: Screenshot-2024-04-09-214351.png]

[Image: Screenshot-2024-04-09-214649.png]

GPT definitely helped me knock out the proper data format for the b' python binary format & types and buffering in the Python - I had to fix a couple lines but it works easily because all the actual real logic is in the DLL file! This simplifies the actual usage of it. I understand now how I can port Windows functions to any lang, or even let other languages manage peb/teb/ntdll/whatever to have a bridge to the higher level language that is easy to use. Still working on things for it, will release some useful DLLs to community soon, with examples in Python & probably also PHP too but might use Linux versions for php to be more in line w/ server arch.


RE: How to use Windows DLLs in Python (ctypes example) - PulseLink - Apr 10, 2024

(Apr 9, 2024 06:49 PM)nodes Wrote:  It's interesting you say that. I actually just did that using my own custom dll from C++ for 32 & 64 to use in Python - and it uses pyinstaller for static builds :)
I am just starting to write my own dlls more often, but the ability to bring my code to any language essentially is very exciting to me. I can basically package complicated logic into functions that others can use... I knew this but I'm finally able to really start doing it and I already know a good few other languages so I think I'll try to do some releases like this...

[Image: Screenshot-2024-04-09-214351.png]

[Image: Screenshot-2024-04-09-214649.png]

GPT definitely helped me knock out the proper data format for the b' python binary format & types and buffering in the Python - I had to fix a couple lines but it works easily because all the actual real logic is in the DLL file! This simplifies the actual usage of it. I understand now how I can port Windows functions to any lang, or even let other languages manage peb/teb/ntdll/whatever to have a bridge to the higher level language that is easy to use. Still working on things for it, will release some useful DLLs to community soon, with examples in Python & probably also PHP too but might use Linux versions for php to be more in line w/ server arch.

look at my most recent post about dll sideloading etc, and PM me pls, we've talked on these threads id like to share some code with you:)


RE: How to use Windows DLLs in Python (ctypes example) - Hackers - Apr 10, 2024

(Apr 10, 2024 03:34 PM)PulseLink Wrote:  look at my most recent post about dll sideloading etc, and PM me pls, we've talked on these threads id like to share some code with you:)

Nice, that looks good. I'm excited to see what else you put out, keep it up