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 6342131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:10:45+00:00 2026-05-24T20:10:45+00:00

I am trying to invoke the CreateFile method from Kernel32.dll in a C# application,

  • 0

I am trying to invoke the CreateFile method from Kernel32.dll in a C# application, and it is returning an invalid handle everytime. I’ve looked all over and can’t find a solution to this problem. Here is the relevant code:

Constants:

    const UInt64 GENERIC_READ = 0x80000000L;
    const UInt64 GENERIC_WRITE = 0x40000000L;
    const UInt64 GENERIC_EXECUTE = 0x20000000L;
    const UInt64 GENERIC_ALL = 0x10000000L;
    const uint FILE_SHARE_READ = 0x00000001; 
    const uint FILE_SHARE_WRITE = 0x00000002;
    const uint OPEN_EXISTING = 0x00000003;
    const uint FILE_FLAG_DELETE_ON_CLOSE = 0x04000000;

Importing the Method:

[DllImport("kernel32.dll", SetLastError = true)]
    static extern unsafe SafeFileHandle CreateFile(
          string FileName,                   
          ulong DesiredAccess,                
          uint ShareMode,                    
          IntPtr SecurityAttributes,            
          uint CreationDisposition,           
          uint FlagsAndAttributes,            
          IntPtr hTemplateFile                   
          );
SafeFileHandle JFifoDrv = LoadDriver();

Method to Load Driver:

unsafe private SafeFileHandle LoadDriver()
    {
        SafeFileHandle hDrv = (SafeFileHandle)null;
        try
        {
            hDrv = CreateFile("\\\\.\\JFIFODRV",
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                (IntPtr)null,
                OPEN_EXISTING,
                FILE_FLAG_DELETE_ON_CLOSE,
                (IntPtr)null);

            DriverLoaded = true;
        }
        catch
        {
            poll.Stop();
            DriverLoaded = false;
        }

        return hDrv;
    }

Can Anyone help me figure out the problem?

  • 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-24T20:10:45+00:00Added an answer on May 24, 2026 at 8:10 pm

    Your declaration is wrong, the 2nd argument is an uint, not an ulong. The pinvoke.net website is a decent source for declarations. Be sure to throw Win32Exception when JFifoDrv.IsInvalid is true.

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

Sidebar

Related Questions

When trying to invoke a method on an external webservice (over the Internet) it
How can I invoke a service directly from a view? I'm trying with ${my.domain.service.method}
I am trying to invoke an internal method from a dynamically generated one. The
I'm currently trying to invoke a method made in C from C# C code
Im trying to invoke a method in java from javascript, but this doesn't happen
I'm trying to invoke another application (Beyond Compare) from Powershell which requires an @
I'm trying to invoke a webservice from a console application, and I need to
I'm trying to invoke a method that takes a super class as a parameter
I am trying to invoke an ANT target from Windows (right-click) file context menu.
I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){

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.