We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

QueryPerformanceFreqency - Memory access error

Medium Priority
340 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

Comment
Watch Question

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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.

Author

Commented:
D'oh

Thanks :P
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.