C# Implementing DllMain with DllExport

2019-07-30 01:42发布

问题:

Im Using UnmanagedExports By RobertGiesecke

I want export DllMain

Here what i try

    [DllExport("DllMain", CallingConvention.StdCall)]
    public static bool DllMain(IntPtr hModule, uint dwReason, byte[] lpReserved)
    { // I Write a text to file here
        return true; 
    }

Now i call LoadLibrary but nothing happened.

回答1:

Hooray, I found a way by using static constructor.

Just make class that contains exports static, and add static method.

public static class Class1
{
    static Class1()
    {
        Console.WriteLine("DLL MAIN (Only DLL_PROCESS_ATTACH) :D");
    }

    [DllExport("AddFunc", CallingConvention.Cdecl)]
    public static int AddFunc(int a, int b)
    {
        return a + b + 1;
    }
}

When AddFunc called, Program first call Class1(Only one time) next call AddFunc

Anyway for DLL_PROCESS_DETACH ?