【问题标题】:Explicit (runtime) DLL linking with Visual Basic 2013 Fails与 Visual Basic 2013 的显式(运行时)DLL 链接失败
【发布时间】:2015-11-17 16:21:12
【问题描述】:

我在 Visual Studio 2013 上用 Visual Basic 编写了一个 dll(它的作用现在无关紧要):

Namespace TextFiles

    Public Class ConfigTextFiles
        'Library of functions to handle configuration text files holding pairs of parm-name, param-value.
        'Param-name and Param-value are separated by the Delimiter property, or "=" if not set.

        Shared strDelimiter As String
        Shared intRowsCount As Integer


        Public Shared Function getParamValue(ByVal strFileLocation As String, ByVal strFileName As String, ByVal strParamName As String) As String
            'Return the value of a specific parameter within the text file
            'Return an empty string if param-name is not found
            'Return vbNullChar if file was not found

            Dim TextLine() As String
            Dim strFullFileName As String

            If Right(strFileLocation, 1) = "\" Then
                strFullFileName = strFileLocation & strFileName
            Else
                strFullFileName = strFileLocation & "\" & strFileName
            End If

            getParamValue = ""
            If System.IO.File.Exists(strFullFileName) = True Then
                If (IsNothing(strDelimiter)) Then strDelimiter = "="
                Dim objReader As New System.IO.StreamReader(strFullFileName)

                Do While objReader.Peek() <> -1
                    TextLine = Split(objReader.ReadLine(), strDelimiter)
                    If (Trim(strParamName) = Trim(TextLine(0))) Then
                        getParamValue = Trim(TextLine(1))
                        Exit Do
                    End If
                Loop
                objReader = Nothing
            Else
                MsgBox("File Does Not Exist: " & strFullFileName, MsgBoxStyle.OkOnly, "File Open Error")
                getParamValue = vbNullChar

            End If
        End Function
    End Class
End Namespace

这个DLL被称为:SisConfigTextFiles.dll,存放在执行(调用)(exe)文件的文件夹中。

我想在运行时从另一个 Visual Basic 应用程序显式链接到它:

Public Class Initializing
    Public Const CONFIG_FILE_NAME As String = "AppConfig.ini" 

    Public Shared Function getConfigParam(ParamName As String) As String
        'Load external dll
        Dim asm As Assembly = Assembly.Load("SisConfigTextFiles")
        Dim type As Type = asm.GetType("TextFiles.ConfigTextFiles")

        ' Create an instance of a Type by calling Activator.CreateInstance
        Dim dynamicObject As Object = Activator.CreateInstance(type)

        Dim appPath As String = My.Application.Info.DirectoryPath

        getConfigParam = ""
        Dim returnValue = DirectCast(type.InvokeMember("getParamValue", _
                                               BindingFlags.InvokeMethod Or BindingFlags.Static, _
                                               Nothing, dynamicObject, {appPath, CONFIG_FILE_NAME, ParamName}), String)
        getConfigParam = returnValue
    End Function

End Class

但是,我不断收到此运行时错误:

System.ArgumentNullException:值不能为空。 参数名称:类型

为什么“类型”为空?我该如何解决这个问题?

谢谢!

【问题讨论】:

  • LoadLibrary,可能在VB中可用,因为它是windows核心功能。
  • 谢谢山姆。是的,它看起来像一个副本。但是,我无法让它工作。我现在将这个问题更新为更具体。
  • @Sam,您能否取消将此问题标记为已回答,现在情况已不再如此。谢谢。

标签: vb.net dll runtime


【解决方案1】:

你快到了,但这条线无效:

asm.GetType("TextFiles.ConfigTextFiles")

应该是

asm.GetType("SisConfigTextFiles.TextFiles.ConfigTextFiles")

您可以在Assembly.ExportedTypes 属性中查看可用类型。

【讨论】:

  • 谢谢,山姆。这给了我以下错误:System.MissingMethodException:找不到方法'SisConfigTextFiles.TextFiles.ConfigTextFiles.getParamValue'。我无法让“ExportedTypes”工作。你能说明如何在这种情况下使用吗?
  • 没问题。抱歉没有完整测试代码!此错误是由于 InvokeMember 调用上的绑定标志造成的。添加“BindingFlags.Public”。
  • 如果您都已排序,请您将答案标记为正确吗?谢谢!
【解决方案2】:

按照 Sam 的建议,下面是调用具有后期(运行时)绑定的 DLL 中的方法的完整工作代码:

Imports System.Reflection

Public Class Initializing
    Public Const CONFIG_FILE_NAME As String = "AppConfig.ini"

    Public Shared Sub Config_Init()

    End Sub


    Public Shared Function getConfigParam(ParamName As String) As String
        'Load external dll
        Dim asm As Assembly = Assembly.Load("SisConfigTextFiles")
        Dim type As Type = asm.GetType("SisConfigTextFiles.TextFiles.ConfigTextFiles")


        ' Create an instance of a Type by calling Activator.CreateInstance
        Dim dynamicObject As Object = Activator.CreateInstance(type)

        Dim appPath As String = My.Application.Info.DirectoryPath

        getConfigParam = ""
        'Invoking a function "setParamValue" in the dll
        Dim returnValue = DirectCast(type.InvokeMember("getParamValue", _
                                               BindingFlags.InvokeMethod Or BindingFlags.Static Or BindingFlags.Public, _
                                               Nothing, dynamicObject, {appPath, CONFIG_FILE_NAME, ParamName}), String)
        getConfigParam = returnValue
    End Function

End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多