views:

170

answers:

9

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 (the idiots call ResolveDNS on every method call which writes to TCP socket - what they were going to achieve with it?) which results in untolerable lags. I want to cache DNS name in static field and screw them!

Their support havent answered for 3d in a row. I lost my patience.

+1  A: 

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.

Stefan Steinegger
Yes, but he can decompile assembly and complie it again without signing. This is license agreement violation, but modifying MSIL is violation too.
STO
That's what I mean by 'legitimate way'.
Stefan Steinegger
I dont have license agreement..
Bobb
@Bobb: I'm not an expert in this, but I would say that copyrights are implicit to some degree. I just propose to ask before just taking code someone else wrote and change it and use it in your own product.
Stefan Steinegger
sure Stefan will do
Bobb
+5  A: 

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.

STO
This is usually not allowed...
Stefan Steinegger
@STO: *then compile it again*.
mcandre
I just mean: you should ask for the permission to do so. If you get it, you should also get the source code. If you don't, you shouldn't do it.
Stefan Steinegger
The reflexil plugin for Reflector allows you to inject both IL and C# as mentioned in my answer. But, by doing this you might infringe on something you shouldn't.
Mikael Svenson
As long as you don't distribute your changed version, I don't see how they would have any legal recourse. It isn't like you are using something without paying for it.
Jonathan Allen
I havent signed any legal papers on using this software.. soooo... I think.... I dont care ! :)
Bobb
@Jonathan: You can't know. MS for instance forbids to decompile any assemblies... In many cases, not only distributing is forbidden, also decompiling or changing in general. Don't misunderstand me, I don't like it either, I don't think that it makes sense. It's just how it is for common copyrighted stuff. Ignoring it is illegal.
Stefan Steinegger
Some years ago someone where I worked disassembled a partners dll since we got no support after repeated e-mails/phone calls on a super slow API (which they knew was slow). The code got improved by 200x in terms of speed with some simple caching.Later when a new contract was being negotiated it came out that we had a performant version going, and they wondered how that was possible with the dll we got from them. The cat was out of the bag, and they got really pissed, almost breaking the deal.Personally anyone may disassemble and improve my code. Win-win imo.
Mikael Svenson
@Mikael: If it is win-win, you could tell it from the beginning and get not only the permission, but also the source. That's what I call win-win.
Stefan Steinegger
@Stefan, I agree, but it's hard to get permission when you don't get an answer. When stuck between fulfilling a contract and no response from a necessary third party, it's easy to just go ahead. Money talks. That said, as a developer you should never have to make that decision.
Mikael Svenson
A: 

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

Ben S
the code I can see through Reflector
Bobb
A: 

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

drharris
because the buggy method is inside the other 3d party methods. I found it through Reflector.
Bobb
+2  A: 

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
Tim Hoolihan
Please add links to the tools you are talking about.
Jonathan Allen
yeah I never heard about this.. Linux probably... I cant - this is real-time. I want to fix the bug to reduce latency. Not to add it with external tools
Bobb
Bobb, I added some links to point you in this direction. This is a realtime solution, and has nothing to do with linux or network proxies. These frameworks let you create objects that look like your 3rd party library, but you can configure and reroute certain method calls. There is a no alteration of the 3rd party dll. These libraries do what you are asking to do, the question is if there is a simpler solution.
Tim Hoolihan
+1 for completely different approach.
Stefan Steinegger
A: 

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

Achille
+1  A: 

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.

Justin
+4  A: 

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.

Mikael Svenson
yeah thats what I wanted. but apparently their support guy is now looking at the problem. so I might not need it after all :)
Bobb
got fix from the vendor yesterday! no hacks needed :)
Bobb
A: 

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.

Paul Farry