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

The Archive Base Latest Questions

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

I have TXT file in shard memory. The code is at the end. I’ve

  • 0

I have TXT file in shard memory.
The code is at the end. I’ve been attempting to get it out of the memory and write to a file in the C:\ drive.

But i get a error:

    Type 'SharedMemSaveToFile.SharedMemSaver+Data' cannot be marshaled as an 
unmanaged structure; no meaningful size or offset can be computed.

If i change the code to write the Memory the CMD, it works, so i know the memory is there. I’ve also tried using these to write the TXT:

 System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\file.txt");
 file.WriteLine(d);

and:

using (StreamWriter outfile = new StreamWriter(d + @"C:\\file.txt"))
{
     outfile.Write(sb.ToString());
}

and:

StreamWriter sw = new StreamWriter("file.txt");
    sw.Write(d);
    sw.Close();

Thanks!

    public class Data
    {
        static void Main(string[] args)
        {
            SharedMemSaver sf = new SharedMemSaver();
            sf.OpenView();
            String d = sf.GetData();
            System.IO.File.WriteAllText(@"C:\file.txt", d);

        }
    }

    #region Win32 API stuff
    public const int FILE_MAP_READ = 0x0004;

    [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern IntPtr OpenFileMapping(int dwDesiredAccess,
        bool bInheritHandle, StringBuilder lpName);

    [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern IntPtr MapViewOfFile(IntPtr hFileMapping,
        int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow,
        int dwNumberOfBytesToMap);

    [DllImport("Kernel32.dll")]
    internal static extern bool UnmapViewOfFile(IntPtr map);

    [DllImport("kernel32.dll")]
    internal static extern bool CloseHandle(IntPtr hObject);
    #endregion

    private bool fileOpen = false;
    private IntPtr map;
    private IntPtr handle;

    ~SharedMemSaver()
    {
        CloseView();
    }

    public bool OpenView()
    {
        if (!fileOpen)
        {
            StringBuilder sharedMemFile = new StringBuilder("Mem_Values");
            handle = OpenFileMapping(FILE_MAP_READ, false, sharedMemFile);
            if (handle == IntPtr.Zero)
            {
                throw new Exception("Unable to open file mapping.");
            }
            map = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, Marshal.SizeOf((Type)typeof(Data)));
            if (map == IntPtr.Zero)
            {
                throw new Exception("Unable to read shared memory.");
            }
            fileOpen = true;
        }
        return fileOpen;
    }

    public void CloseView()
    {
        if (fileOpen)
        {
            UnmapViewOfFile(map);
            CloseHandle(handle);
        }
    }

    public String GetData()
    {
        if (fileOpen)
        {
            String data = (String)Marshal.PtrToStringAuto(map);
            return data;
        }
        else
        {
            return null;
        }
    }
}

}

  • 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-24T23:45:55+00:00Added an answer on May 24, 2026 at 11:45 pm

    I would strongly recommend to use the built-in MemoryMappedFile class (new in .NET 4).

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

Sidebar

Related Questions

I have a txt file which contains a google adsense code block and I'm
I have a .txt file that I need to include into a page, that
I have a txt file which actually is a html source of some webpage.
I have a txt file and each line has a set of random strings,
I have a .txt file like: Symbols from __ctype_tab.o: Name Value Class Type Size
I have a txt file which contains data in the following format: X1 Y1
I have a txt file with columns separated by tabs and based on that
Imagine I have a txt file with a path like: c:\programs\SRC_CODE\ How can I
For example, I have a txt file that reads: 12 345 45 2342 234
I have a generated txt file. This file has certain lines that are superfluous,

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.