QueryPerformanceFreqency - Memory access error

Posted on 2009-02-11
Last Modified: 2012-06-22

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


    End Sub

End Class

Open in new window

Question by:Corrup7ioN
    LVL 13

    Expert Comment

    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

    LVL 13

    Expert Comment

    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

    LVL 13

    Accepted Solution

    OK, sorry a thousand times, you were passing it ByVal and needed to pass it ByRef !!!
    Of course this is an out parameter.
    LVL 3

    Author Comment

    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.
    LVL 3

    Author Closing Comment


    Thanks :P

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now