Writing a C wrapper for VC++

Posted on 2003-11-05
Last Modified: 2010-05-18
I have a package called OpenNurbs ( 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 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.
Question by:cdunn03
LVL 86

Assisted Solution

jkr earned 200 total points
ID: 9688081
Unfortunately, it is not possible to just 'view' the code of that package, so here's a more general approach:

class CWhatever {


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

// C-interface
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);
LVL 48

Accepted Solution

AlexFM earned 300 total points
ID: 9688201
You need to write Dll which exposes global functions. Internally it uses OpenNurbs classes. Suppose you have the following C++ class:

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

    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;
    return m.Sum();

int Subtract(int x1, int x2)
    CMath m;
    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;

void SetSecondArgument(void* pObject, int n)
    CMath* pMath = (CMath*)pObject;

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);


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now