user3375027
user3375027

Reputation: 87

Attempt to load library; get "bad image" error

I've been trying to load a library into lua file. Sparing the details, as they are not really important, I have tried this many ways.

The final way, and the one I believe to be correct although I still can't get it to work, is to use "package.loadlib". See code:

ed = package.loadlib("Encode_Decode.lua", "luaopen_ed")
print(ed)

But when I run the program I get this error:

Encode_Decode.lua is either not designed to run on Windows or it contains an error. Try installing the program again using the original installation media or contact your system administrator or the software vendor for support.

I know the program runs because I used it internally to test it's encoding and decoding abilities and it worked fine. I'd really prefer not moving the contents of the library over as my main lua file is crowded as it is. I will if I have to though.

Yes it is in the main folder. I've also tried changing the extension of the library file into a .dll, with the same error.

What am I doing wrong?

I apologize in advance if this is a duplicate, I did my best to research this problem as thoroughly as I could. But to be honest it's almost 3 AM and I've been searching for almost an hour.

Upvotes: 2

Views: 256

Answers (2)

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26569

package.loadlib is used for loading shared libraries; i.e. .dll or .so files. You're passing a .lua file to it, so Windows attempts to load it as a .dll and fails when it can't.

To load Lua source code, you can use dofile. Alternatively, you can use require, which is a bit more complex, but handles loading modules only once and works with both Lua and C modules.

Upvotes: 0

user3375027
user3375027

Reputation: 87

Stupid beginner mistake, used the wrong syntax.

require("Encode_Decode")
print(dec("bnVs")) --returns "nul"

Upvotes: 2

Related Questions