[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Lua 5.1, alien and C struct with string
- From: Markus Korber <korbse@...>
- Date: Wed, 15 Apr 2015 18:40:42 +0200
Hi,
I'm trying to call a C library function in Lua5.1 via alien 0.7.0. The C
struct takes a pointer to a string buffer. However, I do not know how to
handle/allocate this buffer. When trying the solution below I get a
segmentation fault. If I try to change the type of "d" to "pointer" then
I can assign the buffer to "c.d". However, the shared lib does not print
anything. Any ideas? Thanks in advance!
Wrap of shared lib (see below) in Lua:
require "alien"
def=alien.load("libmkoshared.so")
test = def.test
test:types{ ret = "int", "pointer" }
testit = alien.defstruct {
{ "a", "int" },
{ "d", "string" }}
-- Create struct instance
c = testit:new()
c.a = 3
-- Try to alloc buffer for "d"
buf = alien.buffer("12345")
c.d = buf
-- print(alien.tostring(c.d))
-- Application call
r = test(c())
print(r)
Shared library:
Compile with: gcc -c -Wall -Werror -fPIC mkoshared.c && gcc -shared -o
libmkoshared.so mkoshared.o
#include <stdio.h>
#include <string.h>
#define STRING_LENGTH 256
typedef char TString[STRING_LENGTH];
typedef struct TUSB
{
int a;
TString d;
} TUSB;
typedef TUSB * PUSB;
extern int test(PUSB p)
{
printf("-%s-\n", (char*)p);
return 2*(p->a);
}