from .lib and .h files, how to get a managed wrapper

D

DBT

I'm sure this is simple if you know how.

I've got .h files and .lib files for a C language API that I need to access
from a C# assembly.

For the sake of this question, assume there are two .h files (ctap.h and
rtap.h) and three .lib files (a.lib, b.lib, c.lib). There are six functions
to be accessed (void afunc1(int), char afunc2(char []), void bfunc1(float),
double bfunc2(int *), void cfunc1(char), char[] cfunc2(char [])).

Can you provide a quick example, or the code, or a clear walkthrough on how
to wrap these in a C++ managed wrapper that will allow a C# assembly to
access the 6 named functions?
 
J

Jochen Kalmbach

DBT said:
I'm sure this is simple if you know how.

I've got .h files and .lib files for a C language API that I need to
access from a C# assembly.

For the sake of this question, assume there are two .h files (ctap.h
and rtap.h) and three .lib files (a.lib, b.lib, c.lib). There are six
functions to be accessed (void afunc1(int), char afunc2(char []), void
bfunc1(float), double bfunc2(int *), void cfunc1(char), char[]
cfunc2(char [])).

Can you provide a quick example, or the code, or a clear walkthrough
on how to wrap these in a C++ managed wrapper that will allow a C#
assembly to access the 6 named functions?


Create a managed C++ assembly:
- "File | New | Project | Visual C++ Projects | Class Library (.NET)"


#include "ctap.h"
#include "rtap.h"

#pragma comment(lib, "a.lib)
#pragma comment(lib, "b.lib)
#pragma comment(lib, "c.lib)

namespace MyWrapper {
public __gc class Class1 {
public:

static void Managed_afunc1(int value)
{
afunc1(value);
}

// The following is a little bit complicated...
// what is "char" ? (string or byte array)
static char Managed_afunc2(char [])
{
return 0;
}

static void Managed_bfunc1(float value)
{
bfunc1(value);
}

// The following is also complicated...
// what is "int *" ? (menaing of in/out, or array ?)
static double Managed_bfunc2(int *)

// What is char !? (String-Char !? or 1 byte?)
static void Managed_cfunc1(char value)
{
cfunc1(value);
}

// What is char !? (String char or byte array?)
static char[] Managed_cfunc2(char []));

};
}


In C# you can simply make a reference to this DLL and use it:

Example:
MyWrapper.Class1.Managed_afunc1(12);


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top