VB调用dll内存不可读,谁可以帮帮我啊?有时候不会出现,有时候会出现。

羽熙 2014-09-29 09:41:43
Public Function mySearching(ByVal mode As Long, ByVal search_ip As Long) As Long
Dim i As Long
Dim ret As Long
Dim retcnt As Long

For i = 0 To search_count
If (i < search_count) Then
If (mySearchInfo(i).dshdl >= 0) Then
dsc_Detach (mySearchInfo(i).dshdl)
End If
Exit Function
End If
Next i

searcg_count = 0
Call dsc_SetSearchOpt(SEARCH_TIMEOUT, SEARCH_RETRY_CNT)

If (mode = BROADCAST) Then
If ((ret = dsc_EnumSearch(0, AddressOf myEnumSearchProc)) <> DSC_OK) Then '出错
txtstring = "dsc_EnumSearch() fail, error code: & ret &" & txtstring
'Exit Function
End If

If (search_count <= 0) Then
MsgBox "No Network Enabler found !" & txtstring
txtstring = "-1"
'Exit Function
End If
End If

For i = 0 To search_count
If i < search_count Then
If ((ret = dsc_Attach(mySearchInfo(i).ds_info, VarPtr(mySearchInfo(i).ds_info))) <> DSC_OK) Then ''出错
txtstring = "dsc_Attach() fail, error code: &ret&" & txtstring
' Exit Function
End If
'Get ip address, netmask, gateway
' SearchInfo(i).p_ifconfig = new ifconfiginfo
If ((ret = dsc_GetIfConfig(mySearchInfo(i).dshdl, 1, retcnt, mySearchInfo(i).p_ifconfig)) <> DSC_OK) Then '出错
txtstring = "dsc_GetIfConfig() fail, error code:" & ret & "" & txtstring

'Exit Function
End If
'Get serial interface
If ((ret = dsc_GetSerialInterface(mySearchInfo(i).dshdl, 1, SERINIT, mySearchInfo(i).port_type)) <> DSC_OK) Then ''出错
txtstring = "dsc_GetSerialInterface() fail, error code: & ret&" & txtstring
'Exit Function
End If
'Get serial port baudrate, mode, flowctrl setting
If ((ret = dsc_GetSerialIOCtl(mySearchInfo(i).dshdl, 1, SERINIT, mySearchInfo(i).pserial_info)) <> DSC_OK) Then'出错
txtstring = "dsc_GetSerialIOCtl() fail, error code:" & ret & "" & txtstring

'Exit Function
End If
'Get Server name
If ((ret = dsc_GetName(mySearchInfo(i).dshdl, mySearchInfo(i).svr_name)) <> DSC_OK) Then '出错
txtstring = "dsc_GetName() fail, error code:" & ret & " " & txtstring
'Exit Function
End If
'Get the kernel/firmware information.
If ((ret = dsc_GetKernelInfo(mySearchInfo(i).dshdl, mySearchInfo(i).pkernel_info)) <> DSC_OK) Then'出错
txtstring = "dsc_GetKernelInfo() fail, error code:" & ret & "" & txtstring
'Exit Function
End If
'Get DIO information information.
If ((ret = dsc_GetMaxDIO(mySearchInfo(i).dshdl, mySearchInfo(i).DIO_max)) <> DSC_OK) Then
txtstring = "dsc_GetDIOMode() fail, error code:" & ret & "" & txtstring
'Exit Function
End If
' If ((ret = dsc_GetDIOMode(mySearchInfo(i).dshdl, 0, mySearchInfo(i).DIO_max, mySearchInfo(i).DIO_mode)) <> DSC_OK) Then '
' txtstring = "dsc_GetDIOMode() fail, error code:" & ret & "" & txtstring
' Exit Function
' End If
' If ((ret = dsc_GetDIOState(mySearchInfo(i).dshdl, 0, mySearchInfo(i).DIO_max, mySearchInfo(i).DIO_state)) <> DSC_OK) Then
' txtstring = "dsc_GetDIOState() fail, error code: " & ret & " &txtstring"
' Exit Function
' End If
End If
Next i
End Function
...全文
154 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Carlven2012 2014-10-06
  • 打赏
  • 举报
