Re: Leveraging C++ functions compiled with /CLR from other .NET languages

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Bern McCarty (bern.mccarty_at_bentley.com)
Date: 04/14/04


Date: Wed, 14 Apr 2004 10:01:14 -0400


Gary,

Can you explain how P/Invoke is relevant to me here? I'm trying to call
managed code from managed code. I'm pretty sure all of my structures are
perfectly representable as .NET value types so why would any marshaling be
necessary?

The code that I want to call just happens to be C++ code compiled with /CLR.
The functions I want to call are managed functions that take value types as
reference or out parameters. The problem appears to simply be that the
value types emitted by the compiler are, well, wrong in that they're all
empty. I will try to define redundant/extra value types that are right and
see how it goes trying to pass them into these methods. Assuming that I can
figure out how to get the compiler to be willing to convert a reference to
one value type (the one that I defined explicitly) to the one that the C++
compiler defined (that the methods actually expect and which are wrong) then
I guess it should work.

Even if it does work it is a lot of manual transformation of code that
already exists and already compiles into IL just fine and is already
callable from C++ just fine. Seems like there should be a shorter path to
having it be callable from C# too. I don't know if round tripping mixed
dlls through ildasm/ilasm is possible (probably not easily if at all) but if
it were I could imagine automating it and putting the missing value type
members back in... In my case these C structures are all "Plain Old Data".
No pointer members, nothing fancy. So it seems easy to imagine the C++
compiler at least having an option to emit accurate value types for POD
structures. It would be a huge boon to language interoperability. Yeah I
can do great stuff very easily with Managed C++, but only C++ programmers
are able to call the managed code that results. I don't want to cut out
programmers that prefer C#.

I'm trying to be friendly to the .NET programmer by having this library be
in managed code in order to eliminate managed/unmanaged transitions. Due to
the nature of and purpose of this library that seems important. But if I
can't figure this out I'm left with forgetting about that and just providing
a P/Invoke layer to the native version of the library. I'd hate to have to
trade performance for language interoperability (or for writing and
maintaining a bunch of new code that makes it work somehow).

-Bern

"Gary Chang" <v-garych@online.microsoft.com> wrote in message
news:DZdzd7gIEHA.348@cpmsftngxa06.phx.gbl...
> Hi Bern,
>
> >So how to arrange for these functions to be callable from say, C#, by
> passing value types as reference or out parameters?
> >
> You can create an equivalent structure in the C#(maybe need some
> Marshalling work for some unmanaged types), then pass its reference.
>
>
> >So how might I arrange to have these value types made "real" so that this
> API can be called from other .NET languages? Is there a secret Visual C++
> compiler switch that will get it to define these types fully? Some other
> way to make this API callable from C#?
> >
> There is no such a "Silver Bullet" compiler switch, my opinion is to use
> the types which have the directly corresponding .NET value types as the
> types of your DLL functions' parameters.
>
>
> By the way, I think maybe you needn't to care for such problems, the .NET
> program can PInvoke an unmanaged DLL easily:
>
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
> vcwlkPlatformInvokeTutorial.asp
>
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/ht
> ml/vcmg_PlatformInvocationServices.asp
>
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/
> vcgrfmarshalingstructures.asp
>
>
> Thanks!
>
> Best regards,
>
> Gary Chang
> Microsoft Online Partner Support
>
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> --------------------
>



Relevant Pages

  • Re: D8 may be 32 bit only
    ... Longhorn will be highly managed code and much ... When I made the Win64 vs. CLR64 comparison it was to make the point ... needs to needs to happen from the compiler side is to move to the CLR ...
    (borland.public.delphi.non-technical)
  • Peace between Native and .NET freaks (a proposal)
    ... Why can't borland do something like this with Delphi? ... .NET compiler with a full optimizer back end, ... # MC++ is your language of choice if you want full control of the .NET ... unmanaged and managed code, ...
    (borland.public.delphi.non-technical)
  • Re: Is Visual C++ .NET fully ANSI complaint?
    ... forgetting the MANAGED code and all that stuff) ... and at the time Comeau was the only compiler that could compiler ... Download BD Software's free STL Error Message ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Is Visual C++ .NET fully ANSI complaint?
    ... "Ernesto" wrote in message ... forgetting the MANAGED code and all that stuff) ... Every compiler has it's extentions. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Why we should (not?) have closures after all
    ... Programmers should have tools that can tell them as much as possible about their code's likely behavior. ... The wart can be used anywhere a type is used in declaring a reference ... The compiler does nothing special if a "can be null" reference is ... it would be nice if there was a variant of the "assert" statement that was always enabled and threw IllegalArgumentException instead of AssertionError. ...
    (comp.lang.java.programmer)