Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Net DLLImport_Vb.net_Pinvoke_Dllimport - Fatal编程技术网

Vb.net Net DLLImport

Vb.net Net DLLImport,vb.net,pinvoke,dllimport,Vb.net,Pinvoke,Dllimport,我得到了一个PL/I Dll,我正试图将该Dll导入到我的VB.Net应用程序中。 第一次尝试成功了,但在几次通话后,程序毫无评论地退出了。 从两个不同的线程调用这些函数两次是不可能的。 我从paramone中的DLL获得结果。你知道为什么这样做不正确,以及如何让它工作吗 <DllImport(("PLIDLL.dll"), CallingConvention:=CallingConvention.StdCall)> _ Public Shared Sub MYFUNC(By

我得到了一个PL/I Dll,我正试图将该Dll导入到我的VB.Net应用程序中。 第一次尝试成功了,但在几次通话后,程序毫无评论地退出了。 从两个不同的线程调用这些函数两次是不可能的。 我从paramone中的DLL获得结果。你知道为什么这样做不正确,以及如何让它工作吗

<DllImport(("PLIDLL.dll"), CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Sub MYFUNC(ByVal LogonString As String, _
                              <MarshalAs(UnmanagedType.VBByRefStr)> ByRef paramone As String, _
                              ByVal paramtwo As String)
    End Sub

至少找到了一个有效的解决方案

Module NativeMethods

<DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function LoadLibrary(ByVal lpFileName As String) As IntPtr
    End Function

<DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function GetProcAddress(ByVal hModule As IntPtr, ByVal procedureName As String) As IntPtr
    End Function

<DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function FreeLibrary(ByVal hModule As IntPtr) As IntPtr
    End Function

End Module


<UnmanagedFunctionPointer(CallingConvention.StdCall)>
    Private Delegate Sub MYFUNC(ByVal LogonString As String, <MarshalAs(UnmanagedType.VBByRefStr)> ByRef PARAMONE As String, ByVal PARAMTWO As String)

Public Shared Sub CallFunction(ByRef workingObject As PLIOBJECT)
    Dim EntryPointer As IntPtr = NativeMethods.LoadLibrary("PLIDLL.dll")
    Dim FunctionPointer As IntPtr = NativeMethods.GetProcAddress(EntryPointer, "MYFUNC")
    Dim MyFUNC As MYFUNC= CType(Marshal.GetDelegateForFunctionPointer(FunctionPointer, GetType(MYFUNC)), MYFUNC)
    MyFUNC(workingObject.Logonstring, workingObject.PARAMONE, workingObject.PARAMTWO)
    Dim result As Boolean = NativeMethods.FreeLibrary(EntryPointer)

End Sub
模块本地方法
_
公共函数LoadLibrary(ByVal lpFileName作为字符串)作为IntPtr
端函数
_
公共函数GetProcAddress(ByVal hModule作为IntPtr,ByVal procedureName作为String)作为IntPtr
端函数
_
公共函数自由库(ByVal hModule作为IntPtr)作为IntPtr
端函数
端模块
私有委托子MYFUNC(ByVal LogonString作为字符串,ByRef PARAMONE作为字符串,ByVal PARAMTWO作为字符串)
公共共享子调用函数(ByRef workingObject作为PLIOBJECT)
Dim EntryPointer作为IntPtr=NativeMethods.LoadLibrary(“PLIDLL.dll”)
Dim FunctionPointer作为IntPtr=NativeMethods.GetProcAddress(EntryPointer,“MYFUNC”)
将MyFUNC设置为MyFUNC=CType(Marshal.GetDelegateForFunctionPointer(FunctionPointer,GetType(MyFUNC)),MyFUNC)
MyFUNC(workingObject.Logonstring、workingObject.PARAMONE、workingObject.paramtoo)
Dim结果为Boolean=NativeMethods.FreeLibrary(EntryPointer)
端接头

解决了它被调用两次的问题。尽管如此,程序还是会在几次调用后退出。在您显示界面的另一面之前,我们无法帮助您。很明显,你的界面是错误的。我从来没有见过和ByRef字符串匹配的东西。但是,如果我们不知道界面的另一端是什么,我们就无法告诉您什么是正确的。我们如何评论?你只告诉了我们故事的一半。答案中的代码与问题中的代码相同。除了每次调用时都会加载和卸载库之外。这会隐藏错误,而不是修复错误。而且非常浪费。如果你按照我的要求做,并显示界面的另一面,那么我们可以提供帮助。@DavidHeffernan对不起,我不知道界面的另一面是什么?我知道这几乎是一样的,它确实浪费了资源。DLL在VB6中运行了多年,没有任何问题。程序本身只包含DLL调用。所以请告诉我我应该发布什么,或者你说“另一边”是什么意思。这是一个界面。接口有两个方面。您显示了调用端VB.net。另一面是PLI DLL。如果您显示工作的VB6代码,那么我们可以从中推断DLL的另一面。就我个人而言,我觉得你应该删除这个答案,并对问题进行澄清。现在我更困惑了。我已经在问题中张贴了PLI子标题。我不能发布完全不可能的PLI函数。我没有写VB6代码,它只需要在VB.Net中工作,直到我们有时间在VB.Net中重写整个过程@戴维谢弗南
Module NativeMethods

<DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function LoadLibrary(ByVal lpFileName As String) As IntPtr
    End Function

<DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function GetProcAddress(ByVal hModule As IntPtr, ByVal procedureName As String) As IntPtr
    End Function

<DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function FreeLibrary(ByVal hModule As IntPtr) As IntPtr
    End Function

End Module


<UnmanagedFunctionPointer(CallingConvention.StdCall)>
    Private Delegate Sub MYFUNC(ByVal LogonString As String, <MarshalAs(UnmanagedType.VBByRefStr)> ByRef PARAMONE As String, ByVal PARAMTWO As String)

Public Shared Sub CallFunction(ByRef workingObject As PLIOBJECT)
    Dim EntryPointer As IntPtr = NativeMethods.LoadLibrary("PLIDLL.dll")
    Dim FunctionPointer As IntPtr = NativeMethods.GetProcAddress(EntryPointer, "MYFUNC")
    Dim MyFUNC As MYFUNC= CType(Marshal.GetDelegateForFunctionPointer(FunctionPointer, GetType(MYFUNC)), MYFUNC)
    MyFUNC(workingObject.Logonstring, workingObject.PARAMONE, workingObject.PARAMTWO)
    Dim result As Boolean = NativeMethods.FreeLibrary(EntryPointer)

End Sub