Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ProjectEli/8d5dd08f67080cafb7dc5cb29f5eefb3 to your computer and use it in GitHub Desktop.
Save ProjectEli/8d5dd08f67080cafb7dc5cb29f5eefb3 to your computer and use it in GitHub Desktop.
Make C# dll with cdecl support for autohotkey

Why this gist needs?

Autohotkey library supports DllCall() and it can call dll with standard decl feature of c dll. However, typical c# library runs on IL and cannot be called from autohotkey. To support standard dll call, DllExport package is required in Visual Studio.

Step by step

1. Create C# .Net framework library project in Visual studio

image

2. Set projectName

The name will be automatically set to the namespace. image

3. Open Nuget Package manager by right click on your project name

image

4. Install DllExport package

image

5. Select install checkbox and click apply button of DllExport package configuration window

make sure that namespace is the same as your project's namespace image

6. Click Reload all in VS

image

6. Write some function in your main file, and add [DllExport] on top of the function definition

Custom function should be statc, to be exported using DllExport, or it emits error during build process

// class1.cs
namespace myDll
{
    public class Class1 // this class name doesn't matter
    {
        [DllExport]
        public static double mySum(double x, double y)
        {
            return x + y;
        }
    }
}

7. Change build profile to release and build it

image

8. Get dll file in the release folder

You can check path of resulting dll file at console output.

image

If you are in x64 machine, get dll file in the x64 folder!! ex:

C:\Users\Eli\Downloads\EliDll\myDll\myDll\bin\Release\x64\myDll.dll

image

9. Move dll file to your autohotkey folder and make ahk file that calls dll file

; main.ahk
; This file calls mysum function in myDll.dll
; and put double 5 and double 4 as input args and get output with double type.
; Be careful of case-sensitive function name and arg type
; See more info about syntax at https://www.autohotkey.com/docs/commands/DllCall.htm
result:=DllCall("myDll.dll\mySum", "Double", 5, "Double", 4,"Double")
MsgBox, %result%

image

10. Excute ahk file and see result

image

References

@valuex
Copy link

valuex commented Feb 12, 2024

Thanks for sharing this step-by-step instruction.
As for the ahk script, there is a typo. The function name should be mySum, with letter S capitalized. :-D
result:=DllCall("myDll.dll\mySum", "Double", 5, "Double", 4,"Double")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment