VB.NET(VS2005) 使用API的尴尬 (垃圾回收器 FUCK API)
来源:互联网 发布:淘宝天机平台论坛 编辑:程序博客网 时间:2024/05/21 09:59
我们知道微软不提倡在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这个自大的家伙不喜欢你对内存指手画脚,于是乎,,
- VB.NET(VS2005) 使用API的尴尬 (垃圾回收器 FUCK API)
- VB使用API的例子
- 与垃圾回收相关的新API:System.pauseForGCIfCollectionImminent()
- 关于垃圾回收API的中文解说:System.pauseForGCIfCollectionImminent()
- 与垃圾回收相关的新API:System.pauseForGCIfCollectionImminent()
- VB.NET使用API技巧讲解
- vb.net 使用windows API方法
- Vb.net声明API
- .net的垃圾回收
- VB.NET 使用WIN32 API函数,VB.NET使用Windows API函数
- VB 6.0使用api
- API 响应按键事件例子,阻止内存回收,垃圾回收
- VB中一些API的使用
- 使用垃圾回收器
- 使用垃圾回收器
- VB.net 注册表操作API
- .NET的垃圾回收机制
- .NET的垃圾回收机制
- MySQL 备份和恢复
- tomcat 出现 OutOfMemoryError : PermGen space
- 利用DEEP Table做层次XML输出(原文转自网络)
- 第一遍自学C#过程中的一些疑惑
- html读取xml数据实例一
- VB.NET(VS2005) 使用API的尴尬 (垃圾回收器 FUCK API)
- sipX简介
- C#——Marshal.StructureToPtr方法简介
- Android Say Hello 3
- PHP入门笔试题
- JDBC操作DB2 Clob、Blob字段的Bug探究
- 用java打造任意形状窗口和透明窗口
- IMP 数据库版本不对引起的错误
- 正则表达示收藏