View previous topic :: View next topic |
Author |
Message |
Eelco Hoogendoorn
Joined: 23 Jan 2011 Posts: 6 Location: Netherlands
|
Posted: Sun Jan 30, 2011 9:30 am Post subject: C# interop |
|
|
Hi,
I am trying to combine C# and D, to get what is to me the best of both worlds; large libraries and excellent toolchain, with to the metal programming.
Currently, I am doing this by means of C#/CLI/C++ interop, but I got reminded as to why I hate C++ so much after spending a few days trying to get something as simple as ranges to work. But the interop works smooth; no complaints there.
So, visual D to the rescue, I figured.
But I am a little bewildered by all the options. What would be the best way to go about this? The D documentation is kindof cryptic to a non-professional like me. It doesnt seem as if I will be able to use D classes inside CLI, so I think that means I might as well ditch it altogether and just use Pinvoke on a D DLL? What are the relative advantages of a LIB versus a DLL?
Any suggestions are appreciated!
Regards,
Eelco |
|
Back to top |
|
|
sagitario
Joined: 03 Mar 2007 Posts: 292
|
Posted: Mon Jan 31, 2011 1:09 pm Post subject: Re: C# interop |
|
|
Hi,
Eelco Hoogendoorn wrote: | I am trying to combine C# and D, to get what is to me the best of both worlds; large libraries and excellent toolchain, with to the metal programming. |
Quote: | But I am a little bewildered by all the options. What would be the best way to go about this? The D documentation is kindof cryptic to a non-professional like me. It doesnt seem as if I will be able to use D classes inside CLI, so I think that means I might as well ditch it altogether and just use Pinvoke on a D DLL? What are the relative advantages of a LIB versus a DLL?
|
I don't have much experience with C#, most of it comes from studying the example implemetation of a language service for Visual Studio. At the beginning I used something like
Code: |
[DllImport("myd.dll", CharSet=CharSet.Ansi)]
static extern IntPtr parseFile([MarshalAs(UnmanagedType.LPStr)] string file, bool semantics);
|
to import functions from a DLL compiled from D source code. I guess that this is similar to the Pinvoke method that you mentioned above.
The downside is that there is no common memory management, but I don't think this is feasable anyway. Also it's probably no good to try to link a static library built with dmd to Microsoft compiled code, because the C runtime libraries are different, but use identical names.
A good thing is that mixed debugging C# and D works quite nicely.
Best,
Rainer |
|
Back to top |
|
|
Eelco Hoogendoorn
Joined: 23 Jan 2011 Posts: 6 Location: Netherlands
|
Posted: Mon Jan 31, 2011 2:51 pm Post subject: |
|
|
Thanks a bunch.
Quote: | The downside is that there is no common memory management, but I don't think this is feasable anyway. Also it's probably no good to try to link a static library built with dmd to Microsoft compiled code, because the C runtime libraries are different, but use identical names.
|
I see.
Quote: |
A good thing is that mixed debugging C# and D works quite nicely.
|
I dont quite see, since im not even aware of what could go wrong; but if you say so ;)
So summarizing, youd say writing a D DLL with extern(C) should work well. As opposed to CLI, I will not be able to access types directly, but I will have to interop by free functions or mashalled data. Right?
As for transferring data; I can read unsafe pinned C# allocated pointers in D, and the other way around, right? |
|
Back to top |
|
|
sagitario
Joined: 03 Mar 2007 Posts: 292
|
Posted: Tue Feb 01, 2011 1:52 am Post subject: |
|
|
Eelco Hoogendoorn wrote: | So summarizing, youd say writing a D DLL with extern(C) should work well. As opposed to CLI, I will not be able to access types directly, but I will have to interop by free functions or mashalled data. Right? |
As said before, I actually don't have much experience with it, but I'd say yes. Please note that you will have to keep references to pointers passed to the other language to avoid collection by the garbage collector.
Quote: |
As for transferring data; I can read unsafe pinned C# allocated pointers in D, and the other way around, right? |
That should work.
Another option might be to use COM objects. That's what Visual D is using, but you should consider applying the patch in http://d.puremagic.com/issues/show_bug.cgi?id=4092. I don't know the C# side of the COM interfaces, but isn't it supposed to integrate easily?
Rainer |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|