VB.NET(VS2005) 使用API的尴尬 (垃圾回收器 FUCK API)
2008-05-09 16:52
537 查看
我们知道微软不提倡在VS2005里使用API,甚至取消了以前 VB里的 API文本工具,,
最近由于一个项目的需要,,需要用到一个API : GetLastInputInfo (聪明的你肯定已经知道我要做什么了:)或许还有其他方法,但做人总要追求完美吧~)
正常的代码是: Private Declare Function GetLastInputInfo Lib "user32" (ByVal plii As LASTINPUTINFO) As Long ‘声明
Public Structure LASTINPUTINFO
Dim cbSize As Int32
Dim dwTime As Int32
End Structure
'使用。。API。
Dim lastInputInfo As LASTINPUTINFO
lastInputInfo.cbSize = Len(lastInputInfo)
Try
Dim lostTime As String = ""
If GetLastInputInfo(lastInputInfo) <> 0 Then
lostTime &= thObject2.Target.dwTime
MsgBox(lostTime)
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
很不幸VS给了我一个内存写权限的错误。(Attempted to read or write protected memory. This is often an indication that other memory is corrupt) 分析,,VS垃圾回收机制惹得祸。。用VC6.0重写了一遍代码,,一切OK。。再一次 FUCK VB.NET2005
换代码如下: Private Declare Function GetLastInputInfo Lib "user32" (ByVal plii As IntPtr) As Long ’重新定义为 IntPtr
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _ ‘加了这句心里安心点 Public Structure LASTINPUTINFO
Dim cbSize As Int32
Dim dwTime As Int32
End Structure
Private Sub aa()
Dim lastInputInfo As LASTINPUTINFO
lastInputInfo.cbSize = Len(lastInputInfo)
Dim thObject2 As Runtime.InteropServices.GCHandle = Runtime.InteropServices.GCHandle.Alloc(lastInputInfo, Runtime.InteropServices.GCHandleType.Pinned) Dim tpObject2 As IntPtr = thObject2.AddrOfPinnedObject() '取得指向结构的指针 Try
Dim lostTime As String = ""
Dim ret As Long = GetLastInputInfo(tpObject2)
If ret <> 0 Then
lostTime &= thObject2.Target.dwTime
MsgBox(lostTime)
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'在使用完毕后一定要释放指针指向的内存块,让垃圾回收器可对这个内存块回收处理 If thObject2.IsAllocated Then
thObject2.Free()
End If
end sub
添加一个按钮,点击一下,OK ,,收工~
小记: 微软为什么不推荐我们在.NET中使用API呢?
个人认为使用API很多时候要涉及到对内存的操作,需要人工对内存进行管理,然而.NET这个自大的家伙不喜欢你对内存指手画脚,于是乎,,
最近由于一个项目的需要,,需要用到一个API : GetLastInputInfo (聪明的你肯定已经知道我要做什么了:)或许还有其他方法,但做人总要追求完美吧~)
正常的代码是: Private Declare Function GetLastInputInfo Lib "user32" (ByVal plii As LASTINPUTINFO) As Long ‘声明
Public Structure LASTINPUTINFO
Dim cbSize As Int32
Dim dwTime As Int32
End Structure
'使用。。API。
Dim lastInputInfo As LASTINPUTINFO
lastInputInfo.cbSize = Len(lastInputInfo)
Try
Dim lostTime As String = ""
If GetLastInputInfo(lastInputInfo) <> 0 Then
lostTime &= thObject2.Target.dwTime
MsgBox(lostTime)
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
很不幸VS给了我一个内存写权限的错误。(Attempted to read or write protected memory. This is often an indication that other memory is corrupt) 分析,,VS垃圾回收机制惹得祸。。用VC6.0重写了一遍代码,,一切OK。。再一次 FUCK VB.NET2005
换代码如下: Private Declare Function GetLastInputInfo Lib "user32" (ByVal plii As IntPtr) As Long ’重新定义为 IntPtr
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _ ‘加了这句心里安心点 Public Structure LASTINPUTINFO
Dim cbSize As Int32
Dim dwTime As Int32
End Structure
Private Sub aa()
Dim lastInputInfo As LASTINPUTINFO
lastInputInfo.cbSize = Len(lastInputInfo)
Dim thObject2 As Runtime.InteropServices.GCHandle = Runtime.InteropServices.GCHandle.Alloc(lastInputInfo, Runtime.InteropServices.GCHandleType.Pinned) Dim tpObject2 As IntPtr = thObject2.AddrOfPinnedObject() '取得指向结构的指针 Try
Dim lostTime As String = ""
Dim ret As Long = GetLastInputInfo(tpObject2)
If ret <> 0 Then
lostTime &= thObject2.Target.dwTime
MsgBox(lostTime)
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'在使用完毕后一定要释放指针指向的内存块,让垃圾回收器可对这个内存块回收处理 If thObject2.IsAllocated Then
thObject2.Free()
End If
end sub
添加一个按钮,点击一下,OK ,,收工~
小记: 微软为什么不推荐我们在.NET中使用API呢?
个人认为使用API很多时候要涉及到对内存的操作,需要人工对内存进行管理,然而.NET这个自大的家伙不喜欢你对内存指手画脚,于是乎,,
相关文章推荐
- 用vb.net读取INI配置文件的方法,使用API
- 如何使用VS2005的VB.net开发WinCE应用程序
- VB.NET使用API技巧讲解
- VB.NET从VS2003升到VS2005后报表无法使用的解决方法
- 使用VB.NET开发定制控件
- VB.NET在线升级程序源代码,可以独立使用
- VB.net 中Module的使用1
- 在VB.NET中使用MS Access存储过程 — 第二部份
- Asp.net Core WebApi 使用Swagger做帮助文档,并且自定义Swagger的UI
- 使用VB.NET开发复合控件
- VB.Net中使用LDAP验证基于微软AD的登录用户
- vb.net中使用wmi关闭远程计算机上的进程
- vb.NET 关于进度条控件使用
- vb.net 使用code128 代码
- .net 垃圾回收学习[http://www.codeproject.com/KB/dotnet/idisposable.aspx][翻译&&学习][2]
- 【Vegas原创】VB.NET版的GridView经典使用(编辑,删除,分页,链接列)
- VB使用API实现串口通讯的异常问题
- VB.NET中treeview使用方法一
- vb.net中使用GetPrivateProfileString访问INI文件,解决中文路径问题
- VB.NET 使用 OleDb 操作 Access 数据库(来自 MSDN)