4

When importing a COM library (either directly with tlbimp, or indirectly with visual studio add reference dialog,) is there a way to generate C# source code instead of a binary interop assembly, like Interop.Word.dll, for example?

UPD: Reflector is bad idea. Problem is that for the com interface is not only a signature, but the order of the members. Reflector this order is violated

  • Your question is a little unclear. Are you trying to view the code in the DLL?... and translate it to c#?... gonna have a hard time with that. =) – Chris Barlow Jun 01 '11 at 14:41
  • I know what he's talking about - I've update the title and question to be a little clearer. If you still don't understand, then you're either being facaetious or you don't know squat about unmanaged interop. – x0n Jun 01 '11 at 14:50
  • 1
    to x0n: Thank you. You completely understood me –  Jun 01 '11 at 16:04
  • Similar question: http://stackoverflow.com/questions/1058289/how-do-i-generate-com-interop-proxies-into-c-source-code – Kevin Hsu Jun 01 '11 at 17:57

3 Answers3

1

I would go ahead and generate the interop assembly using TLBIMP, and the use Reflector to disassemble it. The interop assembly has no actual implementation code, as you will see. You can then just copy and paste the code (or the CoClasses and interfaces you need) into a new .cs file in your project.

Kevin Hsu
  • 1,726
  • 11
  • 14
  • No, problem is that for the com interface is not only a signature, but the order of the members. Reflector this order is violated. –  Jun 01 '11 at 17:01
  • Then reorder them by hand. You have the source code, after all. Reflector is just showing you what TLBIMP generated, it's not generating anything else. – Kevin Hsu Jun 01 '11 at 17:02
  • Try to check his proposal into practice :( –  Jun 01 '11 at 17:47
0

You cannot generate C# directly, but if your real problem is that you have a need to tweak the interop library then you might have better luck using the CLR Interop team's custom TLBIMP tool on codeplex - it's pretty flexible and you have full source [to it.]

http://clrinterop.codeplex.com/releases/view/17579

Update: If you're really trying avoid shipping binaries, then you could feasibly integrate the above tool (in source format) into your project so that you generate this interop library at runtime. Result? No binaries to ship.

x0n
  • 51,312
  • 7
  • 89
  • 111
  • Thank you but I need c# instead of dll as result :( –  Jun 01 '11 at 16:01
  • Why do you need source? I've updated my answer with an alternate idea based on the above. – x0n Jun 01 '11 at 16:13
  • Where? In Memory? You understand that this is another pair of crutches :( –  Jun 01 '11 at 17:55
  • I do not understand why MS has not made it possible to generate the source? May have paid products? –  Jun 01 '11 at 17:56
0

In general it cannot be done. C# does not support all possible constructs needed to create a working interop assembly. You cannot add e.g. attributes to return types or method parameters in C# although they are needed sometimes to help the marshaller to correctly translate some C++ types.

Yours, Alois Kruas

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
  • But you CAN add attributes to return types and method parameters in C#. http://stackoverflow.com/questions/131456/how-do-you-apply-a-net-attribute-to-a-return-type – Qwertie Jun 01 '11 at 18:54