Hi,

I am trying to pass a Struct from Go to C (this is cgo).I m passing a 
callback function in that struct that will be called from C code to Go.


main.go
=======

type Callback struct {
Func     func(*C.char , *C.char , C.size_t, C.int)
UserData int32
}

Calling C code in the following manner
=========================================

C.initialize_engine(pattern_db, module_path, 
(C.Foo)(unsafe.Pointer(&Callback{
Func:     C.callOnMeGo_cgo,
UserData: C.int(1),
})))

//export ParsedData
func ParsedData(key *C.char, value *C.char, value_len C.size_t, work C.int) 
{
fmt.Println("WORK:")
fmt.Print()
//performing some operation here
}

cfunc.go
=========

package main

/*

#include <stdio.h>

// The gateway function
void callOnMeGo_cgo(char *key, char *value, size_t value_len, int work)
{
//printf("C.callOnMeGo_cgo(): called");
void ParsedData(const char *key, const char *value, size_t value_len, int 
work);
ParsedData(key, value, value_len, work);
}
*/
import "C"



syslog-node.h
===============
#ifndef _TEST_H_
#define _TEST_H_

#include <stdlib.h>

typedef void (*key_value_cb)(const char* key, const char* value, size_t 
value_len, int work);
typedef struct Foo{
    key_value_cb cbnitish;
    int data;
}Foo;
int initialize_engine(const char* filename, const char* module_path, Foo 
*cb);
int reload_pattern_db(const char* filename, Foo *cb);

#endif

syslog-node.c
===============

gboolean pdbtool_accumulate_fields(NVHandle handle, const gchar *name, 
const gchar *value, gssize length, gpointer user_data)
{
  user_data->cbnitish(name, value, length, user_data->data);// Callback 
happening here
  return FALSE;
}

Eventually user_data will get the instance of the struct.


Getting following error during make command:
==============================================

nsaboo@ubuntu:~/Documents/goworkspace/src/poc$ make
gcc -L/usr/local/lib -lsyslog-ng -o syslog-node.so 
-L/usr/local/lib/syslog-ng -ldbparser -c `pkg-config --libs --cflags 
glib-2.0` -I/usr/local/include/syslog-ng/ -I./syslog-ng-3.6.2/ 
-I/usr/local/include/eventlog/ syslog-node.c
syslog-node.c: In function ‘pdbtool_accumulate_fields’:
syslog-node.c:33:12: warning: dereferencing ‘void *’ pointer
   user_data->cbnitish(name, value, length, user_data->data);
            ^~
syslog-node.c:33:12: error: request for member ‘cbnitish’ in something not 
a structure or union
syslog-node.c:33:53: warning: dereferencing ‘void *’ pointer
   user_data->cbnitish(name, value, length, user_data->data);
                                                     ^~
syslog-node.c:33:53: error: request for member ‘data’ in something not a 
structure or union
makefile:12: recipe for target 'syslog-node.so' failed
make: *** [syslog-node.so] Error 1

Can someone please guide me here ?

Thanks,
Nitish

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c8b2f46a-78e2-4306-8e96-6c30bb7529f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to