Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Scheduled Pinned Locked Moved C#
helpquestioncsharpc++sysadmin
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paulraj G
    wrote on last edited by
    #1

    Hi All, I am calling a c++ dll from c#. it throwing error like "An unhandled exception of type 'System.AccessViolationException' occurred in exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    [DllImport(@"D:\Project\Working\Toolkit-2.0\output\Win32\Debug\ipetk.dll",
    EntryPoint = "?GetModuleTypeString@ModuleIndex@Infrastructure@IPETK@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
    CallingConvention = CallingConvention.Cdecl)]
    static extern string GetModuleTypeString(ModuleType ths);

    string ModuleIndex = GetModuleTypeString(ModuleType.IPD);
    MessageBox.Show(ModuleIndex.ToString());

    how can i resolve this issue..? any help will be appriciated. Thanks

    G.Paulraj

    B L B 3 Replies Last reply
    0
    • P Paulraj G

      Hi All, I am calling a c++ dll from c#. it throwing error like "An unhandled exception of type 'System.AccessViolationException' occurred in exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

      [DllImport(@"D:\Project\Working\Toolkit-2.0\output\Win32\Debug\ipetk.dll",
      EntryPoint = "?GetModuleTypeString@ModuleIndex@Infrastructure@IPETK@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
      CallingConvention = CallingConvention.Cdecl)]
      static extern string GetModuleTypeString(ModuleType ths);

      string ModuleIndex = GetModuleTypeString(ModuleType.IPD);
      MessageBox.Show(ModuleIndex.ToString());

      how can i resolve this issue..? any help will be appriciated. Thanks

      G.Paulraj

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      This one must be in the declaration. What is the return type declared at on the C++ side? Are you sure it is defined as C call convention? I suspect you need to marshal the string differently. See [MarshalAs][^].

      1 Reply Last reply
      0
      • P Paulraj G

        Hi All, I am calling a c++ dll from c#. it throwing error like "An unhandled exception of type 'System.AccessViolationException' occurred in exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

        [DllImport(@"D:\Project\Working\Toolkit-2.0\output\Win32\Debug\ipetk.dll",
        EntryPoint = "?GetModuleTypeString@ModuleIndex@Infrastructure@IPETK@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
        CallingConvention = CallingConvention.Cdecl)]
        static extern string GetModuleTypeString(ModuleType ths);

        string ModuleIndex = GetModuleTypeString(ModuleType.IPD);
        MessageBox.Show(ModuleIndex.ToString());

        how can i resolve this issue..? any help will be appriciated. Thanks

        G.Paulraj

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Looks like the definition of your C function should be changed to something like:

        extern "C" char* GetModuleTypeString(char*);

        STL strings do not work well between managed and unmanaged code.

        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

        1 Reply Last reply
        0
        • P Paulraj G

          Hi All, I am calling a c++ dll from c#. it throwing error like "An unhandled exception of type 'System.AccessViolationException' occurred in exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

          [DllImport(@"D:\Project\Working\Toolkit-2.0\output\Win32\Debug\ipetk.dll",
          EntryPoint = "?GetModuleTypeString@ModuleIndex@Infrastructure@IPETK@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
          CallingConvention = CallingConvention.Cdecl)]
          static extern string GetModuleTypeString(ModuleType ths);

          string ModuleIndex = GetModuleTypeString(ModuleType.IPD);
          MessageBox.Show(ModuleIndex.ToString());

          how can i resolve this issue..? any help will be appriciated. Thanks

          G.Paulraj

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          The error message can also originate from a DEP (data execution protection) problem. If you get the error only when you run your compiled executable directly, but not when you debug from Visual Studio, set the DEP flag off: editbin.exe /NXCOMPAT:NO "$(TargetPath)"

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups