Using Nim to generate a DLL for use in C#/VB6

Viewed 350

After many hours of trying to figure out this problem I'm wondering if there even is a way of doing it.

I need to be able to export functions that are called from VB6 (yes you read that correctly, not VB.NET) and I'm at a loss. I got as far as generating a DLL that should have worked; it worked when being called from C at least, but nothing further.

Maybe some Nim vet can steer me in the right direction. I would be immensely greatful.

1 Answers

I'm definitely not a Nim vet, but here is something that works.
Calling Nim proc from VB6 application.

Nim DLL (compile with nim c --cpu:i386 --app:lib --nomain):

{.passc:"-m32"}
{.passl:"-m32"}
import winim,os

proc TestFunc(a: cint):cint {.exportc, stdcall, dynlib.} =
    echo "function called!"
    a + 5

when defined(vcc):
    {.emit: "N_LIB_EXPORT N_CDECL(void, NimMain)(void);".}
else:
    proc NimMain() {.cdecl, importc.}

proc DllMain(hModule: HINSTANCE, reasonForCall: DWORD, lpReserved: LPVOID): WINBOOL {.exportc, dynlib, stdcall.} =
    case reasonForCall:
    of DLL_PROCESS_ATTACH:
        when defined(vcc):
            {.emit: "NimMain();".}
        else:
            NimMain()
        AllocConsole()
        discard stdout.reopen("CONOUT$", fmWrite)
        discard execShellCmd("chcp 65001")
        echo "Hello from Nim Code!"

    else:
        discard
    return TRUE

VB6:

Private Declare Function TestFunc Lib "nim.dll" Alias "TestFunc@4" (ByVal a As Long) As Long

Private Sub Form_Load()
    Caption = CStr(TestFunc(5))
End Sub
Related