Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Error code
VB
The error is: Object reference not set to an instance of an object.

in
VB
Me.MainForm = Global.WindowsApplication9.Form1

Imports System.Runtime.InteropServices

Form with the error
VB
Public Class Form1
    <DllImport("kernel32.dll", EntryPoint:="Wow64DisableWow64FsRedirection")> _
    Public Shared Function DisableWow64Redirection(ByRef output As IntPtr) As Boolean

    End Function
    Private TargetProcessHandle As Integer

    Private pfnStartAddr As Integer

    Private pszLibFileRemote As String

    Private TargetBufferSize As Integer


    Public Const PROCESS_VM_READ = &H10

    Public Const TH32CS_SNAPPROCESS = &H2

    Public Const MEM_COMMIT = 4096

    Public Const PAGE_READWRITE = 4

    Public Const PROCESS_CREATE_THREAD = (&H2)

    Public Const PROCESS_VM_OPERATION = (&H8)

    Public Const PROCESS_VM_WRITE = (&H20)

    Dim DLLFileName As String

    Public Declare Function ReadProcessMemory Lib "kernel32" ( _
        ByVal hProcess As Integer, _
        ByVal lpBaseAddress As Integer, _
        ByVal lpBuffer As String, _
       ByVal nSize As Integer, _
        ByRef lpNumberOfBytesWritten As Integer) As Integer



    Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" ( _
        ByVal lpLibFileName As String) As Integer

    Public Declare Function VirtualAllocEx Lib "kernel32" ( _
    ByVal hProcess As Integer, _
        ByVal lpAddress As Integer, _
        ByVal dwSize As Integer, _
        ByVal flAllocationType As Integer, _
        ByVal flProtect As Integer) As Integer


    Public Declare Function WriteProcessMemory Lib "kernel32" ( _
        ByVal hProcess As Integer, _
        ByVal lpBaseAddress As Integer, _
        ByVal lpBuffer As String, _
        ByVal nSize As Integer, _
        ByRef lpNumberOfBytesWritten As Integer) As Integer


        Public Declare Function GetProcAddress Lib "kernel32" ( _
        ByVal hModule As Integer, ByVal lpProcName As String) As Integer


        Private Declare Function GetModuleHandle Lib "Kernel32" Alias "GetModuleHandleA" ( _
        ByVal lpModuleName As String) As Integer

        Public Declare Function CreateRemoteThread Lib "kernel32" ( _
        ByVal hProcess As Integer, _
        ByVal lpThreadAttributes As Integer, _
        ByVal dwStackSize As Integer, _
        ByVal lpStartAddress As Integer, _
        ByVal lpParameter As Integer, _
        ByVal dwCreationFlags As Integer, _
        ByRef lpThreadId As Integer) As Integer

        Public Declare Function OpenProcess Lib "kernel32" ( _
        ByVal dwDesiredAccess As Integer, _
        ByVal bInheritHandle As Integer, _
        ByVal dwProcessId As Integer) As Integer


        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Integer


        Private Declare Function CloseHandle Lib "kernel32" Alias "CloseHandleA" ( _
        ByVal hObject As Integer) As Integer



    Dim ExeName As String = IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)


    Private Sub Inject()

        On Error GoTo 1 ' If error occurs, app will close without any error messages

        Timer1.Stop()

        Dim TargetProcess As Process() = Process.GetProcessesByName(processo)

        TargetProcessHandle = OpenProcess(PROCESS_CREATE_THREAD Or PROCESS_VM_OPERATION Or PROCESS_VM_WRITE, False, TargetProcess(0).Id)

        pszLibFileRemote = OpenFileDialog1.FileName

        pfnStartAddr = GetProcAddress(GetModuleHandle("Kernel32"), "LoadLibraryA")

        TargetBufferSize = 1 + Len(pszLibFileRemote)

        Dim Rtn As Integer

        Dim LoadLibParamAdr As Integer

        LoadLibParamAdr = VirtualAllocEx(TargetProcessHandle, 0, TargetBufferSize, MEM_COMMIT, PAGE_READWRITE)

        Rtn = WriteProcessMemory(TargetProcessHandle, LoadLibParamAdr, pszLibFileRemote, TargetBufferSize, 0)

        CreateRemoteThread(TargetProcessHandle, 0, 0, pfnStartAddr, LoadLibParamAdr, 0, 0)

        CloseHandle(TargetProcessHandle)

1:      Me.Show()

    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        Try
            Dim allProcess As Process
            For Each allProcess In Process.GetProcesses()
                ListBox1.Items.Add(allProcess.ProcessName)
            Next
        Catch exx As Exception
            ListBox1.Items.Add("ERROR")
        End Try
        Timer1.Start()
    End Sub

    Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening

    End Sub

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
        Label4.Visible = False

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.MouseClick
        Label4.Visible = False
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
        Label4.Visible = False
        TextBox1.Text = ListBox1.SelectedItem & ".exe"

    End Sub

    Private Sub ListBox1_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Dim teste As String = ListBox1.SelectedItem


        If Not teste = "" Then
            Button4.Enabled = True
        Else
            Button4.Enabled = False

        End If

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Timer1.Start()



        Dim TargetProcess As Process() = Process.GetProcessesByName(TextBox1.Text)


            Timer1.Stop()

            Me.Label1.Text = "Successfully Injected!"

            Call Inject()

        
    End Sub
    Dim processo As String = TextBox1.Text.Replace(".exe", "") & ".exe"
    Private Sub TextBox1_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox1.TextChanged


        Try

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            DLLFileName = OpenFileDialog1.FileName
            TextBox2.Text = OpenFileDialog1.FileName.ToString
        End If
    End Sub
End Class
Posted
Updated 11-Jan-13 16:29pm
v2
Comments
Sergey Alexandrovich Kryukov 11-Jan-13 22:47pm    
What is your question?
—SA
Kuthuparakkal 11-Jan-13 22:48pm    
Please explain more

1 solution

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Based on the line you say:
VB
Me.MainForm = Global.WindowsApplication9.Form1

I would suggest you to made sure if Global.WindowsApplication9 in not null. Have correct reference wherever you are invoking Forms1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900