Replacing a DLL with my own one

From an application, there is a call to functions in a DLL. I don't have access to the source code of this DLL, neither to the source code of the application. In one of the functions of the DLL there is an error that I want to solve. What I want to do is to rename the original DLL to another name. Then I will create a new DLL with the same name as the existing DLL and add a class with the same name as the Class in the original DLL. In the new DLL I implement a rewritten function of the function that causes the error in the original DLL. This will work.

The old situation:
      Application --------- Original DLL with Class 'Test_Class'
The situation I am looking for:
      Application --------- My own DLL with same name and Class --------- Original DLL with Class 'Test_Class'

The problem now is that the call of the other functions in the DLL are not available this way. I want to solve this by using inherits, but this throws an error. Example of the new class in the DLL:

Public Class GCS_General_Functions

    Inherits GCS.General.GCS_General_Functions

    Function X as string
 
   End Function

End Class

But this throws an error 'Class 'GCS_General_Functions' cannot inherit from itself'. Is there a way to solve this problem?
PirieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
The only way I can think of is to replace the original your dll with yours that supplies every function the original did.
it_saigeDeveloperCommented:
If the dependency is a signed assembly, it will not work.

Proof of concept.  Really simple console application:
Module Module1
	Sub Main()
		Console.WriteLine(EE_Q28758999_Common.Methods.Add(2, 2))
		Console.WriteLine(EE_Q28758999_Common.Methods.Subtract(2, 2))
		Console.WriteLine(EE_Q28758999_Common.Methods.Multiply(2, 2))
		Console.WriteLine(EE_Q28758999_Common.Methods.Divide(2, 2))
		Console.ReadLine()
	End Sub
End Module

Open in new window

Has a reference to the EE_Q28758999_Common project -Capture.JPGTest output -Capture.JPGIf we look at the metadata for EE_Q28758999 we find that it contains a signature that represents the EE_Q28758999_Common dll. -Capture.JPGNotice how the assembly is unsigned.  This means that so long as the dependent assembly has the same name and method structure, we can inline replace with our own.

If, however, the assembly is signed -Capture.JPGThen we cannot simply inline replace the dependent assembly.

-saige-
Jacques Bourgeois (James Burger)PresidentCommented:
If you have the source code for the application, then you can recreate the dll, remove the reference to the old one in the application, create a new reference to your own dll and then recompile the application.

This would go around the problem you would encounter if the original dll was signed, as any good dll should be.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

PirieAuthor Commented:
Hi guys,

Thanks for your answers on my question. The answer where I am looking for unfortunately not there yet.

So I have no source code of both the application and the DLL. The DLL indeed is signed. I only know what function I would like to change in the original DLL. To do so the only way I can imagine is to place my own new DLL with the same name as the original. In this new DLL is the function I want to change. The other functions from the original DLL would therefore have to be linked to the old DLL so I do not have to rewrite all the functions. Using inherits of a Class in a Class with the same name does not work (see my first post). Isn't there something possible like aliases or so? Anyone have an idea?

Regards,
Laurens Eykelkamp
AndyAinscowFreelance programmer / ConsultantCommented:
To repeat myself.
The only way I can think of is to replace the original your dll with yours that supplies every function the original did.
Both other experts have essentially said the same.


>>The answer where I am looking for unfortunately not there yet.
If you KNOW the answer then please tell us what it is.  Bear in mind that what you would like might not be possible.
Jacques Bourgeois (James Burger)PresidentCommented:
It used to work like that, but it caused lot of security and stability problems. So they added the signature mechanism in .NET.

A signature is there to insure that a dll cannot not be replaced by another one. Unless you have the private key (a special file) that was used to sign the original dll), or have the source code of the application so that you can recompile against another dll, you cannot replace the signed dll by another one with the same name.

For the framework, the signature becomes part of the name. This is called a strong name. It is used by the application when it tries to call the dll. Because your dll would not have the proper signature, it would be seen as being another dll, even if in your eyes, it would look as being the same.

Look at 2 locks. They look the same to the eye, but they are not. You need the proper key to use it. The same for digital signatures, that acts as locks for dlls and exes.
it_saigeDeveloperCommented:
To reiterate what has been stated here (and based upon the information you have further provided; i.e., a signed dll is used).

The answer to your question is:

No, you cannot simply inline replace the DLL with one of your own making.  To do so would require that you change the reference in the original application (which you do not have the source code for).

-saige-
AndyAinscowFreelance programmer / ConsultantCommented:
I'm getting a little confused with this signed or not aspect.
In the question:
>>In the new DLL I implement a rewritten function of the function that causes the error in the original DLL. This will work.

Specifically the highlighted bit.  You seem to say that you can replace the dll with your own and it does cure the error BUT now introduces other problems elsewhere in the app.  In other words you initially imply it is not signed.  Was the 'this will work' just a hope you expressed without actually testing?
PirieAuthor Commented:
Of course I don't know the answer, otherwise I should not have posted this question. What I meant was that it is not the answer I hoped for. Sorry for the confusion.

Of course I understand that the easiest way is to replace the complete DLL and create a new one with all the functions in it.

