6

that approach in "possible duplicate" is different to the one in the book. The book's approach is to extend a managed .dll after compilation through a decompile, recompile process. The approach that you think is an exact duplicate is actually a pre compilation technique. I prefer method in the book because it is more AOP in style. Thanks for the link though. I will also explore this avenue

In Expert .NET 2.0 IL Assembler, in Ch 18 pp. 387 Serge Lidin talks about Creative Round-Tripping. He says, "ILAsm allows you to export the managed methods as unmanaged entry points".

I haven't heard this talked about anywhere else. Is this something that you can do in PostSharp? Are there any downloadable code / script examples of using this technique available?

To clarify, I don't want to use COM or Managed C++. So what's the best way to implement a call to a C# method from C/C++ through thunking?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
sgtz
  • 8,849
  • 9
  • 51
  • 91
  • @PaulSasik: that approach is different to the one in the book. The book's approach is to extend a managed .dll after compilation. This approach is pre compilation. I prefer method in the book because it is more AOP in style. Thanks for the link though. I will also explore this avenue. – sgtz Mar 22 '12 at 13:07
  • 3
    The [bottom answer of that question](http://stackoverflow.com/a/4605486) links to [Unmanaged Exports](https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports) which sounds like your citation. – Rup Mar 22 '12 at 13:12
  • @Rup: yes it is. Thanks for pointing that out. Pity there's no working downloadable example though. – sgtz Mar 22 '12 at 13:19
  • @MarcGravel: do you think that closing the question was the right thing to do? – sgtz Mar 22 '12 at 13:22
  • fine, give it another go, but: I suspect it was – Marc Gravell Mar 22 '12 at 13:41
  • @MarcGravell: ty. I have some good leads to follow up on now, but maybe somebody will be able to fill in the blanks a little better. I'm surprised that this technique hasn't had more exposure. It appears to be really useful in some specific situations. – sgtz Mar 22 '12 at 13:47
  • 1
    What do you mean about no working downloadable example? I've downloaded the code from Robert's site. Works well. – David Heffernan Mar 22 '12 at 14:23
  • 2
    @DavidHefferman: was in the middle of something and typed before I looked thinking I knew entirely what was on that link from memory. I didn't. I downloaded that too. It works well? That's great. Can't wait to give it a try. Does the managed code also perform well? – sgtz Mar 22 '12 at 16:13
  • Is anyone going to link the article as an answer and tell us if the sample code behaved as described? – Joshua Drake Mar 29 '12 at 15:15
  • @JoshuaDrake: I now have it on good authority that the technique from https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports came from the book I mentioned. It's working for people. I haven't run extensive tests on it yet. I will get back to the thread once I have. IL manipulation is a valid technique. It's the basis of the PostSharp product http://www.sharpcrafters.com/. However Anders (http://en.wikipedia.org/wiki/Anders_Hejlsberg) doesn't like the technique (changing IL). Not sure what to say on that. – sgtz Mar 30 '12 at 03:14

1 Answers1

3

ildasm, change corflags, add v-table info and export function, ilasm and you're done.

Here's a code project article: http://www.codeproject.com/Articles/37675/Simple-Method-of-DLL-Export-without-C-CLI

Which is based on:

Dead link: http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/

Wayback Machine for dead link: https://web.archive.org/web/20140213030149/http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/

Which is based on: http://www.amazon.com/Inside-Microsoft-NET-IL-Assembler/dp/0735615470 (the first edition of the book you refer to)

Eric Dahlvang
  • 8,252
  • 4
  • 29
  • 50