Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:10:55+00:00 2026-05-27T22:10:55+00:00

The task is to determine the last write time for the registry key. As

  • 0

The task is to determine the last write time for the registry key. As standard RegistryKey class doesn’t provide this feature, I have to use WinAPI function “RegQueryInfoKey”. To get the key handle I open it by “RegOpenKeyEx”.

This is the WinAPI prototype of the function (taken from MSDN):

LONG WINAPI RegOpenKeyEx(
  __in          HKEY hKey,
  __in          LPCTSTR lpSubKey,
                DWORD ulOptions,
  __in          REGSAM samDesired,
  __out         PHKEY phkResult
);

I use the following declaration:

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int RegOpenKeyEx(UIntPtr hkey, string lpSubKey, uint samDesired, ref UIntPtr phkResult);

Then I call it in the following way:

UIntPtr hKey = UIntPtr.Zero;
string myKeyName = "blablabla";
UIntPtr HKEY_USERS = (UIntPtr)0x80000003; 
uint KEY_READ = 0x20019;
RegOpenKeyEx(HKEY_USERS, myKeyName, KEY_READ, ref hKey);

At this point I get “Access violation” exception. What am I doing wrong?
I think something is wrong with parameters passing, but how to do it right?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:10:55+00:00Added an answer on May 27, 2026 at 10:10 pm

    There are 5 parameters in the native function’s prototype, and only 4 in your P/Invoke signature.

    In particular, you’re missing DWORD ulOptions. This parameter is “reserved and must be zero” according to the MSDN documentation, but it must still be passed in the function call.

    Also, you don’t need to set the SetLastError field because the RegOpenKeyEx function returns its error code; you don’t have to retrieve it by calling GetLastError. Consequently, you don’t need the marshaler to save that value for you automatically. Just check the return value for the error code.

    Change your P/Invoke signature to look like this:

    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
    public static extern int RegOpenKeyEx(UIntPtr hkey, string lpSubKey,
                                          uint ulOptions, uint samDesired,
                                          out UIntPtr phkResult);
    

    The wrong P/Invoke signature is almost always the cause of “access violation” errors. When you see one of those, make sure you double-check it twice!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to determine the best time efficient algorithm to accomplish the task
I've read many different articles/threads on this, but have yet to determine the best
I need to write an ant task to determine if a certain file is
This is my first time I need to create a cutscene system. I have
Task at hand — I have three versions of some code, developed by different
TASK : I have an existing xml document (UTF-8) which uses xml namespaces and
Task: I have a camera mounted on the end of our assembly line, which
The task is to take a list of tables which is changeable. Write a
I've currently got a bash script that parses /var/log/mail.log to determine the last login
I would like to have a workflow create a task, then email the assigned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.