VB.net使用从dll非托管管理的回调

时间:2013-11-20 14:07:14

标签: vb.net memory-management callback dllimport

我想使用dll中的函数:

<DllImport("netXTransport.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto, ExactSpelling:=True)> _
 Public Shared Function xSysdeviceUpload(ByVal hSysdevice As IntPtr, ByVal ulChannel As UInt32, ByVal ulMode As UInt32, ByVal pszFileName As IntPtr, ByVal pabFileData As IntPtr, ByVal pulFileSize As IntPtr, ByVal pfnCallback As PFN_NXAPI_PROGRESS_CALLBACK, ByVal pfnRecvPktCallback As PFN_NXAPI_BROWSE_CALLBACK, ByVal pvUser As IntPtr) As Int32
 End Function

 Public Delegate Sub PFN_NXAPI_PROGRESS_CALLBACK(ByVal ulStep As UInt32, ByVal ulMaxStep As UInt32, ByVal pvUser As IntPtr, ByVal bFinished As Char, ByVal lError As Int32)

(有关C接口的信息:

int32_t APIENTRY xSysdeviceUpload(CIFXHANDLE  hSysdevice, uint32_t ulChannel, uint32_t ulMode, char* pszFileName, uint8_t* pabFileData, uint32_t* pulFileSize, PFN_PROGRESS_CALLBACK pfnCallback, PFN_RECV_PKT_CALLBACK pfnRecvPktCallback, void* pvUser)`;)

我的pfnCallback函数在托管内存(vb.net)中完成:

<AllowReversePInvokeCalls()>
Public Shared Sub TCPRenderProgressBar(ByVal ulStep As UInt32, ByVal ulMaxStep As UInt32, ByVal pvUser As IntPtr, ByVal bFinished As Char, ByVal lError As Int32)
End Sub

因此我在我的代码中使用了函数:

Dim pFile As IntPtr = Marshal.AllocHGlobal(CInt(FileLength))
Dim pFileLength As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(FileLength))
Dim pszFileName As IntPtr = Marshal.AllocHGlobal(szFileName.Length)
Dim Temp() As Byte = g_encoding.GetBytes(szFileName & " ")
Temp(szFileName.Length) = 0
Marshal.Copy(Temp, 0, pszFileName, (szFileName.Length + 1))

iResult = xSysdeviceUpload(g_hSysdevice, 0, DOWNLOAD_MODE_FILE, pszFileName, pFile, pFileLength, AddressOf TCPRenderProgressBar, Nothing, Nothing)

如果没有声明回调(AddressOf TCPRenderProgressBar被任何东西替换),则所有运行良好,但如果我声明(AddressOf) TCPRenderProgressBar,我看到我传入函数TCPRenderProgressBar但我错误{{ 1}}之后...

有什么问题?

感谢您的帮助

0 个答案:

没有答案