Re: Leveraging C++ functions compiled with /CLR from other .NET languages
From: Bern McCarty (bern.mccarty_at_bentley.com)
Date: 04/14/04
- Next message: David Lowndes: "Re: Debugging on Win9x?"
- Previous message: mccoyn: "My Solution."
- In reply to: Gary Chang: "RE: Leveraging C++ functions compiled with /CLR from other .NET languages"
- Next in thread: Yan-Hong Huang[MSFT]: "Re: Leveraging C++ functions compiled with /CLR from other .NET languages"
- Messages sorted by: [ date ] [ thread ]
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.
> --------------------
>
- Next message: David Lowndes: "Re: Debugging on Win9x?"
- Previous message: mccoyn: "My Solution."
- In reply to: Gary Chang: "RE: Leveraging C++ functions compiled with /CLR from other .NET languages"
- Next in thread: Yan-Hong Huang[MSFT]: "Re: Leveraging C++ functions compiled with /CLR from other .NET languages"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|