Writing a C wrapper for VC++

I have a package called OpenNurbs (www.opennurbs.org) that is written in VC++ and compiled as a DLL. I need to use the functions in this package in Matlab. Matlab can call a standard Windows DLL, but the DLL must have a C interface, since Matlab is written in C.

Is there an efficient way to to create this interface using either VC++ 6 or VC++.net? I have the standard version of both compilers.

I have some programming background, but am a physicist by trade, so I apologize if this task is easier than I think it is!

Thank you for your help.
cdunn03Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Unfortunately, it is not possible to just 'view' the code of that package, so here's a more general approach:

class CWhatever {

public:

   int SampleMethod ( int i) { return 2 * i;}
};

// C-interface
typedef DWORD HOBJECT;
HOBJECT CreateObject ( <params>) { return ( HOBJECT) new CWhatever(<params>);}

int SampleMethod ( HOBJECT hObj, int i) { return (( CWhatever*) hObj)->SampleMethod ( i);}

//...
HOBJECT hObj = CreateObject ( ...);

int n = SampleMethod ( hObj, 2);
AlexFMCommented:
You need to write Dll which exposes global functions. Internally it uses OpenNurbs classes. Suppose you have the following C++ class:

class CMath
{
public:
    int Sum();
    int Subtract();
    int SetFirstArgument(int n){m_n1 = n}
    int SetSecondArgument(int n){m_n1 = n}

protected:
    int m_n1;
    int m_n2;
};
   
Let's make two operations using C-style exported functions.
First approach.

int Sum(int x1, int x2)
{
    CMath m;
    m.SetFirstArgument(1);
    m.SetSecondArgument(2);
    return m.Sum();
}

int Subtract(int x1, int x2)
{
    CMath m;
    m.SetFirstArgument(1);
    m.SetSecondArgument(2);
    return m.Subtract();
}

Client code:
int n = Sum(1, 2);
int m = Subtract(1, 2);


Second approach is better. It allows to C client to create class, use it and delete when necessary:

void* CreateObject()
{
    CMath* pMath = new CMath();
    return (void*)pMath;
}

void DeleteObject(void* p)
{
    delete (CMath*)p;
}

void SetFirstArgument(void* pObject, int n)
{
    CMath* pMath = (CMath*)pObject;
    pMath->SetFirstArgument(n);
}

void SetSecondArgument(void* pObject, int n)
{
    CMath* pMath = (CMath*)pObject;
    pMath->SetSecondArgument(n);
}

void Sum(void* pObject)
{
    CMath* pMath = (CMath*)pObject;
    return pMath->Sum();
}

void Subtract(void* pObject)
{
    CMath* pMath = (CMath*)pObject;
    return pMath->Subtract();
}

Client code:

void* p = CreateObject();
SetFirstArgument(p, 1);
SetSecondArgument(p, 2);
int n = Sum(p);
int m = Subtract(p);
DeleteObject(p);






Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.