回复
引用 9 楼 u010302729 的回复:
请问你是怎么解决的呢
for .... next 里面不要用回调函数,或者另想个办法吧。至少要按这个思路走: 内存读写操作要一个一个地进行,即第一个操作执行完毕了,才能再执行第二个,不然内存地址变化了,就极容易出现内存读取错误,或是写入错误。
羽熙 2014-10-06
  • 打赏
  • 举报
回复
引用 7 楼 Carlven2012 的回复:
我发现你的问题所在了: For.....Next里面用了回调函数,而且这函数里面用了memcpy, 百分百要遇到内存读写错误,跑不掉的。 就相当于For....Next里面加了一个DoEvents,当第一个循环还在执行时,第二个、第三个循环就又开始执行起来了,机器忙不过来,就很容易冲突,就挂了。我以前就遇到过这样的问题。
请问你是怎么解决的呢
羽熙 2014-10-06
  • 打赏
  • 举报
回复
引用 6 楼 of123 的回复:
VB 的数据类型与 VC++ 是不同的。函数声明时数据类型错误引起的内存不可读异常的情况是有的。
我已经把VC中的数据类型转化为对应的VB数据类型了
Carlven2012 2014-09-30
  • 打赏
  • 举报
回复
我发现你的问题所在了: For.....Next里面用了回调函数,而且这函数里面用了memcpy, 百分百要遇到内存读写错误,跑不掉的。 就相当于For....Next里面加了一个DoEvents,当第一个循环还在执行时,第二个、第三个循环就又开始执行起来了,机器忙不过来,就很容易冲突,就挂了。我以前就遇到过这样的问题。
of123 2014-09-30
  • 打赏
  • 举报
回复
VB 的数据类型与 VC++ 是不同的。函数声明时数据类型错误引起的内存不可读异常的情况是有的。
羽熙 2014-09-30
  • 打赏
  • 举报
回复
@caozhydll函数原型是正确的,因为在vc6里面已经可以运行
羽熙 2014-09-30
  • 打赏
  • 举报
回复
@Carlven2012这个问题在调试的时候也会出现,有函数第一次运行没有出现,但是运行一次之后又会出现,有的函数就会一直出现,我不知道我的回调函数是不是多线程的,麻烦你帮我看看。回到函数是用vc6写的 回调函数: int CALLBACK EnumSearchProc(PDS_INFO dsinfo) { if (dsinfo == NULL) return true; SearchInfo[search_count].dshdl = -1; if (dsinfo->hwid == NE4100T) { printf("Found: NE4100T\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; }else if (dsinfo->hwid == NE4110S) { printf("Found: NE4110S\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; }else if (dsinfo->hwid == NE4110A) { printf("Found: NE4110A\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; }else if (dsinfo->hwid == NE4120S) { printf("Found: NE4120S\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; }else if (dsinfo->hwid == NE4120A) { printf("Found: NE4120A\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; }else if (dsinfo->hwid == WE2100T_US) { printf("Found: WE2100T_US\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; }else if (dsinfo->hwid == WE2100T_EU) { printf("Found: WE2100T_EU\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; }else if (dsinfo->hwid == WE2100T_JP) { printf("Found: WE2100T_JP\n"); memcpy(&SearchInfo[search_count].ds_info, dsinfo, sizeof(DS_INFO)); search_count++; } return true; }
threenewbee 2014-09-29
  • 打赏
  • 举报
回复
检查下dll函数原型是否正确。
Carlven2012 2014-09-29
  • 打赏
  • 举报
回复
调试时也会出现这个问题吗? 你那回调函数是多线程的?这样搞容易挂得很哦。
羽熙 2014-09-29
  • 打赏
  • 举报
回复
大师都去休假了吗?

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