none
DLL Injection in VB Net RRS feed

  • Question

  • Hi Everyone,

    I need some help with this, and know if it is good:

    I create a VB NET DLL with this code:

    Imports System.Windows.forms
    Public Class YCE_DLL
        Public Shared Function GetForm(Name As String) As Form
            Dim frm As Form 
            Dim frms = Application.OpenForms
            For Each i As Form In frms
                If (i.Name = Name) Then
                    frm = i
                    Exit For
                End If
            Next
            GetForm = frm
        End Function
    End Class

    So its to get the form of the application. I want to get the form object

    Now i Want to inject this DLL into this custom .NET application:

    and get the textbox for example. so i thought... maybe if i can inject a DLL into the application to get the form it could be possible to get Textbox object to view its native properties.

    There's any way to do that? and how i can do it? i have try with this:

    Dim dllpath As String = "path to custom YCE_DLL.dll"

    GetWindowThreadProcessId(aeProgram.Current.NativeWindowHandle, pid)
            Dim hHandle = OpenProcess(ProcessAccessFlags.All, False, pid)
            '-----Allocating Memory------------
            Dim dllPathAddr = VirtualAllocEx(hHandle, 0, Len(dllpath), AllocationType.Reserve Or AllocationType.Commit, MemoryProtection.ExecuteReadWrite)
            '-----Expanded---------------------
            Dim tf = WriteProcessMemory(hHandle, dllPathAddr, dllpath, Len(dllpath), 0) 'tf=true
            Dim j
            RemoteLibraryFunction(hHandle, "YCE_DLL.dll", "getform", ListBox1.Items(ListBox1.SelectedIndex), 1, j)

    Private Function RemoteLibraryFunction(ByVal hProcess As IntPtr, ByVal lpModuleName As String, ByVal lpProcName As String, ByVal lpParameters As Object, ByVal dwParamSize As IntPtr, ByVal ppReturn() As Object) As Boolean
            Dim lpRemoteParams As Object = Nothing

            Dim lpFunctionAddress As Object = GetProcAddress(GetModuleHandle(lpModuleName), lpProcName) 'Result=0 (fail)
            Dim o1 = GetModuleHandle(lpModuleName) 'It retrieves a handle

    end function

    what i'm doing wrong or maybe i'm missing something, please help!

    the final objective its this:


    Yordy Corrales

    This question it's related with this one: https://social.msdn.microsoft.com/Forums/vstudio/en-US/23965a39-9002-48cc-9bc3-f3e0a338646e/get-external-errorprovider-tooltip-message?forum=vbgeneral#4c754070-1397-41a3-a28e-0710d2473b22
    Friday, March 8, 2019 1:19 AM

All replies

  • That I know about, you don't inject a DLL into anything. You can set refernce to a DLL in a project and dependency inject an instance of  class/object from a DLL into a class/object that wants to use a method on the  instanced class/object that has been injected into a class/object instance needing to use an object from the DLL.

    Lets start wit class vs object vs instance, which is OO 101 basics. .NET and  Java are object oriented language platforms and the principles are the same between the two

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Now let us  talk about dependency injection.

    https://medium.freecodecamp.org/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f

    Now let us talk about dependency injection form a VB.NET perspective.

    https://devblogs.microsoft.com/vbteam/dependency-injection-with-visual-basic-net-part-1/

    https://devblogs.microsoft.com/vbteam/dependency-injection-with-visual-basic-net-part-2-ioc-containers/


    • Edited by DA924x Friday, March 8, 2019 2:18 AM
    Friday, March 8, 2019 2:13 AM
  • Hi,

    reference dll

    use 

    Imports testdll
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "error")
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dllform As Form = YCE_DLL.GetForm("FormName")
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 8, 2019 3:10 AM
  • Hi,

    The same is true for external applications, you can try it out.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 8, 2019 5:35 AM
  • Hi,

    reference dll

    use 

    Imports testdll
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "error")
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dllform As Form = YCE_DLL.GetForm("FormName")
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    This is helpful if its the same application, but i need to do it in an external aplication, the screen that i attach it's a final .exe. I can't add a reference like that.

    Yordy Corrales


    Friday, March 8, 2019 5:35 AM
  • That I know about, you don't inject a DLL into anything. You can set refernce to a DLL in a project and dependency inject an instance of  class/object from a DLL into a class/object that wants to use a method on the  instanced class/object that has been injected into a class/object instance needing to use an object from the DLL.

    Lets start wit class vs object vs instance, which is OO 101 basics. .NET and  Java are object oriented language platforms and the principles are the same between the two

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Now let us  talk about dependency injection.

    https://medium.freecodecamp.org/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f

    Now let us talk about dependency injection form a VB.NET perspective.

    https://devblogs.microsoft.com/vbteam/dependency-injection-with-visual-basic-net-part-1/

    https://devblogs.microsoft.com/vbteam/dependency-injection-with-visual-basic-net-part-2-ioc-containers/


    Thanks, i appreciate it, but i'm looking for what i'm asking. Thanks again.

    Yordy Corrales

    Friday, March 8, 2019 5:36 AM
  • Hi,

    reference dll

    use 

    Imports testdll
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "error")
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dllform As Form = YCE_DLL.GetForm("FormName")
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    This is helpful if its the same application, but i need to do it in an external aplication, the screen that i attach it's a final .exe. I can't add a reference like that.

    Yordy Corrales


    Hi,

    external aplication reference the dll,then call the function of the class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 8, 2019 5:44 AM
  • Hi,

    The same is true for external applications, you can try it out.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Noup, isn't true for external apps. How can i get the .net properties of a textbox from application2 as shown in the picture #2 i attached?. Aplication2 it's an EXE program, but i need to receive the controls from the application2. in application1 i just make the calls to application2. application2 its running. i cant debug it, remember that its an EXE program. not a VB Project, but it was built/Compile in .NET

    Yordy Corrales

    Friday, March 8, 2019 5:49 AM
  • Hi,

    The same is true for external applications, you can try it out.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Noup, isn't true for external apps. How can i get the .net properties of a textbox from application2 as shown in the picture #2 i attached?. Aplication2 it's an EXE program, but i need to receive the controls from the application2. in application1 i just make the calls to application2. application2 its running. i cant debug it, remember that its an EXE program. not a VB Project, but it was built/Compile in .NET

    Yordy Corrales

    I can get the controls handles and names with UIAutomation, BUT, i need to catch when the form display an error (ErrorProvider), and get the message of it. With UIAutomation the errorprovider doest display. and doesn exist a pattern to get it. But maybe if i can get the form, and view its controls i can get all those properties. That it's the main problem. How can i detect an error during automation with UIAutomation

    Yordy Corrales

    Friday, March 8, 2019 5:55 AM
  • UIAutomation?

    What are you talking about?

    Friday, March 8, 2019 6:35 AM
  • UIAutomation?

    What are you talking about?

    Uiautomation it's a Microsoft .net library to automate another application

    Yordy Corrales

    Friday, March 8, 2019 6:39 AM
  • Maybe, it will help or similar articles, but you may not find anything in VB.NET.

    https://blog.adamfurmanek.pl/2016/04/16/dll-injection-part-4/

    Friday, March 8, 2019 8:06 AM