Jump to content

Welcome to NulledBlog
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. If you already have an account, login here - otherwise create an account for free today!
Photo

Writing extensions in C


  • Please log in to reply
3 replies to this topic

#1
mistake010

  • Offline
  • Member

  • PipPipPip
  • Posts:
    25
    Reputation:
    4
    Joined:
    18 Jan, 2015

Warning - if you want to use it in BoL: it won't work as the package.searchers[3] and [4] are blocked.

Hi everybody!

If you ever written anything in Lua, you might have had a feeling that you wanted to add more possibilities to the language, for example functions to wait, set console color or other stuff.

You can do this using lower-level language, which is C.
I am targetting the version Lua 5.3. It should work with 5.2, you just need to download the appropriate libraries

You will need:

  • Visual Studio (I personally use 2013 and recommend you to do the same) OR other compiler (like gcc) if you don't want to target Windows
  • A copy of Lua binaries (obviously)
  • A copy of Lua static libraries (to link against)
  • Minimal knowledge about C syntax

Get your copy of libraries here: http://sourceforge.n...braries/Static/

Choose lua-5.3_Win32_vc12_lib.zip for VS 2013 (vc11 for VS 2012 etc.)

Download it and extract it somewhere.

 

1. Here we begin!

Open Visual Studio, create a new project. Name the project as you want. Set everything as it was done here:

18AnIja.png

Click OK, then Next and select everything like it was done on the screenshot below:

Q59KtqW.png

Click Finish.

On the Solution Explorer, right-click the Source Files filter and choose Add -> New Item.

Select C++ File (.cpp), however name with the extension .c, otherwise it will fail during linking!

 

After doing this, on the Solution Explorer, right-click your project and select Properties.

Change the following things:

qHynSK3.png

 

BZAzpvT.png

 

G1xUbZF.png

 

wz0FGPc.png

 

L7a7O46.png

 

In the place of D:\LuaExts select your path where you extracted your static libraries.

Click OK.

Paste the following source code to check if your settings are working correctly:

#include <Windows.h>
#include "lauxlib.h"

int __declspec(dllexport) InitLibrary(lua_State* L)
{
    lua_error(L);
    return 0;
}

If your code compiles correctly, it means you have set everything as intented. If you get any errors or warnings, review your settings once more. Make sure that everything is set just like in screenshots. If all else fails, post your errors here and I'll do my best to help you with them.

I also recommend selecting Release mode:

hFGamqH.png

 

2. Coding time!

First of all, remove the line lua_error(L); - it was only there to check your linker settings.

All functions must have parameter lua_State* and must have the return type of int.

So, let's write a function that will make your Lua script sleep for 200 milliseconds.

It's very easy to do.

We will be using for that WinAPI function called Sleep.

Create your function, call it for example lua_sleep. Remember about the parameters and return type!

int lua_sleep(lua_State* L)
{
    Sleep(200);
    return 0;
}

As you can see, the function doesn't return anything.

Before we finish, let's actually add parameter that will define the number of milliseconds we want to sleep. The function we need: luaL_checknumber(lua_State* L, int index).

Indexes in Lua begin from 1, not from 0 like in C!

luaL_checknumber returns lua_Number, which is basically a typedef for double.

So, let's do it!

int lua_sleep(lua_State* L)
{
    lua_Number timeToSleep = luaL_checknumber(L, 1);
    Sleep((DWORD)timeToSleep);
    return 0;
}

luaL_checknumber automatically checks if the input is actually a number.

We are casting timeToSleep to DWORD so the compiler doesn't get angry and give us a warning about possible data loss.

The last thing that is left is to register the function, i.e. make it possible to call it from Lua code.

Let's go back to our InitLibrary function. The function we need: lua_register(lua_State* L, const char* functionName, lua_CFunction function).

The first argument is obviously the Lua state. The second one is the name of function that will be called from Lua. The third name is our C function.

Now you should be able to register your function:

int __declspec(dllexport) InitLibrary(lua_State* L)
{
    lua_register(L, "sleep", lua_sleep);
    return 0;
}

Compile your code. Go to your project directory, enter the Release directory and copy your .dll file into directory with Lua binaries.

Now, create a new Lua script and type the following code:

package.loadlib("nameofyourlib.dll", "InitLibrary")()

print("printing something")
sleep(50)
print("after 50 ms")
sleep(500)
print("after 500 ms")

If you've done everything correctly, it will wait 50 and 500 ms before printing.

 

Final code:

#include <Windows.h>
#include "lauxlib.h"

int lua_sleep(lua_State* L)
{
    lua_Number timeToSleep = luaL_checknumber(L, 1);
    Sleep((DWORD)timeToSleep);
    return 0;
}

int __declspec(dllexport) InitLibrary(lua_State* L)
{
    lua_register(L, "sleep", lua_sleep);
    return 0;
}

3. Homework!

Write a function that changes the text color in console.

You can achieve this by calling SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), num).

The num variable must be from 0 to 15 and represents these values: http://rosettacode.o...olored_text.png

Name the functions as you like.

 

Solution for homework: http://pastebin.com/BEaTq3DX

 

Have fun!


  • 1

#2
lolbox88

  • Offline
  • Member

  • Posts:
    27
    Reputation:
    -2
    Joined:
    25 Nov, 2015

thanks man its soo nice from you


  • 0

#3
zelazny

  • Offline
  • Member

  • PipPipPip
  • Posts:
    28
    Reputation:
    -50
    Joined:
    13 Mar, 2016

thanks :)


  • 0

#4
miszauke

  • Offline
  • Member

  • PipPipPip
  • Posts:
    25
    Reputation:
    -50
    Joined:
    13 Mar, 2016

Thanks!


  • 0


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users