Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Wrapping c++

Wrapping c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 12999132
    wrote on last edited by
    #1

    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 *

    L 1 Reply Last reply
    0
    • M Member 12999132

      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 *

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 and wrap_t in your C code?

      M 1 Reply Last reply
      0
      • L Lost User

        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 and wrap_t in your C code?

        M Offline
        M Offline
        Member 12999132
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • M Member 12999132

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups