Fixing a problem in a dll for which you do not have source code

When you somehow want to fix a business need and there is no way out, you may have to resort to this method. I have a few links here which will be of use in that case because I had to do this recently:

http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/05b3cf5d-ead3-4274-88f5-6e8cbda8e8d8/

An intriguing thing I found out that looks like valid C# gets converted into IL and gets decompiled - but when you get the source code like this, it may not compile properly, because apparently, the C# compiler does not  like many things in this decompiled code which probably is optimized out of the IL.

So, you need to look at each case and work around it. You may have to use keywords like unchecked, new, etc. In some cases, you may have to explicitly cast objects (see how the code is in the decompiler and then figure out that it needs to be cast to X or Y class).

If you deal with this dll being a wrapper around C++ dlls which are called from within them, the following links will be useful to deal with it:

http://www.developmentnow.com/g/21_2003_9_0_0_103952/MarshalDirectiveException--Array-size-control-parameter-must-be-an-integral-type.htm

http://msdn.microsoft.com/en-us/library/z6cfh6e6.aspx#cpcondefaultmarshalingforarraysanchor3

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshaldirectiveexception.aspx

I had a case where code like:

void New1([MarshalAs(UnmanagedType.LPArray, SizeConst=10)] int[] ar);
was throwing a runtime error (see exception in the reference). I used ref instead of MarshalAs attribute as per the reference and it works fine!.
[Update] Initially the decompiled dll could not be debugged and/ or would provide proper correct line numbers in the exceptions. After a few days, it started working. We did not change anything explicitly though. 
[Update] The weird debug behavior continues. But it is all working now.
Note: In the end what we ended up doing is that in all such cases, you want to be able to use the latest dll if the vendor releases an update, so we moved 95% of the code outside the dll (because it uses an interface and we just create a class in our code, implementing the interface - we modified this interface so it could give us more control which was needed).

Comments

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

The ridiculous interview experience