472,358 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

How to reach unmanaged API functions from managed code?

I've been instructed to work againt a huge unmanaged C++ API from a C#
application.

Now, the only way, as I've understood it, is to go the Managed Extensions
for C++ way. This means I have to write a wrapper between unmanaged API and
my managed app.

Now on to the question:

If there's an unmanaged API class called X with a defined method

TypeA* foobar(TypeB* b, TypeC* c);

then how can my managed code (my c# app) deal with the problem of inputing
TypeB and TypeC since they are not known or compatible with managed code,
and how should my c# app deal with the TypeA* return type?

Is there a way to "use" the unmanaged types "as they are" in the managed
code (maybe by uisng the API include files?) or do I have to do something
like this in my wrapper:

TypeA, TypeB and TypeC consists only of a public int member for simplicity.

public __gc class wrapper
{
public:
wrapper() {m_obj = new X;}
X* m_obj;
....

// exposed to the managed code to be able to
// talk to the API foobar method
int wrappedFoobar(int b, int c)
{
TypeB* bTmp = new TypeB(b);
TypeC* cTmp = new TypeC(c);

TypeA* aTmp = m_obj->foobar(bTmp, cTmp);
return aTmp->m_nInt;
}
}

This is EXTREMELY cumbersome if I want to use large parts of the API... or
not even doable if the types are really complex. Is there a way to reach
the unmanaged API where I'm not forced to practically rewrite the entire
library in managed code?

PLEASE, reply also to to***@grafu.se.

Sincerely,

/Tommy
Nov 17 '05 #1
3 1828
"Tommy Svensson (InfoGrafix)" <to***@infografix.nu> wrote in message
news:el****************@TK2MSFTNGP15.phx.gbl...
I've been instructed to work againt a huge unmanaged C++ API from a C#
application.

Now, the only way, as I've understood it, is to go the Managed Extensions
for C++ way. This means I have to write a wrapper between unmanaged API
and
my managed app.
Well, sometimes what developers do is to define a managed class in MC++
(soon to be C++/CLI) which exposes the same public interface. That done,
callers written in any CLS compliant language can create objects of the MC++
class.

Another option is to wrap up the C++ class into a COM object and then take
advanatge of the fact that .Net applications can consume COM objects.

A third option is to "flatten" the existing object oriented interface into a
procedural one which exposes its interface via exported DLL functions and
then use .Net's Platform/Invoke (aka P/Invoke) from .Net.
Now on to the question:

If there's an unmanaged API class called X with a defined method

TypeA* foobar(TypeB* b, TypeC* c);
then how can my managed code (my c# app) deal with the problem of inputing
TypeB and TypeC since they are not known or compatible with managed code,
and how should my c# app deal with the TypeA* return type?
Well, you can always treat the pointers as opaque cookies or handles on the
managed side of things. If you do that you will need to validate the
"handles" before you dereference them (or suffer the consequences <g>).
Depending on how complicated the types are you may be able to define
equivalent "structs" for C#.

IMO, the most elegant way, and the one that often involves the most work is
to define a .Net proxy class each of whose instances contain a pointer to an
instance of a ntaive object. The constructor of the proxy class would take
advantage of MC++'s "it just works" capbility to hop the fence into
unmanaged code and invoke the constructore for the managed object. Every
public method in the proxy class would then marshall its arguments, delegate
to the corresponding in the native object and unmarshall the result.

These articles address some basic interop issues from the perspective of teh
C# developer:

http://msdn.microsoft.com/library/de...rp09192002.asp

http://msdn.microsoft.com/library/de...asp?frame=true

http://msdn.microsoft.com/library/de...asp?frame=true

And the book "Essential Guide to Managed Extensions for C++" (APRESS
1-893115-28-3) written by Challa and Laksberg (of the VC++ team) does a good
job of explaining interop from the perspective of a C++ programmer.

Note that VS2005 is going to change the syntax of managed C++ in a major
way.
PLEASE, reply also to ...


Post it here, read it here.

Regards,
Will
Nov 17 '05 #2
> Well, sometimes what developers do is to define a managed class in MC++
(soon to be C++/CLI) which exposes the same public interface. That done,
callers written in any CLS compliant language can create objects of the MC++ class.

Yes, this is what I'm trying to do as it seems to be the fastest option.

Another option is to wrap up the C++ class into a COM object and then take
advanatge of the fact that .Net applications can consume COM objects.

No can do, the API is huge, more than 2 000 files and thousands of classes
and functions.

A third option is to "flatten" the existing object oriented interface into a procedural one which exposes its interface via exported DLL functions and
then use .Net's Platform/Invoke (aka P/Invoke) from .Net.

No, for the same reason as above.
If there's an unmanaged API class called X with a defined method

TypeA* foobar(TypeB* b, TypeC* c);
then how can my managed code (my c# app) deal with the problem of inputing TypeB and TypeC since they are not known or compatible with managed code, and how should my c# app deal with the TypeA* return type?

Well, you can always treat the pointers as opaque cookies or handles on the managed side of things. If you do that you will need to validate the
"handles" before you dereference them (or suffer the consequences <g>).
Depending on how complicated the types are you may be able to define
equivalent "structs" for C#.

Ok, so in the API I have a type called WString which is a unicode string
type only.

If I try to return a string of type WString from a MC++ class to managed
code like this

char* wrapper::returnString()
{
return m_apiObj->my_WString.c_str();
}

then how should I receive this string? Like this:

String str = wrapperObj.returnString();

Will this work? How will I know what unmanaged string are compatible with
..NETs single string type String?

IMO, the most elegant way, and the one that often involves the most work is to define a .Net proxy class each of whose instances contain a pointer to an instance of a ntaive object. The constructor of the proxy class would take
advantage of MC++'s "it just works" capbility to hop the fence into
unmanaged code and invoke the constructore for the managed object. Every
public method in the proxy class would then marshall its arguments, delegate to the corresponding in the native object and unmarshall the result.

Is there a list somewhere of what unmanaged types get unmarshalled to what
managed types?

These articles address some basic interop issues from the perspective of teh C# developer:


Thx!
Nov 17 '05 #3
"Tommy Svensson (InfoGrafix)" <to***@infografix.nu> wrote in message
news:u7**************@TK2MSFTNGP15.phx.gbl...
Ok, so in the API I have a type called WString which is a unicode string
type only.

If I try to return a string of type WString from a MC++ class to managed
code like this

char* wrapper::returnString()
{
return m_apiObj->my_WString.c_str();
}
Well, why do you want to a null terminated array of characters to your
managed proxy? That's done in the unmanaged world mostly because in the
beginning (K&R days) that's all there was. .Net has a string class, so it
seems more nature to invoke the constructor of System::String(). And if your
managed class is going to be used by, say C# clients or VB.Net clients, you
certainly don't want to be exposing character strings of null terminated
characters. Or do you?
Will this work? How will I know what unmanaged string are compatible with
.NETs single string type String?
Instances of System::String are composed of wide characters. Take a look at
its constructors and build one from your unmanaged array of characters.
Is there a list somewhere of what unmanaged types get unmarshalled to what
managed types?
I don't know. Perhaps someone else here has an idea.
Thx!


You are welcome.

Regards,
Will
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
4
by: 0to60 | last post by:
I'm trying to create a .dll with VS.NET 2003 Architect that contains a math computational component. I need the guts of the thing to be in native code, as performance is key for that part. But, I...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
4
by: repstat | last post by:
Hi I have a project which is going to be doing some string manipulation which needs to be pretty fast. The user interface is going to be written in C#. I am going to write the string handling...
2
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.