I thought the DLL is signed, but I think now that this is a different type of signing than you intend. I was referring to a code signing, signing a certificate to the application/DLL. I think this is a different type of signing than you intend. I think the DLL is suited to be replaced with another DLL.

Maybe it was better if i had formulated my question this way. We have a software application that contains of an executable and several DLLs. In one of the DLLs there is a function that I want to replace by my own function. Of course, there are more functions in the DLL then the one that I want to replace. I want to keep using that other functions. I don't have source code available of this application and the DLLs. Is it possible to do this?
AndyAinscowFreelance programmer / ConsultantCommented:
>> We have a software application that contains of an executable and several DLLs. In one of the DLLs there is a function that I want to replace by my own function. Of course, there are more functions in the DLL then the one that I want to replace. I want to keep using that other functions. I don't have source code available of this application and the DLLs. Is it possible to do this?

As far as I know, no.  You would have to replace all.  (Maybe it_saige has an idea, he mentions an inline replace in his first comment.)

By the way, if the app and dll aren't yours then why not ask the company that created them for a fix - especially if this is to fix a bug in their software.
it_saigeDeveloperCommented:
Again, the answer is no.  The answer does not change simply because you reword the question, you are still wanting to either:

A.  Replace a dll with one of your own making.
      i.   If the dll is signed you must change the reference in the main application.
      ii.  If the dll is not signed you must *recreate the entire dll*.  This way you do not have to change anything in the main application.

B.  Add a new dll with the new method in a new namespace (or use an extension method if this is preferable and possible).  With this method you must add a reference in the main application and you must change the line where the method is called to reference your new method.

-saige-
Jacques Bourgeois (James Burger)PresidentCommented:
There are different ways to sign a dll, but no matter which one you use, it is signed, so it cannot be replaced by another one, unless that other one also has the same signature. This is what signing is all about.

that it is not the answer I hoped for

Is an Integer variable sufficient to deal with a Bitmap.

Answer: No

This might not be what you hoped for, but this is the only valid answer.

Same for your question.

I thought the DLL is signed, but I think now that this is a different type of signing than you intend. I was referring to a code signing, signing a certificate to the application/DLL. I think this is a different type of signing than you intend. I think the DLL is suited to be replaced with another DLL.

A lot of thinking in there. And thinking that way is not a way of dealing with programming problems. You cannot get a straight solution out of thinking. You need to know the fact, and they you can find a solution. You have a signed dll or you don't. The answer is clear in both conditions. If you say Maybe it's signed (which is what thinking does in that situation), then you cannot expect a solution.
Jacques Bourgeois (James Burger)PresidentCommented:
@Ark

I would be very surprised to learn that such a technique will work with a signed dll.

Signed dlls are compiled with a CRC, that is checked back when the dll is loaded in memory. Any change to the dll will then be detected and flagged as a security exception. Security would be useless if it could be circumvented like that.
AndyAinscowFreelance programmer / ConsultantCommented:
I would suggest you try that method.  If it works then you are happy.  (In that case it also probably means your dll is not signed after all).
ArkCommented:
Just a suggestion - you can write your dll without recreating ALL function - just redirect to original dll.
1. Rename original dll to smth else, say OldOriginal.dll
2. Create your own dll with same name as original
3.
Public Class MainClass
    Private oldClass = Reflection.Assembly.LoadFrom( _
                       IO.Path.Combine(My.Application.Info.DirectoryPath, _
                       "OldOriginal.dll")).CreateInstance("Original.MainClass", True)

'Wrong function    
    Public Function Wrong(a As String, b As String) As String
        Return String.Format("Wrong function from replacement ({0},{1})", a, b)
    End Function
    'Other functions - just redirect call to original class
    Public Function Correct(a As String, b As String) As String
        Return oldClass.correct(a, b)
    End Function

End Class

Open in new window

4. Copy both dlls into app folder
For test: Original.dll
Public Class MainClass
    Public Function Wrong(a As String, b As String) As String
        Return String.Format("Wrong function from original ({0},{1})", a, b)
    End Function
    Public Function Correct(a As String, b As String) As String
        Return String.Format("Correct function from original ({0},{1})", a, b)
    End Function
End Class

Open in new window

Test app (first was referenced to Original.dll):
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim x = New Original.MainClass
        Debug.Print(x.Wrong(1, 2))
        Debug.Print(x.Correct(1, 2))
    End Sub

Open in new window

output:
Wrong function from replacement (1,2)
Correct function from original (1,2)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PirieAuthor Commented:
Sorry for my late response. I was out of the office last week and did not had the possibility to write a response.

Thank you all again for the answers. The method that Ark described in his last answer does work for me! Using the technique of Reflection made it possible for me to replace the wrong routine by the one that I want. The only thing is that all functions and routines had to be redirected, but it was possible to do this in this case.

I will close the question for now. If I need more information, I will open a new thread.

Thanks again!
AndyAinscowFreelance programmer / ConsultantCommented:
I'm rather surprised that worked.  How did you get around exporting the class?

>>But this throws an error 'Class 'GCS_General_Functions' cannot inherit from itself'
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.