locked
Modifying a DLL file RRS feed

  • Question

  • Hi people,

    I am working with a third-party software. Some of the functionality isn't defined and I was told that I need to custom define them.
    Not sure why they can't do it for us instead of spreading the work. So now I needed to know how to add function to a third-party
    dll file. This is a PcmDllInt.dll file that I can right click and select view in Object Browser. But I am still not sure how to modify it.
    I can only reference this file and expose some of its basic functionality.

    BlueMarker
    Thursday, April 2, 2009 1:55 PM

Answers

  • Are you in .NET 2.0 VS 2005?  If so, you can't use extension methods anyways. 

    I know how you feel with the third party software thing.  I've definately experienced that before.  Very frustrating. 

    There's only one way I know of now to do what you're trying to do, that is to use Reflector.  There are a few add-ins for reflector that will allow you to output the result of the disassembler output. 

    Check with your friendly Intellectual Property Attorney before you do this, though.  It may not be legal depending on what their licensing agreement is.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by David M Morton Thursday, April 2, 2009 4:30 PM
    • Marked as answer by BlueMarker Thursday, April 2, 2009 5:05 PM
    Thursday, April 2, 2009 3:50 PM

All replies

  • I'd take a look at using extension methods for something like this, unless you absolutely positively need a property, or unless you're going to need access to the private fields and methods of the class.

    You could use extension methods in combination with some reflection to get at the methods internally.
    David Morton - http://blog.davemorton.net/
    Thursday, April 2, 2009 2:00 PM
  • Hi David,

    Thanks for replying.

    I did a little research into using extension. Am I right to say that I can use it as defined below?:

    namespace Extensions
    {
      using System;
      using PCMDLLINT;
    
         // Define extension methods for any type that implements IMyInterface.
         public static class Extension
         {
            public static void MethodA(this IMyInterface myInterface, int i)
            {
                Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, int i)");
            }
    
            public static void MethodA(this IMyInterface myInterface, string s) 
            {
                Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, string s)");
            }
    
            // This method is never called, because the three classes implement MethodB.
            public static void MethodB(this IMyInterface myInterface) 
            {
                Console.WriteLine("Extension.MethodB(this IMyInterface myInterface)");
            }
        }
    }
    
    So extension is basically a namespace that includes the DLL?

    And I am not sure how to implement reflection. I've heard of it before but seem way beyond my level.

    BlueMarker
    Thursday, April 2, 2009 2:37 PM
  • Good job for the first try. 

    I'm not sure what you mean by "a namespace that includes the dll", but your code is solid.

    What an extension method does is allows you to define new methods for instances of a type that can be called off of the type itself.  It can help to add some basic functionality to classes.  The Linq methods are great examples of extension methods.  They're not built into the IEnumerable<T> type, but they act as though they are. 

    Your example should work fine for the interface IMyInterface. 

    Just as a note, I'd do a couple of things:

    1. Make sure you define your namespace properly.  Regardless of they type you're extending, you'll need to have a using statement at the top of your code for those extension methods to appear. 
    2. The typical naming convention for defining extension methods is <TypeName>Extensions.  So your static class named Extension should really be named "IMyInterfaceExtensions".  This will help you to group your extension methods properly so as not to be confused. 

    Great job!
    David Morton - http://blog.davemorton.net/
    Thursday, April 2, 2009 2:52 PM
  • Thanks for replying David,

    The code came from MSDN, so I have to give them/microsoft credit I guess. But implementation is another thing. To be clear I got an error at "public static void MethodA(this IMyInterface myInterface, int i)". "this IMyInterface" gave me an error. I have no idea how to implement this IMyInterface.

    Another question is how do I call/use this extension. I would declare "using PCMDLLINT;" at the top and start using it below such as:
    "PCMDLLINT.PCM.PCMSCalculate(trip);" With this extension, I would declare it at the top as "using Extensions;"? Sorry I couldn't test this out due to the error I got above.

    And last question is, unfortunately, I can't define any custom method in this extension since I don't have the know how. Its a third-party software that have documentation on how to call these function/methods and use them but not on how to define these methods. So I have no real info on how to code the internal working of these functions.

    *Note: I would love to try Linq but it's available on framework 3.5 isn't it? As much as I love microsoft, I doubt my company would pay 
               for it, especially when the rewrite is already over budget and way behind schedule (none of which is my fault). And getting help
               from this third-party software is like pulling their finger nails. It takes a full week for them to respond to my email the first time
               around. Consecutive respond takes at least a day. And their 1-2 sentence respond is vague as it is. I am currently using 
               framework 2.0.


    BlueMarker

    • Marked as answer by BlueMarker Thursday, April 2, 2009 4:18 PM
    • Unmarked as answer by BlueMarker Thursday, April 2, 2009 5:04 PM
    Thursday, April 2, 2009 3:44 PM
  • Are you in .NET 2.0 VS 2005?  If so, you can't use extension methods anyways. 

    I know how you feel with the third party software thing.  I've definately experienced that before.  Very frustrating. 

    There's only one way I know of now to do what you're trying to do, that is to use Reflector.  There are a few add-ins for reflector that will allow you to output the result of the disassembler output. 

    Check with your friendly Intellectual Property Attorney before you do this, though.  It may not be legal depending on what their licensing agreement is.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by David M Morton Thursday, April 2, 2009 4:30 PM
    • Marked as answer by BlueMarker Thursday, April 2, 2009 5:05 PM
    Thursday, April 2, 2009 3:50 PM
  • Thanks for replying David,

    Well I guess I am back on square one. And be at the mercy of the third-party software company. I'll keep hammering them with questions and keep researching/expanding my knowledgebase in the meantime.

    As always, your responds is helpful.

    BlueMarker
    Thursday, April 2, 2009 4:17 PM
  • Would you mind checking which post you marked as an answer?  It seems as though my last post was more helpful than your response prior to it.
    David Morton - http://blog.davemorton.net/
    Thursday, April 2, 2009 4:29 PM
  • Sure, the Mark As Answer used to be at the top and I thought it's the same.

    ? wow, I marked your comment as answered and it also jumped to second from the top?

    Weird, Microsoft or someone need to fix this updates.

    BlueMarker
    Thursday, April 2, 2009 5:06 PM