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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:17:29+00:00 2026-05-11T12:17:29+00:00

I keep getting an AccessViolationException when calling the following from an external DLL: FILES_GetMemoryMapping(MapFile,

  • 0

I keep getting an AccessViolationException when calling the following from an external DLL:

FILES_GetMemoryMapping(MapFile, out size, MapName, out PacketSize, pMapping, out PagePerSector); 

Which has a prototype that I’ve setup as such:

    [DllImport('Files.DLL', SetLastError = true)]     public static extern uint FILES_GetMemoryMapping(         [MarshalAs(UnmanagedType.LPStr)]         string pPathFile,         out ushort Size,         [MarshalAs(UnmanagedType.LPStr)]         string MapName,         out ushort PacketSize,         IntPtr pMapping,         out byte PagesPerSector); 

Now, the argument that is causing this is most likely the 5th one (IntPtr pMapping). I’ve ported this code over from a C++ app into C#. The 5th argument above is a pointer to a struct which also contains a pointer to another struct. Below is how I have these sctructs setup:

    [StructLayout(LayoutKind.Sequential)]     public struct MappingSector     {         [MarshalAs(UnmanagedType.LPStr)]         public string Name;         public uint dwStartAddress;         public uint dwAliasedAddress;         public uint dwSectorIndex;         public uint dwSectorSize;         public byte bSectorType;         public bool UseForOperation;         public bool UseForErase;         public bool UseForUpload;         public bool UseForWriteProtect;     }      [StructLayout(LayoutKind.Sequential)]     public struct Mapping     {         public byte nAlternate;         [MarshalAs(UnmanagedType.LPStr, SizeConst=260)]         public string Name;         public uint NbSectors;         public IntPtr pSectors;     } 

The C++ equivalent of these are as follows:

typedef struct {     char*       Name;     DWORD       dwStartAddress;     DWORD       dwAliasedAddress;     DWORD       dwSectorIndex;     DWORD       dwSectorSize;     BYTE        bSectorType;     BOOL        UseForOperation;     BOOL        UseForErase;     BOOL        UseForUpload;     BOOL        UseForWriteProtect; } MAPPINGSECTOR, *PMAPPINGSECTOR;  typedef struct {     BYTE            nAlternate;     char            Name[MAX_PATH]; // MAX_PATH = 260     DWORD           NbSectors;     PMAPPINGSECTOR  pSectors;    } MAPPING, *PMAPPING; 

I have a feeling I did something wrong with either porting over these structs, or porting over the function prototype. A Marshaling issue of somesort.

The function all the way at the top of this post gets called twice in my code. Once with pMapping set to null (this puts a value in ‘size’). Memory is then allocated for a new struct using this size parameter and the function is called again now using a pointer to this allocated memory space for pMapping. (pMapping also has a pointer for the other struct which also gets some space allocated during this time).

Here is the old c++ code that accomplished this:

FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pMapping, &PagePerSector); // Allocate the mapping structure memory pMapping = (PMAPPING)malloc(sizeof(MAPPING)); pMapping->NbSectors = 0; pMapping->pSectors = (PMAPPINGSECTOR) malloc((Size) * sizeof(MAPPINGSECTOR)); printf('mapsectorsize: <%d>\n', football); printf('pMappingsize: <%d>\n', f2);   // Get the mapping info FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pMapping, &PagePerSector); 

I initially thought I wasn’t allocating the correct amount of space so I tried the old C++ code above and found out that:

sizeof(MAPPING) = 272 and sizeof(PMAPPINGSECTOR) = 40 

I did the same check in my C# code and found the following:

Marshal.SizeOf(new Mapping()) = 16 and Marshal.SizeOF(new MappingSector()) = 40 

We got a problem here. The Mapping struct should be of size 272, but its only 16. Thinking I could just do a quick fix, I manually allocated 272 instead of 16 here, but it still errored out with an AccessViolationException.

Any idea on how to fix this? Or what might still be going wrong?

  • 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. 2026-05-11T12:17:29+00:00Added an answer on May 11, 2026 at 12:17 pm

    ‘prototype’ was not the correct word, I like ‘DLLImport declaration’ better.

    And I’ve just got it working.

    so in C++:

    typedef struct {     BYTE                        nAlternate;     char                        Name[MAX_PATH]; // MAX_PATH = 260     DWORD                       NbSectors;     PMAPPINGSECTOR      pSectors;        }  

    to C#:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct Mapping {     public byte nAlternate;     [MarshalAs(UnmanagedType.ByValArray, SizeConst=260)]     public char[] Name;     public uint NbSectors;     public IntPtr pSectors; } 

    A character Array is NOT a string, and should be treated as an array of characters…. Who would have guessed 😛

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The correct way to float is with "cssFloat" property: DOM.setStyleAttribute(element,… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer I'm a developer for CoRD, which is a Cocoa RDP… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer You may load html, which seems to be valid XMl… May 11, 2026 at 11:55 pm

Related Questions

I keep getting an exception about Linq to Entities not supporting certaion query expressions
I'm trying to create a view in an Oracle database, but keep getting an
I'm trying to verify a Recaptcha using jQuery, but I keep getting an error
I'm trying to get simple encryption/decryption working with AesManaged, but I keep getting an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.