0

I'm trying to send an email to someone using python and Nim. I want for the script to work with a DLL, so I compiled my Nim code into a DLL. When loading the DLL into python and calling a function from it, it displays this error:

out of memory

Process finished with exit code 1

The function call and DLL loading:

import ctypes
    
email_ = ctypes.CDLL("email_.dll", winmode=0)
message = email_.message
message.argtypes = [ctypes.c_wchar_p]
message.restype = None

message(
    'Subj',
    'msg',
    'fromexample@gmail.com',
    'frompassword',
    'toexample@gmail.com'
)

And Nim DLL source code:

import smtp
import strutils

proc message*(
  subject: string,
  body: string,
  mfrom: string,
  password: string,
  mto: string
  ) {.cdecl, exportc, dynlib.} =
    var msg = smtp.createMessage(
        subject,
        body, 
        @[mto]
    )

    let smtpConn = newSmtp(useSsl=true, debug=true)
    smtpConn.connect("smtp.gmail.com", Port(465))
    smtpConn.auth(mfrom.split('@')[0], password)
    smtpConn.sendMail(mfrom, @[mto], $msg)
What is the cause of this error and how can I fix it?
nonimportant
  • 406
  • 4
  • 15

1 Answers1

1

The Nim documentation on interfacing with different languages mentions that before calling Nim code one has to call NimMain(). Presumably this could be necessary for Nim's garbage collection code to work, and maybe the out of error you are getting is nim code trying to reserve memory but the garbage collector not being ready yet and refusing to cooperate.

There could be other issues with such raw ctypes invokation, so I would recommend looking at Nim's package directory regarding python libraries. For instance, nimpy seems to be able to easily export Nim code to Python, and hopefully provides all the necessary plumbing that you may otherwise lack with ctypes.

Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78