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

Related Questions

All this originated from me poking at a compiler warning message (C4267) when attempting
I'm always surprised that even after using C# for all this time now, I
I have a small app that has a Render thread. All this thread does
I've seen this all over the place: Download here! SHA1 = 8e1ed2ce9e7e473d38a9dc7824a384a9ac34d7d0 What does
What's all this business about Flash, Flex, Adobe Air, Java FX and Silverlight? Why
I've managed to mostly ignore all this multi-byte character stuff, but now I need
I don't see what all this fuss is about Microsoft's decision to support JQuery
All of this is pertaining to WebHttp binding, hosted in a custom Service Host
I see this all the time in Ruby: require File.dirname(__FILE__) + /../../config/environment What does
I do this all the time using VNC and it is very easy, but

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.