MCode Tutorial (Compiled Code in AHK)
Posted: 20 Dec 2023, 10:52
With great appreciation of the original MCode Tutorial by nnnik, from which has been rewritten and extended.
Please visit the Wiki for the full tutorial.
Please visit the Wiki for the full tutorial.
The wiki follows this up with a proper introduction to using MCode in AutoHotkey v2.0, with interactive examples where you can adjust the C code being compiled, test different ways to call the C code, and even output code that can be pasted directly into your script so you can use MCode without having a compiler installed.AutoHotkey Wiki wrote:Machine code is the lowest level of binary code that your computer can run on. Programming languages like C, C++, Rust, and Go all compile to machine code in order for your computer to understand them. Machine code can run several hundred times faster than equivalent AutoHotkey code, which does not compile to machine code.
In the AutoHotkey community the term "MCode" refers to tools and methods used for putting machine code into scripts. These MCode tools normally take code written in a language like C, use a compiler to turn that code into machine code, then turns that machine code either directly into AutoHotkey code or into text that can be loaded using a custom AutoHotkey library and then called by DllCall.
MCode is important for optimization when writing scripts that need to process a relatively large amount of data quickly. Here are some common situations where MCode can be helpful:
MCode is not the only way to achieve these performance goals. It is possible, and sometimes more flexible, to use the normal tooling of those compiled languages to produce a standard machine code DLL that can be used from AutoHotkey. However, a script that comes with a custom DLL is harder to share because it takes multiple files, it can take up more disk space than a normal script, and is more likely to be blocked by antivirus and corporate application filters.
- Encoding or decoding data more than a few kilobytes in size, in formats like Json
- Hashing files or large amounts of text
- Manipulating images like GDI+ bitmaps (especially for custom ImageSearch algorithms)
- Performing real-time calculations, such as for a physics engine