Wrapping c++
-
I am trying to wrap a c++ class for c. The function uses pass by reference. Example Test::funcref(int & h,BSTR & b) void wrap_funcref(wrap_t *m, int h, BSTR) { Test *obj; if (m == NULL) return; obj = static_cast(m->obj); obj->funcref(&h,&b); } Not sure if I need to change the c++ functions for *
-
I am trying to wrap a c++ class for c. The function uses pass by reference. Example Test::funcref(int & h,BSTR & b) void wrap_funcref(wrap_t *m, int h, BSTR) { Test *obj; if (m == NULL) return; obj = static_cast(m->obj); obj->funcref(&h,&b); } Not sure if I need to change the c++ functions for *
-
obj = static_cast(m->obj); // static_cast is C++ not C
obj->funcref(&h,&b); // The C compiler may not generate what you expect here
}How do you define
Test
andwrap_t
in your C code?Hi richard thanks for the reply obj->funcref(&h,&b); I know this wont work, don't know why I even typed it. Any way here goes file.cpp //------------ // CONSTRUCTOR //------------ Test::Test() { } //----------- // DESTRUCTOR //----------- Test::~Test() { } HRESULT Test::funcref(int &h, BSTR &b ) { HRESULT hr = S_OK char cstr[200]; strcpy_s(cstr, "BLAHHHHH"); *b = A2BSTR(cstr); return hr; } HRESULT Test::funcp(int i,int *y) { HRESULT hr = S_OK *y = i +7; return hr; } file.h class Test { public: Test(); virtual ~Test(); static HRESULT funcref( int &h,BSTR &b); static HRESULT funcp(int i, int *y); }; wrapit.cpp struct wrapStruct { void *obj; }; extern "C" __declspec(dllexport) wrap_t *wrap_create() { wrap_t *m; Test*obj; m = static_cast(malloc(sizeof(*m))); obj= new Test(); m->obj = obj; return t; } extern "C" __declspec(dllexport) void wrap_destroy(wrap_t *m) { if (m == NULL) return; delete static_cast(m->obj); free(m); } extern "C" __declspec(dllexport) int wrap_funcp(wrapper_L2H *m,int i,int* y) { Test *obj; if (m == NULL) return false; obj= static_cast(m->obj); obj->funcp(i, y); return true; } extern "C" wrapper_API void wrap_funcref(wrap_t *m, int* h,BSTR* b) //? { Test *obj; if (m == NULL) return false; obj = static_cast(m->obj); obj->funcref(h, b); return true; } wrapit.h #ifdef WRAPPER_EXPORTS #define wrapper_API __declspec(dllexport) #else #define wrapper_API __declspec(dllimport) #endif struct wrapStruct; typedef struct wrapStruct wrap_t extern "C" wrapper_API wrap_t* wrap__create(); extern "C" wrapper_API void wrap__destroy(wrap_t *m); extern "C" wrapper_API void wrap_funcref(wrap_t *m, int *h, BSTR* b); //? extern "C" wrapper_API void wrap_funcp(wrap_t *m, int *y; useit.c #include "wrapit.h" int z=0,uu=0,d=0,p=0; BSTR uu,nn; wrap_t *z = wrap_create(); wrap_funcref(&p,&nn); //not sure does not work wrap_funcp(z,&uu); //works wrap_destroy(z); wrap_funcp works wrap_funcref does not.
-
Hi richard thanks for the reply obj->funcref(&h,&b); I know this wont work, don't know why I even typed it. Any way here goes file.cpp //------------ // CONSTRUCTOR //------------ Test::Test() { } //----------- // DESTRUCTOR //----------- Test::~Test() { } HRESULT Test::funcref(int &h, BSTR &b ) { HRESULT hr = S_OK char cstr[200]; strcpy_s(cstr, "BLAHHHHH"); *b = A2BSTR(cstr); return hr; } HRESULT Test::funcp(int i,int *y) { HRESULT hr = S_OK *y = i +7; return hr; } file.h class Test { public: Test(); virtual ~Test(); static HRESULT funcref( int &h,BSTR &b); static HRESULT funcp(int i, int *y); }; wrapit.cpp struct wrapStruct { void *obj; }; extern "C" __declspec(dllexport) wrap_t *wrap_create() { wrap_t *m; Test*obj; m = static_cast(malloc(sizeof(*m))); obj= new Test(); m->obj = obj; return t; } extern "C" __declspec(dllexport) void wrap_destroy(wrap_t *m) { if (m == NULL) return; delete static_cast(m->obj); free(m); } extern "C" __declspec(dllexport) int wrap_funcp(wrapper_L2H *m,int i,int* y) { Test *obj; if (m == NULL) return false; obj= static_cast(m->obj); obj->funcp(i, y); return true; } extern "C" wrapper_API void wrap_funcref(wrap_t *m, int* h,BSTR* b) //? { Test *obj; if (m == NULL) return false; obj = static_cast(m->obj); obj->funcref(h, b); return true; } wrapit.h #ifdef WRAPPER_EXPORTS #define wrapper_API __declspec(dllexport) #else #define wrapper_API __declspec(dllimport) #endif struct wrapStruct; typedef struct wrapStruct wrap_t extern "C" wrapper_API wrap_t* wrap__create(); extern "C" wrapper_API void wrap__destroy(wrap_t *m); extern "C" wrapper_API void wrap_funcref(wrap_t *m, int *h, BSTR* b); //? extern "C" wrapper_API void wrap_funcp(wrap_t *m, int *y; useit.c #include "wrapit.h" int z=0,uu=0,d=0,p=0; BSTR uu,nn; wrap_t *z = wrap_create(); wrap_funcref(&p,&nn); //not sure does not work wrap_funcp(z,&uu); //works wrap_destroy(z); wrap_funcp works wrap_funcref does not.
wrap_funcref(&p,&nn); //not sure does not work
Where is
p
defined, and what is it? And where is the missing parameter that should be included per the declaration:extern "C" wrapper_API void wrap_funcref(wrap_t *m, int *h, BSTR* b); //?
Some of this might also make a bit more sense if it was properly formatted between <pre lang="c++"></pre> tags, as my samples above. And the use of single letters for variable names is confusing.