Create Instance of class fails on some machine but not others

NachitoMax 411 Reputation points
2022-08-26T05:07:31.727+00:00

Hi

I have a solution that reads a class & function from another dll without needing to reference it. There is a reason i need to do it this way. anyway it works fine and has done for months on my pc however, im finding that it doesnt work on any workstation either at admin or user level. I have narrows it down to a single line of code. Here is the Class-

Imports System.Reflection  
  
Namespace INVOKER  
  
    Public Class C_Invoke  
#Region "MEMBERS"  
        Private _path As String = String.Empty  
        Private _ClassName As String = String.Empty  
        Private _FunctionName As String = String.Empty  
        Private _RegKey As String = String.Empty  
  
#End Region  
  
#Region "PROPERTIES"  
        Friend Property Path As String  
            Set(value As String)  
                _path = value  
            End Set  
            Get  
                Return _path  
            End Get  
        End Property  
  
        Friend Property ClassName As String  
            Set(value As String)  
                _ClassName = value  
            End Set  
            Get  
                Return _ClassName  
            End Get  
        End Property  
  
        Friend Property FunctionName As String  
            Set(value As String)  
                _FunctionName = value  
            End Set  
            Get  
                Return _FunctionName  
            End Get  
        End Property  
  
        Friend Property RegKey As String  
            Set(value As String)  
                _RegKey = value  
            End Set  
            Get  
                Return _RegKey  
            End Get  
        End Property  
#End Region  
  
#Region "CONSTRUCTORS"  
        Friend Sub New(ByVal dll_path As String,  
                        ByVal cls_name As String,  
                        ByVal fn_name As String,  
                        ByVal reg_key As String)  
  
            Path = dll_path  
            ClassName = cls_name  
            FunctionName = fn_name  
            RegKey = reg_key  
  
        End Sub  
#End Region  
  
#Region "METHODS"  
        Public Sub Invoke()  
  
            Dim exeConfigPath As String = Path  
  
            Dim strCommandName As String = ClassName 'name of class with function being called  
            Dim assemblyBytes As Byte() = IO.File.ReadAllBytes(exeConfigPath)  
  
            Dim objAssembly As Assembly = Assembly.Load(assemblyBytes)  
            Dim myIEnumerable As IEnumerable(Of Type) = GetTypeSafely(objAssembly)  
  
            For Each objType As Type In myIEnumerable  
                If objType.IsClass Then  
                    If objType.Name.ToLower() = strCommandName.ToLower() Then  
                        Dim iBaseObject As Object = Activator.CreateInstance(objType)  
                        Dim arguments As Object() = New Object() {RegKey}  
                        Dim result As Object = objType.InvokeMember(FunctionName, BindingFlags.[Default] Or BindingFlags.InvokeMethod, Nothing, iBaseObject, arguments)  
                        MsgBox("4")  
                        Exit For  
                    End If  
                End If  
            Next  
        End Sub  
  
        Private Shared Function GetTypeSafely(ByVal assembly As Assembly) As IEnumerable(Of Type)  
            Try  
                Return assembly.GetTypes()  
            Catch ex As ReflectionTypeLoadException  
                Return ex.Types.Where(Function(x) x IsNot Nothing)  
            End Try  
        End Function  
#End Region  
  
    End Class  
End Namespace  

and this is the line it fails on

Dim result As Object = objType.InvokeMember(FunctionName, BindingFlags.[Default] Or BindingFlags.InvokeMethod, Nothing, iBaseObject, arguments)  

Would user restrictions on a workstation prevent this file reading a class and function from another dll? If so, what user restriction would need to be changed in order for it to work again?

Like i said, it works just fine on my machine in multiple solutions, just not on a network workstation...

Thanks

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,118 questions
{count} votes

2 answers

Sort by: Most helpful
  1. ahsan dime 1 Reputation point
    2022-08-30T06:20:58.27+00:00

    “Object Reference Not Set to an instance of an object.” Cast the first stone those who never struggled with this error message when they were a beginner C#/.NET programmer.

    This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a null reference.

    But what is a null reference? What are “references” in the first place? How can you stop the NullReferenceException from happening in your code? That’s what were going to cover in today’s post.

    We’ll start with fundamentals, by giving a brief explanation of what references are in C#/.NET. After that, you’ll learn what null references are. At this point, you’re halfway there to seeing the whole picture.

    After this round of theoretical definitions, we’ll get to more practical matters, teaching you how to avoid the NullReferenceException in practice. Let’s dig in.

    0 comments No comments

  2. Bruce (SqlWork.com) 55,366 Reputation points
    2022-08-30T15:42:45.68+00:00

    have you checked the the required version of the dll is installed? you can use an IL spy tool to check the contents of the dll.

    0 comments No comments