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

The Archive Base Latest Questions

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

All, this is a follow up from a previous question here: C# formatting external

  • 0

All, this is a follow up from a previous question here: C# formatting external Dll function parameters

Here specifically is the code that I am trying to convert to C#:

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));  // Get the mapping info FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pMapping, &PagePerSector); 

The function ‘FILES_GetMemoryMapping’ gets called twice, I’m guessing the first time to get the size of the struct, and the second to actually fill it.

the ‘pMapping’ is a pointer to a struct in C++, In my C# code, I have pMapping as type IntPtr. The next line I can converted to :

pMapping = Marshal.AllocHGlobal(Marshal.SizeOf(new UM0516.Mapping())); 

With (UM0516.Mapping) being the struct. Cool, so I’ve just allocated some space that IntPtr is pointing to. Now for the next line… ‘pMapping->NbSectors = 0;’

How am I suppose to go into the now allocated unmanaged memory space, type cast it as a (UM0516.Mapping) struct, and set one of its members? Then make sure i didn’t screw with it too much so that the second time I call ‘FILES_GetMemoryMapping’, it can now use this struct??

— Ok, I’ve taken some advice and now have this:

I tried this and I get a ‘AccessViolationException was unhandled’ exception on the first ‘FILES_GetMemoryMapping’ call

Here is what I have:

string filepath = @'C:\blah.blah'; string MapFile = @'D:\blah.blah'; UM0516.Mapping myMapping = new UM0516.Mapping(); IntPtr pMapping = Marshal.AllocHGlobal(Marshal.SizeOf(myMapping)); Marshal.StructureToPtr(myMapping, pMapping, false); ushort PacketSize = 0; ushort size = 0; string MapName = String.Empty; byte PagePerSector = 0;  uint b7 = UM0516.FILES_GetMemoryMapping(MapFile, out size, MapName, out PacketSize, pMapping, out PagePerSector); 

Do you think this exception is coming from the ‘pMapping’ parameter? Could this come from anything else that I’ve passed in?

  • 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:41:01+00:00Added an answer on May 11, 2026 at 12:41 pm

    In order to get the IntPtr, what you’ll want to do is create your structure, set any options that you may need to, allocate the memory like you already have, then call..

    System.Runtime.InteropServices.Marshal.StructureToPtr(yourStructVariable, pMapping, false); 

    This will copy the data from your populated structure into your allocated memory.

    To copy data from the memory into a new structure named ‘mapping‘, call…

    UM0516.Mapping mapping = (UM0516.Mapping)System.Runtime.InteropServices.Marshal.PtrToStructure(pMapping, typeof(UM0516.Mapping)) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • 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 Presumably the userid under which your app is running doesn't… May 11, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer The most standard way I know of to do this… May 11, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer The command line client shows the lock owner with the… May 11, 2026 at 5:05 pm

Related Questions

This question is a follow up to my previous question about getting the HTML
I am attempting to build a .PLY parser to load 3d models stored as
I'm the author of GitX . One of the features GitX has is the
Here is the scenario 1. I have a table called items, inside the table

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.