Link to home
Start Free TrialLog in
Avatar of Corrup7ioN
Corrup7ioNFlag for United Kingdom of Great Britain and Northern Ireland

asked on

QueryPerformanceFreqency - Memory access error

Hi,

I've been clinging on to VB6 for way too long and decided to move on to VB.NET 2008. I know practically nothing about .NET, but I do know that it has lots of new fangled memory protecty goodness. Unfortunately this seems to be preventing me from using QueryPerformanceFrequency and I have no idea how to get around it short of reading lots of 'Intro to .NET' articles (which I don't have time for at the minute).

I get the following error when calling QueryPerformanceFrequency:

System.AccessViolationException was unhandled
  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I've created an empty project and put in the basic code to execute QueryPerformanceFreqency on a form; could someone please fix the code and briefly explain what had to be done (and why) to get it working.

Many Thanks
Public Class Form1
 
    Public Declare Function QueryPerformanceFrequency Lib "kernel32.dll" Alias "QueryPerformanceFrequency" (ByVal lpFrequency As Int64) As Long
    Public Declare Function QueryPerformanceCounter Lib "kernel32.dll" Alias "QueryPerformanceCounter" (ByVal lpPerformanceCount As Int64) As Long
 
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim frequency As Int64
 
        QueryPerformanceFrequency(frequency)
 
    End Sub
 
End Class

Open in new window

Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

I see some example where people are not declaring these functions with the same types:

Probably your variable are too big so your app is hitting a location that is not available.
I've tried with what I suggest, it works.
Public Declare Auto Function QueryPerformanceCounter _
Lib "kernel32.dll" (ByRef Counter As Long) As Integer
 
Public Declare Auto Function QueryPerformanceFrequency _
Lib "kernel32.dll" (ByRef counter As Long) As Integer

Open in new window

OK the problem was the returned type. Those are also working and I think the 2nd is best suited:
    Public Declare Auto Function QueryPerformanceFrequency _
    Lib "kernel32.dll" (ByRef counter As Int64) As Integer
    
    Public Declare Auto Function QueryPerformanceFrequency _
    Lib "kernel32.dll" (ByRef counter As Int64) As Boolean

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Corrup7ioN

ASKER

Good spot. I copied the declarations from a colleagues old VB6 program which didn't specify ByRef/Val. It turns out that VB.NET 2008 automatically inserts ByVal if nothing is specified.
D'oh

Thanks :P