0

I have number of cpp files in unmanaged c++ , I want to access class methods of these files from vb .net using P\Invoke, For that i have write C wrapper for exposing class methods.Can anybody help me ,how to write a c wrapper around C++ code. I am copying some code of my files ,please help me writting c wrapper for these function.

#include "StdAfx.h"
#include "Verify.h"

Verify::Verify(void)
    :_verified(false)
{
}

Verify::~Verify(void)
{
}

void Verify::SetVerified(bool value)
{
    _verified = value;
}

bool Verify::GetVerified(void) const
{
        _verified;
}

void Verify::SetFailurePoint(std::basic_string<TCHAR> const & value)
{
    _failurePoint = value;
}

std::basic_string<TCHAR> const & Verify::GetFailurePoint(void) const
{
    return _failurePoint;
}
Xaruth
  • 4,034
  • 3
  • 19
  • 26
sachin
  • 311
  • 2
  • 13
  • 24

2 Answers2

2

In wrapper.h:

    typedef void * VERIFY_HANDLE;
    extern VERIFY_HANDLE Verify_Create();
    extern void VERIFY_SetVerified(VERIFY_HANDLE, bool);
    extern bool VERIFY_GetVerified(VERIFY_HANDLE);
    /* etc, etc */

In wrapper.c:

    #include "wrapper.h"
    #include "Verify.h"
    VERIFY_HANDLE Verify_Create() { return (VERIFY_HANDLE) new Verify(); }
    void SetVerified(VERIFY_HANDLE h, bool b) { ((Verify *)h)->SetVerified(b); }
    bool GetVerified(VERIFY_HANDLE h) { return ((Verify *)h)->GetVerified();  }
David H
  • 1,461
  • 2
  • 17
  • 37
1

It'll be like:

extern "C" {
    typedef void *VERIFY;

    VERIFY create_verify() {
        return (VERIFY)new Verify();
    }
    void verify_set(VERIFY verify, int value) {
        ((Verify*)verify)->SetVerified((bool)value);
    }
    int verify_get(VERIFY verify) {
        return ((int)((Verify*)verify)->GetVerified());
    }
}

More info at Oracle pages

Hauleth
  • 22,873
  • 4
  • 61
  • 112