Function pointers
See original GitHub issueI am trying to pass function pointers as struct members to a c api using ffi. Here is the function definitions and the struct in c
struct Session {
PWLOG *pwLog;
PWACCEPT *pwAccept;
PWSEND *pwSend;
};
void * create_session(struct Session *session);
typedef int PWLOG(const char *type, const char *msg);
typedef const char * PWACCEPT(const char *keys, const char *replyStr, size_t replyStrSize);
typedef void PWSEND(const char *type, const char *msg);
Here is the struct defined in javascript using ref-struct with ffi.
const pwLogFunc = function(type, message) {
console.log(type.readCString(), message.readCString());
};
const pwSendFunc = function (type, msg) {
console.log(type.readCString(), msg.readCString());
};
const pwAcceptFunc= function (keys, replyStr, replyStrSize) {
console.log(keys.readCString(), replyStr.readCString(), replyStrSize);
}
const Session = Struct({
pwLog: pwLogFunc,
pwSend: pwSendFunc,
pwAccept: pwAcceptFunc
});
const structPtr = Ref.refType(Session);
const lib = Ffi.Library(dllPath, {
'create_session': ['void *', [structPtr]]
});
const session = new Session({
pwLog: pwLogFunc,
pwAccept: pwAcceptFunc,
pwSend: pwSendFunc
});
const result = lib.create_session(session.ref());
Any help would be greatly appreciated as my knowledge of c is very limited!
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Function Pointer in C - GeeksforGeeks
1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.
Read more >Function Pointers in C and C++ - Cprogramming.com
A function pointer is a variable that stores the address of a function that can later be called through that function pointer.
Read more >Function pointer - Wikipedia
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing...
Read more >How do function pointers in C work? - Stack Overflow
A function pointer is a variable that contains the address of a function. Since it is a pointer variable though with ...
Read more >Pointers to functions - IBM
A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Perhaps the topic is already outdated, but here’s a example, how to call a function pointer inside a c-struct:
data.h
:data.c
:wrapper.c
, here we can allocate/free memory for the structs:after
gcc -shared -fpic data.c wrapper.c -o data.so
we can use it indata.js
:result:
I hope this example will be useful.
If your
pwCreate()
function really does return theSession *
, then you probably want to iteratively define the struct fields, so that you can access Session: