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

  • Home
  • SEARCH
  • 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 8231309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:26:36+00:00 2026-06-07T17:26:36+00:00

I am trying to use a Windows API .dll called netapi32.dll to manipulate DFS

  • 0

I am trying to use a Windows API .dll called netapi32.dll to manipulate DFS shares. We are using DFS on Windows Server 2008 R2, and I am using .NET 4.0. I’ve never used something like this before in .NET, but I am having trouble understanding what is failing.

I am using DllImport to call NetDfsGetInfo to test getting information about a DFS share. I had to create structures for the level of information I wanted to receive from the function.

public struct DFS_INFO_3
{
    [MarshalAs(UnmanagedType.LPWStr)]
    public string EntryPath;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string Comment;
    public UInt32 State;
    public UInt32 NumberOfStorages;
    public IntPtr Storage;
}

public struct DFS_STORAGE_INFO
{
    public ulong State;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ServerName;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ShareName;
}

I used the following code to get and read the result:

[DllImport("netapi32", CallingConvention = CallingConvention.Winapi)]
public static extern int NetDfsGetInfo(
                    [MarshalAs(UnmanagedType.LPWStr)] string DfsEntryPath,
                    [MarshalAs(UnmanagedType.LPWStr)]string ServerName, 
                    [MarshalAs(UnmanagedType.LPWStr)]string ShareName, 
                    int Level, 
                    out IntPtr Buffer);

public void GetInfo() {
    IntPtr buffer;
    string dfsPath = "\\\\server\\share";

    int result = NetDfsGetInfo(EntryPath, null, null, 3, out buffer);

    if (result == 0) { //0 is Success
        DFS_INFO_3 info = (DFS_INFO_3)Marshal.PtrToStructure(buffer, typeof(DFS_INFO_3));
        DFS_STORAGE_INFO storage = (DFS_STORAGE_INFO)Marshal.PtrToStructure(info.Storage, typeof(DFS_STORAGE_INFO)); //Error
        Console.WriteLine("{0}, {1}, {2} ", storage.ServerName, storage.ShareName, storage.State);
    }
}

Everything goes fine until it goes to read the storage line.

I sometimes get an error that says:

The runtime has encountered a fatal error. The address of the error was at 0x626ac91c, on thread 0x1d44. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

The error type is fatalExecutionEngineError, which isn’t a .NET Exception, so I can’t catch it. It doesn’t happen every time, but I’d say about 50% of the time I get this error. Looking up Windows error codes, I’m seeing that 5 is “Access Denied”. Being only intro-level Comp Sci proficient, I understand a bit about pointers (which is what the IntPtr object is), and that the pointer may have spilled over into a piece of the result that it wasn’t supposed to? I still don’t understand why this error would occur only some of the time.

How do I understand/avoid this error?

  • 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-06-07T17:26:38+00:00Added an answer on June 7, 2026 at 5:26 pm

    Your definition of DFS_STORAGE_INFO is incorrect. In .NET a long/ulong is 64 bits while in unmanaged Win32 it’s only 32 bits (the same as an int/uint)

    public struct DFS_STORAGE_INFO
    {
        public UInt32 State;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string ServerName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string ShareName;
    }
    

    When you marshal the DFS_STORAGE_INFO you would be reading 4 bytes past the end of the structure – which may or may not work depending on what is after the structure.

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

Sidebar

Related Questions

I am trying to use the ASP.NET Web API Self-Host option with Windows authentication
I am trying to use native windows API with Qt using mingw toolset. There
I am trying to use windows dll functionality in Linux. My current solution is
While trying to use LINQ in a .NET 2.0 Windows Forms project I got:
I'm trying to use windows' API IsTextUnicode to check if a character input is
I am trying to use gmail's SMTP server smtp.gmail.com to send mails using C
Im trying to use a function from Windows API on delphi, functions for Windows
I'm trying to use the Windows 7 Taskbar API to set the overlay icon
im trying to read newline terminated strings using windows api and saw that the
I am trying to use _findfirst() Windows API in C to match file name

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.