2 years ago

#24955

test-img

Dimitri Sophinos

Why does my program exit when calling Printf not working through LuaJIT FFI?

I have been trying to load a very simple DLL through the LuaJIT FFI library, but when my code runs, the program pauses for a second and then quits without any error message halfway through as soon as it tries to run printf.

My DLL's code:

#include <windows.h>
#include <stdio.h>

__declspec(dllexport) int getnumber(){
    return 4000;
}

__declspec(dllexport) void doprintf(){
    printf("hello from printf!");
}

__declspec(dllexport) const char* returnstring(const char*source){
    
    return source;

}


int main()
{
    
}

My main.lua:

print('hello from lua!') --> hello from lua!
local ffi = require("ffi")
ffi.cdef[[
  
  int getnumber();
  
  void doprintf();
  
  const char* returnstring(const char*source);
  
]]
clib = ffi.load("my-dll")

print('libraries loaded.') --> libraries loaded.
print(ffi.string(clib.returnstring('string test'))) --> string test
print(clib.getnumber() - 1)  --> 3999
print(clib.doprintf())  --> hello from printf!
print('done!!') --> done!!

What happens when I run it:

C:\Users\DPS2004\OneDrive\Documents\ffitest>ls
main.lua    my-dll.dll

C:\Users\DPS2004\OneDrive\Documents\ffitest>luajit main.lua
hello from lua!
libraries loaded.
string test
3999

C:\Users\DPS2004\OneDrive\Documents\ffitest>

c

lua

printf

ffi

luajit

0 Answers

Your Answer

Accepted video resources