Skip to content

Instantly share code, notes, and snippets.

@audibleblink
Forked from NaniteFactory/dllmain.go
Created July 31, 2021 00:30
Show Gist options
  • Save audibleblink/f07a98d43d28a577036c93b21e73d2e7 to your computer and use it in GitHub Desktop.
Save audibleblink/f07a98d43d28a577036c93b21e73d2e7 to your computer and use it in GitHub Desktop.

Revisions

  1. NaniteFactory revised this gist Sep 25, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions dllmain.h
    Original file line number Diff line number Diff line change
    @@ -26,9 +26,9 @@ BOOL WINAPI DllMain(
    // Return FALSE to fail DLL load.
    {
    MyThreadParams* lpThrdParam = (MyThreadParams*)malloc(sizeof(MyThreadParams));
    dwThrdParam->hinstDLL = _hinstDLL;
    dwThrdParam->fdwReason = _fdwReason;
    dwThrdParam->lpReserved = _lpReserved;
    lpThrdParam->hinstDLL = _hinstDLL;
    lpThrdParam->fdwReason = _fdwReason;
    lpThrdParam->lpReserved = _lpReserved;
    HANDLE hThread = CreateThread(NULL, 0, MyThreadFunction, lpThrdParam, 0, NULL);
    // CreateThread() because otherwise DllMain() is highly likely to deadlock.
    }
  2. NaniteFactory revised this gist Sep 25, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions dllmain.h
    Original file line number Diff line number Diff line change
    @@ -25,11 +25,11 @@ BOOL WINAPI DllMain(
    // Initialize once for each new process.
    // Return FALSE to fail DLL load.
    {
    MyThreadParams* dwThrdParam = (MyThreadParams*)malloc(sizeof(MyThreadParams));
    MyThreadParams* lpThrdParam = (MyThreadParams*)malloc(sizeof(MyThreadParams));
    dwThrdParam->hinstDLL = _hinstDLL;
    dwThrdParam->fdwReason = _fdwReason;
    dwThrdParam->lpReserved = _lpReserved;
    HANDLE hThread = CreateThread(NULL, 0, MyThreadFunction, &dwThrdParam, 0, NULL);
    HANDLE hThread = CreateThread(NULL, 0, MyThreadFunction, lpThrdParam, 0, NULL);
    // CreateThread() because otherwise DllMain() is highly likely to deadlock.
    }
    break;
  3. NaniteFactory created this gist Sep 25, 2018.
    4 changes: 4 additions & 0 deletions dllmain.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    package main

    //#include "dllmain.h"
    import "C"
    47 changes: 47 additions & 0 deletions dllmain.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #include <windows.h>

    void OnProcessAttach(HINSTANCE, DWORD, LPVOID);

    typedef struct {
    HINSTANCE hinstDLL; // handle to DLL module
    DWORD fdwReason; // reason for calling function // reserved
    LPVOID lpReserved; // reserved
    } MyThreadParams;

    DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
    MyThreadParams params = *((MyThreadParams*)lpParam);
    OnProcessAttach(params.hinstDLL, params.fdwReason, params.lpReserved);
    free(lpParam);
    return 0;
    }

    BOOL WINAPI DllMain(
    HINSTANCE _hinstDLL, // handle to DLL module
    DWORD _fdwReason, // reason for calling function
    LPVOID _lpReserved) // reserved
    {
    switch (_fdwReason) {
    case DLL_PROCESS_ATTACH:
    // Initialize once for each new process.
    // Return FALSE to fail DLL load.
    {
    MyThreadParams* dwThrdParam = (MyThreadParams*)malloc(sizeof(MyThreadParams));
    dwThrdParam->hinstDLL = _hinstDLL;
    dwThrdParam->fdwReason = _fdwReason;
    dwThrdParam->lpReserved = _lpReserved;
    HANDLE hThread = CreateThread(NULL, 0, MyThreadFunction, &dwThrdParam, 0, NULL);
    // CreateThread() because otherwise DllMain() is highly likely to deadlock.
    }
    break;
    case DLL_PROCESS_DETACH:
    // Perform any necessary cleanup.
    break;
    case DLL_THREAD_DETACH:
    // Do thread-specific cleanup.
    break;
    case DLL_THREAD_ATTACH:
    // Do thread-specific initialization.
    break;
    }
    return TRUE; // Successful.
    }
    28 changes: 28 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    package main

    import "C"

    import (
    "unsafe"

    "github.com/nanitefactory/winmb"
    )

    //export Test
    func Test() {
    winmb.MessageBoxPlain("export Test", "export Test")
    }

    // OnProcessAttach is an async callback (hook).
    //export OnProcessAttach
    func OnProcessAttach(
    hinstDLL unsafe.Pointer, // handle to DLL module
    fdwReason uint32, // reason for calling function
    lpReserved unsafe.Pointer, // reserved
    ) {
    winmb.MessageBoxPlain("DLL_PROCESS_ATTACH", "DLL_PROCESS_ATTACH")
    }

    func main() {
    // nothing really. xD
    }