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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. System.AccessViolationException - Returning from cpp dll

System.AccessViolationException - Returning from cpp dll

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++helpdata-structuresdebugging
2 Posts 2 Posters 5 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.
  • T Offline
    T Offline
    ToolMaker2007
    wrote on last edited by
    #1

    Hi All, Im sure this is easy. I'm forgotten to cpp. I need to write cpp wrapper over a dot net c# web service proxy class. I thought I would start with this basic tutorial...LOL already stuck. Overview: I have a Dotnet(c# managed) dll just adding some text to your input string:

    public class CSharpClass
    {
    public static byte[] Hello(byte[] name)
    {
    string s = ", hello from .NET!";
    byte[] helloPart = Encoding.ASCII.GetBytes(s);
    byte[] whole =
    new byte[name.Length + helloPart.Length];
    int i = 0;
    foreach (byte b in name)
    {
    whole[i++] = b;
    }
    foreach (byte b in helloPart)
    {
    whole[i++] = b;
    }
    return whole;
    }
    }

    This get consummed by the CPP dll:

    using namespace CSharpAssembly;

    __declspec(dllexport) char* __stdcall Hello(char* name)
    {
    int i = 0;
    while (*name != '\0')
    {
    i++;
    name++;
    }
    array<unsigned char>^ nameManArr = gcnew array<unsigned char>(i);
    name -= i;
    i = 0;
    while (*name != '\0')
    {
    nameManArr[i] = *name;
    name++;
    i++;
    }
    array<unsigned char>^ char8ManArr = CSharpClass::Hello(nameManArr);
    char* char8UnmanArr = new char[char8ManArr->Length + 1];
    for (int i = 0; i < char8ManArr->Length; i++)
    {
    char8UnmanArr[i] = char8ManArr[i];
    }
    char8UnmanArr[char8ManArr->Length] = '\0';
    return char8UnmanArr;
    }

    I have a debug entry test command line app (C#) consuming the C++ dll.

    class DebugEntry
    {
    [DllImport("CppStdcallInerfaceWrapper2.dll",
    CharSet = CharSet.Ansi, CallingConvention =
    CallingConvention.StdCall)]
    public static extern string Hello(string name);

        static void Main(string\[\] args)
        {
            string sd = Hello("MyName");
            System.Console.WriteLine();
            System.Console.ReadLine();
        }
    }
    

    When I run the command line app I can debug through the cpp dll into the C# dll but when I return from the cpp dll I get error on returning to the debug commanline app. I get error:

    An unhandled exception of type 'System.AccessViolationException' occurred in mscorlib.dll

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

    U 1 Reply Last reply
    0
    • T ToolMaker2007

      Hi All, Im sure this is easy. I'm forgotten to cpp. I need to write cpp wrapper over a dot net c# web service proxy class. I thought I would start with this basic tutorial...LOL already stuck. Overview: I have a Dotnet(c# managed) dll just adding some text to your input string:

      public class CSharpClass
      {
      public static byte[] Hello(byte[] name)
      {
      string s = ", hello from .NET!";
      byte[] helloPart = Encoding.ASCII.GetBytes(s);
      byte[] whole =
      new byte[name.Length + helloPart.Length];
      int i = 0;
      foreach (byte b in name)
      {
      whole[i++] = b;
      }
      foreach (byte b in helloPart)
      {
      whole[i++] = b;
      }
      return whole;
      }
      }

      This get consummed by the CPP dll:

      using namespace CSharpAssembly;

      __declspec(dllexport) char* __stdcall Hello(char* name)
      {
      int i = 0;
      while (*name != '\0')
      {
      i++;
      name++;
      }
      array<unsigned char>^ nameManArr = gcnew array<unsigned char>(i);
      name -= i;
      i = 0;
      while (*name != '\0')
      {
      nameManArr[i] = *name;
      name++;
      i++;
      }
      array<unsigned char>^ char8ManArr = CSharpClass::Hello(nameManArr);
      char* char8UnmanArr = new char[char8ManArr->Length + 1];
      for (int i = 0; i < char8ManArr->Length; i++)
      {
      char8UnmanArr[i] = char8ManArr[i];
      }
      char8UnmanArr[char8ManArr->Length] = '\0';
      return char8UnmanArr;
      }

      I have a debug entry test command line app (C#) consuming the C++ dll.

      class DebugEntry
      {
      [DllImport("CppStdcallInerfaceWrapper2.dll",
      CharSet = CharSet.Ansi, CallingConvention =
      CallingConvention.StdCall)]
      public static extern string Hello(string name);

          static void Main(string\[\] args)
          {
              string sd = Hello("MyName");
              System.Console.WriteLine();
              System.Console.ReadLine();
          }
      }
      

      When I run the command line app I can debug through the cpp dll into the C# dll but when I return from the cpp dll I get error on returning to the debug commanline app. I get error:

      An unhandled exception of type 'System.AccessViolationException' occurred in mscorlib.dll

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

      U Offline
      U Offline
      User 10455964
      wrote on last edited by
      #2

      What do you mean by returning? You mean after you invoke the static method Hello.

      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