Boppity Bop
Boppity Bop

Reputation: 10483

Can I inject few line of my code into someone elses .NET dll?

I want to replace one line of code in 3d party API with my own code.

I mean this is questionable practice but I need to fix their bug.

Upvotes: 3

Views: 2892

Answers (9)

Paul Farry
Paul Farry

Reputation: 4768

Thinking about this a little differently, is it possible you resolve the IP with DNS in the startup of your App before involving the spurious component, and then put an entry in the HOSTS file in system32\drivers\etc and that way you won't actually need to go out to DNS again (well you will, but it should be almost instant).

You may also instead of trying to do this via your assembly actually look at the Windows DLL api injection methods of hooking the call to the DNS Resolver to perform the caching.

Upvotes: 0

Tim Hoolihan
Tim Hoolihan

Reputation: 12396

Have you considered trying to proxy and intercept calls with an AOP tool, like Postsharp or LinFu?

Explanation: AoP frameworks (Aspect Oriented Programming) allow you to inject behaviour before an after calls, and reroute calls. More info here

Some added benefits of this vs changing MSIL:

  • The fix would survive upgrades of the 3rd party dll (given API is stable)
  • You should be within licensing terms
  • You can easily remove the proxy if they fix the method in question

Upvotes: 3

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

If it doesn't allow you to inject your own code and you don't get the source, you don't have a chance to do it in a legitimate way.

Assemblies normally are signed and you can't change it. Additionally, most software is protected by copyrights.

If you are an important customer for this vendor, ask for the code. And of course for the right to fix this bug.

Upvotes: 0

Mikael Svenson
Mikael Svenson

Reputation: 39695

Not condoning disassembling the code of third party or commercial software, but there is a way which can work.

If you use Reflector with the Reflexil plugin you can both remove strong name signing, and inject/change code in an assembly.

Upvotes: 7

Justin
Justin

Reputation: 8943

Yes you can. For one thing, the DLL assembly is just CIL (Common Intermediate Language). In principle you could modify it directly yourself.

For example, you could use a tool like Cecil:

http://www.mono-project.com/Cecil

You could also decompile it with a tool like Reflector into the language you are most comfortable with. At that point, you could just modify the code and recompile into your own custom assembly.

http://www.red-gate.com/products/reflector/

Cecil also has a decompiler:

http://evain.net/blog/articles/2008/12/15/cecil-decompiler

MonoDevelop (the Mono IDE) let's you open an assembly as a project. Just open the DLL as a project file and (if it has sufficient debugging info) it will look just like a code project that you can then modify and build.

http://monodevelop.com/

All these tools are usable in either Microsoft .NET or Mono. MonoDevelop can be installed on Windows without installing Mono at all.

Of course, I am saying that you can. I am not necessarily endorsing that you do. You will have to work out the legal and ethical side of things yourself since you know more about your situation.

Upvotes: 3

Achille
Achille

Reputation: 111

If it's a minor modification, you may have some luck round-tripping the code through ildasm then ilasm.

Upvotes: 0

drharris
drharris

Reputation: 11214

Why not just rewrite the buggy method yourself (inside your own code) and use that instead of the external library?

Upvotes: 0

Ben S
Ben S

Reputation: 69382

If you have access to the source code why not make the change and re-compile?

Upvotes: 0

STO
STO

Reputation: 10648

You can, but you will work with MSIL, not with C#.

http://ccimetadata.codeplex.com/ http://www.mono-project.com/Cecil

Also you may decomplie dll using Reflector.NET, fix bug and complie it back.

Upvotes: 5

Related Questions