微软.net精简框架常见问题及回答(中文版)(19)

王朝c#·作者佚名  2006-01-09
宽屏版  字体: |||超大  

Me.Text = "FindMe"

Dim deskWin As IntPtr = FindWindow(Nothing, "FindMe")

Me.Capture = True

Dim hwnd As IntPtr = GetCapture()

Me.Capture = False

6.9. 如何使用性能计数器功能?

使用QueryPerformanceFrequency函数和QueryPerformanceCounter函数可以建立精确的计时程序。 这些功能是和设备提供商相关的,如果他们不能执行,那么只能和GetTickCount功能得到一样的结果。如果能执行这些函数,就能保证计时器最准确的运行,比GetTickCounter或Environment.TickCount准确得多。TickCount其实是调用GetTickCounter的。

如果性能计数器是GetTickCount的一个实例,QueryPerformanceFrequency将把1000作为计时频率。如果这些函数不能执行,将得到返回值为0。以下代码演示了如何使用这些函数。//C#

[DllImport("CoreDll.dll")]

public static extern int QueryPerformanceFrequency(ref Int64 lpFrequency);

[DllImport("CoreDll.dll")]

public static extern int QueryPerformanceCounter(ref Int64 lpPerformanceCount);

private void TestTimer()

{

System.Int64 freq = 0;

if (QueryPerformanceFrequency(ref freq) != 0)

{

System.Int64 count1 = 0;

System.Int64 count2 = 0;

if (QueryPerformanceCounter(ref count1) != 0)

{

System.Threading.Thread.Sleep(1200);

QueryPerformanceCounter(ref count2);

System.Int64 time_ms = (count2 - count1) * 1000 / freq;

}

}

}

'VB

<DllImport("CoreDll.dll")> _

Public Shared Function QueryPerformanceFrequency(ByRef lpFrequency As Int64) As Integer

End Function

<DllImport("coredll.dll")> _

Public Shared Function QueryPerformanceCounter(ByRef lpPerformanceCount As Int64) As Integer

End Function

Private Sub TestTimer()

Dim freq As System.Int64 = 0

If QueryPerformanceFrequency(freq) <> 0 Then

Dim count1 As System.Int64 = 0

Dim count2 As System.Int64 = 0

If QueryPerformanceCounter(count1) <> 0 Then

System.Threading.Thread.Sleep(1200)

QueryPerformanceCounter(count2)

Dim time_ms As System.Int64 = (count2 - count1) * 1000 / freq

End If

End If

End Sub 'TestTimer

6.10. 调用本地代码时,数据类型有什么限制?What are the limitations on marshalling types via P/Invoke?

返回值

只能是长度小于等于32位的类型

非浮点型not floating point

参数

Only support marshaling blittable types

blittable types -> same representation in memory in both managed and native

non-blittable -> memory transformation required

Since only blittable types, all objects are pinned and never copied

Exception: passing String ByVal in VB.NET

Implies that you can't marshal nested objects since this requires a memory transformation (non-blittable)

只能是长度小于等于32位的类型

值通过堆栈传递

例外:float32

参考(References)

Pass blittable reference types

把参考传递到值类型变量

这就是如何传递float32类型的值

可以传递值类型的数组

在本地代码中,您可以使用指针指向第一个对象,然后一个接一个地访问其他对象

String是特殊的,传递char数组 -> 不变的

StringBuilder是特殊的,传递char数组 -> 易变的 (需要单独传递长度)

注意:C# bool是8个比特位的,并且不等于Win32的BOOL

队列:编译器默认的队列 (4字节)

Marshal.GetLastWin32Error 支持 GetLastError() 语义

第一页    上一页    第19页/共78页    下一页    最后页
第01页 第02页 第03页 第04页 第05页 第06页 第07页 第08页 第09页 第10页 
第11页 第12页 第13页 第14页 第15页 第16页 第17页 第18页 第19页 第20页 
第21页 第22页 第23页 第24页 第25页 第26页 第27页 第28页 第29页 第30页 
第31页 第32页 第33页 第34页 第35页 第36页 第37页 第38页 第39页 第40页 
第41页 第42页 第43页 第44页 第45页 第46页 第47页 第48页 第49页 第50页 
第51页 第52页 第53页 第54页 第55页 第56页 第57页 第58页 第59页 第60页 
第61页 第62页 第63页 第64页 第65页 第66页 第67页 第68页 第69页 第70页 
第71页 第72页 第73页 第74页 第75页 第76页 第77页 第78页 
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有