?
Solved

QueryPerformanceFreqency - Memory access error

Posted on 2009-02-11
5
Medium Priority
?
325 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:Corrup7ioN
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 23610685
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

0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 23610739
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

0
 
LVL 13

Accepted Solution

by:
kaylanreilor earned 2000 total points
ID: 23610784
OK, sorry a thousand times, you were passing it ByVal and needed to pass it ByRef !!!
Of course this is an out parameter.
0
 
LVL 3

Author Comment

by:Corrup7ioN
ID: 23611541
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.
0
 
LVL 3

Author Closing Comment

by:Corrup7ioN
ID: 31545512
D'oh

Thanks :P
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question