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. Unmanaged dll call from C#, AccessViolationExceptionMessage [modified]

Unmanaged dll call from C#, AccessViolationExceptionMessage [modified]

Scheduled Pinned Locked Moved C#
csharpc++helpvisual-studioperformance
8 Posts 5 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.
  • M Offline
    M Offline
    marca292
    wrote on last edited by
    #1

    Hi, I am calling a .dll from C#. There is no problem loading the dll and call some functions from C#, but one function call gives "System.AccessViolationExceptionMessage: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." There is no problem to call this function from c++. I suppose something is wrong with the structures I use in C# but I'm not sure what and how to solve this issue. Using Visual Studio 2008. ********************************** C++ **********************************

    typedef enum _MAPI_APN_TYPE
    {
    MAPI_APN_PRIMARY = 0,
    MAPI_APN_ACTIVATION,

    }MAPI_APN_TYPE;

    typedef struct _MAPI_APN_DATA
    {
    wchar_t wcApn[80];
    wchar_t wcUsername[80];
    wchar_t wcPassword[80];
    wchar_t wcDialNumber[80];

    }MAPI_APN_DATA, * LPMAPI_APN_DATA;

    typedef struct _MAPI_CORE_PATH
    {
    wchar_t strPath [ 260 ];

    }MAPI_CORE_PATH, *LPMAPI_CORE_PATH;

    ***** c++ function that works: *******
    MAPI_APN_DATA data;
    MAPI_CORE_PATH path;
    memset( &path, 0, sizeof( MAPI_CORE_PATH ) );
    result = M_API_GetApnData( &path, MAPI_APN_ACTIVATION, &data );

    ********************************** C# **********************************

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct MAPI_APN_DATA
    {
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    public string apn;

    \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
    public string username;
    
    \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
    public string password;
    
    \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
    public string dialNumber;
    

    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct MAPI_CORE_PATH
    {
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public string strPath;
    }
    public enum MAPI_APN_TYPE
    {
    MAPI_APN_PRIMARY = 0,
    MAPI_APN_ACTIVATION,
    };

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate MAPI_RESULT M_API_GetApnData(ref MAPI_CORE_PATH path, MAPI_APN_TYPE apn_type, ref MAPI_APN_DATA data);
    private M_API_GetApnData fnMGetApnData;

    //Init function delegates
    pFunction = MbmFramework.Native.GetProcAddress(pMClientDll, "M_API_GetApnData");
    fnMGetApnData = (M_API_GetApnData)Marshal.GetDelegateForFunctionPointer(pFunction, typeof(M_API_GetApnData));

    //And the method that calls fnMGetApnData and gives System.AccessViolationExceptionMessage
    MAPI_APN_DATA data = new MAPI_APN_DATA();
    MAPI_CORE_PATH path = new MAPI_CORE_PATH

    L B P G 4 Replies Last reply
    0
    • M marca292

      Hi, I am calling a .dll from C#. There is no problem loading the dll and call some functions from C#, but one function call gives "System.AccessViolationExceptionMessage: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." There is no problem to call this function from c++. I suppose something is wrong with the structures I use in C# but I'm not sure what and how to solve this issue. Using Visual Studio 2008. ********************************** C++ **********************************

      typedef enum _MAPI_APN_TYPE
      {
      MAPI_APN_PRIMARY = 0,
      MAPI_APN_ACTIVATION,

      }MAPI_APN_TYPE;

      typedef struct _MAPI_APN_DATA
      {
      wchar_t wcApn[80];
      wchar_t wcUsername[80];
      wchar_t wcPassword[80];
      wchar_t wcDialNumber[80];

      }MAPI_APN_DATA, * LPMAPI_APN_DATA;

      typedef struct _MAPI_CORE_PATH
      {
      wchar_t strPath [ 260 ];

      }MAPI_CORE_PATH, *LPMAPI_CORE_PATH;

      ***** c++ function that works: *******
      MAPI_APN_DATA data;
      MAPI_CORE_PATH path;
      memset( &path, 0, sizeof( MAPI_CORE_PATH ) );
      result = M_API_GetApnData( &path, MAPI_APN_ACTIVATION, &data );

      ********************************** C# **********************************

      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
      public struct MAPI_APN_DATA
      {
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
      public string apn;

      \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
      public string username;
      
      \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
      public string password;
      
      \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
      public string dialNumber;
      

      }
      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
      public struct MAPI_CORE_PATH
      {
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
      public string strPath;
      }
      public enum MAPI_APN_TYPE
      {
      MAPI_APN_PRIMARY = 0,
      MAPI_APN_ACTIVATION,
      };

      [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
      private delegate MAPI_RESULT M_API_GetApnData(ref MAPI_CORE_PATH path, MAPI_APN_TYPE apn_type, ref MAPI_APN_DATA data);
      private M_API_GetApnData fnMGetApnData;

      //Init function delegates
      pFunction = MbmFramework.Native.GetProcAddress(pMClientDll, "M_API_GetApnData");
      fnMGetApnData = (M_API_GetApnData)Marshal.GetDelegateForFunctionPointer(pFunction, typeof(M_API_GetApnData));

      //And the method that calls fnMGetApnData and gives System.AccessViolationExceptionMessage
      MAPI_APN_DATA data = new MAPI_APN_DATA();
      MAPI_CORE_PATH path = new MAPI_CORE_PATH

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

      Does the exception occur inside the fnMGetApnData() function? If so you need to track exactly what is going on in there, and without seeing the code it is impossible for anyone here to make a guess.

      The best things in life are not things.

      M 1 Reply Last reply
      0
      • L Lost User

        Does the exception occur inside the fnMGetApnData() function? If so you need to track exactly what is going on in there, and without seeing the code it is impossible for anyone here to make a guess.

        The best things in life are not things.

        M Offline
        M Offline
        marca292
        wrote on last edited by
        #3

        Yes, fnMGetApnData() is the unmanaged call to the dll. I don't have the source code to this dll. I tried to debug this unmanaged call in the assembler code but have not enought assembler experience. The only thing I can see is that a assembler "call" is causing the exception.

        L 1 Reply Last reply
        0
        • M marca292

          Yes, fnMGetApnData() is the unmanaged call to the dll. I don't have the source code to this dll. I tried to debug this unmanaged call in the assembler code but have not enought assembler experience. The only thing I can see is that a assembler "call" is causing the exception.

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

          marca292 wrote:

          I don't have the source code to this dll

          It's unlikely that anyone here will be able to guess what's going on. Is there any way you can get hold of the people who created the dll?

          The best things in life are not things.

          M 1 Reply Last reply
          0
          • L Lost User

            marca292 wrote:

            I don't have the source code to this dll

            It's unlikely that anyone here will be able to guess what's going on. Is there any way you can get hold of the people who created the dll?

            The best things in life are not things.

            M Offline
            M Offline
            marca292
            wrote on last edited by
            #5

            I was just wondering if anyone could see something wrong with my C# struct declaration that could cause this error. Maybe, I shall see if I can get hold of the people who created this dll.

            1 Reply Last reply
            0
            • M marca292

              Hi, I am calling a .dll from C#. There is no problem loading the dll and call some functions from C#, but one function call gives "System.AccessViolationExceptionMessage: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." There is no problem to call this function from c++. I suppose something is wrong with the structures I use in C# but I'm not sure what and how to solve this issue. Using Visual Studio 2008. ********************************** C++ **********************************

              typedef enum _MAPI_APN_TYPE
              {
              MAPI_APN_PRIMARY = 0,
              MAPI_APN_ACTIVATION,

              }MAPI_APN_TYPE;

              typedef struct _MAPI_APN_DATA
              {
              wchar_t wcApn[80];
              wchar_t wcUsername[80];
              wchar_t wcPassword[80];
              wchar_t wcDialNumber[80];

              }MAPI_APN_DATA, * LPMAPI_APN_DATA;

              typedef struct _MAPI_CORE_PATH
              {
              wchar_t strPath [ 260 ];

              }MAPI_CORE_PATH, *LPMAPI_CORE_PATH;

              ***** c++ function that works: *******
              MAPI_APN_DATA data;
              MAPI_CORE_PATH path;
              memset( &path, 0, sizeof( MAPI_CORE_PATH ) );
              result = M_API_GetApnData( &path, MAPI_APN_ACTIVATION, &data );

              ********************************** C# **********************************

              [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
              public struct MAPI_APN_DATA
              {
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
              public string apn;

              \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
              public string username;
              
              \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
              public string password;
              
              \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
              public string dialNumber;
              

              }
              [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
              public struct MAPI_CORE_PATH
              {
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
              public string strPath;
              }
              public enum MAPI_APN_TYPE
              {
              MAPI_APN_PRIMARY = 0,
              MAPI_APN_ACTIVATION,
              };

              [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
              private delegate MAPI_RESULT M_API_GetApnData(ref MAPI_CORE_PATH path, MAPI_APN_TYPE apn_type, ref MAPI_APN_DATA data);
              private M_API_GetApnData fnMGetApnData;

              //Init function delegates
              pFunction = MbmFramework.Native.GetProcAddress(pMClientDll, "M_API_GetApnData");
              fnMGetApnData = (M_API_GetApnData)Marshal.GetDelegateForFunctionPointer(pFunction, typeof(M_API_GetApnData));

              //And the method that calls fnMGetApnData and gives System.AccessViolationExceptionMessage
              MAPI_APN_DATA data = new MAPI_APN_DATA();
              MAPI_CORE_PATH path = new MAPI_CORE_PATH

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

              Make sure wchar_t is what you think it is. I don't see anything obviously wrong with the structure definitions. Alternatively, does the function rely on the path being set to zeros? You are doing that in C++ and not C#. You might need to assign "\0" to path.strPath if the C function expects it to be set to zero length to start with. (I'm not sure what the marshalling does with an unassigned string variable in a struct. You might need to assign a value to all the string properties; a constructor or method on the struct to do that might be a good idea.)

              1 Reply Last reply
              0
              • M marca292

                Hi, I am calling a .dll from C#. There is no problem loading the dll and call some functions from C#, but one function call gives "System.AccessViolationExceptionMessage: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." There is no problem to call this function from c++. I suppose something is wrong with the structures I use in C# but I'm not sure what and how to solve this issue. Using Visual Studio 2008. ********************************** C++ **********************************

                typedef enum _MAPI_APN_TYPE
                {
                MAPI_APN_PRIMARY = 0,
                MAPI_APN_ACTIVATION,

                }MAPI_APN_TYPE;

                typedef struct _MAPI_APN_DATA
                {
                wchar_t wcApn[80];
                wchar_t wcUsername[80];
                wchar_t wcPassword[80];
                wchar_t wcDialNumber[80];

                }MAPI_APN_DATA, * LPMAPI_APN_DATA;

                typedef struct _MAPI_CORE_PATH
                {
                wchar_t strPath [ 260 ];

                }MAPI_CORE_PATH, *LPMAPI_CORE_PATH;

                ***** c++ function that works: *******
                MAPI_APN_DATA data;
                MAPI_CORE_PATH path;
                memset( &path, 0, sizeof( MAPI_CORE_PATH ) );
                result = M_API_GetApnData( &path, MAPI_APN_ACTIVATION, &data );

                ********************************** C# **********************************

                [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
                public struct MAPI_APN_DATA
                {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                public string apn;

                \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
                public string username;
                
                \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
                public string password;
                
                \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
                public string dialNumber;
                

                }
                [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
                public struct MAPI_CORE_PATH
                {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
                public string strPath;
                }
                public enum MAPI_APN_TYPE
                {
                MAPI_APN_PRIMARY = 0,
                MAPI_APN_ACTIVATION,
                };

                [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
                private delegate MAPI_RESULT M_API_GetApnData(ref MAPI_CORE_PATH path, MAPI_APN_TYPE apn_type, ref MAPI_APN_DATA data);
                private M_API_GetApnData fnMGetApnData;

                //Init function delegates
                pFunction = MbmFramework.Native.GetProcAddress(pMClientDll, "M_API_GetApnData");
                fnMGetApnData = (M_API_GetApnData)Marshal.GetDelegateForFunctionPointer(pFunction, typeof(M_API_GetApnData));

                //And the method that calls fnMGetApnData and gives System.AccessViolationExceptionMessage
                MAPI_APN_DATA data = new MAPI_APN_DATA();
                MAPI_CORE_PATH path = new MAPI_CORE_PATH

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Try changing the CharSet to CharSet.Ansi.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                1 Reply Last reply
                0
                • M marca292

                  Hi, I am calling a .dll from C#. There is no problem loading the dll and call some functions from C#, but one function call gives "System.AccessViolationExceptionMessage: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." There is no problem to call this function from c++. I suppose something is wrong with the structures I use in C# but I'm not sure what and how to solve this issue. Using Visual Studio 2008. ********************************** C++ **********************************

                  typedef enum _MAPI_APN_TYPE
                  {
                  MAPI_APN_PRIMARY = 0,
                  MAPI_APN_ACTIVATION,

                  }MAPI_APN_TYPE;

                  typedef struct _MAPI_APN_DATA
                  {
                  wchar_t wcApn[80];
                  wchar_t wcUsername[80];
                  wchar_t wcPassword[80];
                  wchar_t wcDialNumber[80];

                  }MAPI_APN_DATA, * LPMAPI_APN_DATA;

                  typedef struct _MAPI_CORE_PATH
                  {
                  wchar_t strPath [ 260 ];

                  }MAPI_CORE_PATH, *LPMAPI_CORE_PATH;

                  ***** c++ function that works: *******
                  MAPI_APN_DATA data;
                  MAPI_CORE_PATH path;
                  memset( &path, 0, sizeof( MAPI_CORE_PATH ) );
                  result = M_API_GetApnData( &path, MAPI_APN_ACTIVATION, &data );

                  ********************************** C# **********************************

                  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
                  public struct MAPI_APN_DATA
                  {
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                  public string apn;

                  \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
                  public string username;
                  
                  \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
                  public string password;
                  
                  \[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)\]
                  public string dialNumber;
                  

                  }
                  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
                  public struct MAPI_CORE_PATH
                  {
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
                  public string strPath;
                  }
                  public enum MAPI_APN_TYPE
                  {
                  MAPI_APN_PRIMARY = 0,
                  MAPI_APN_ACTIVATION,
                  };

                  [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
                  private delegate MAPI_RESULT M_API_GetApnData(ref MAPI_CORE_PATH path, MAPI_APN_TYPE apn_type, ref MAPI_APN_DATA data);
                  private M_API_GetApnData fnMGetApnData;

                  //Init function delegates
                  pFunction = MbmFramework.Native.GetProcAddress(pMClientDll, "M_API_GetApnData");
                  fnMGetApnData = (M_API_GetApnData)Marshal.GetDelegateForFunctionPointer(pFunction, typeof(M_API_GetApnData));

                  //And the method that calls fnMGetApnData and gives System.AccessViolationExceptionMessage
                  MAPI_APN_DATA data = new MAPI_APN_DATA();
                  MAPI_CORE_PATH path = new MAPI_CORE_PATH

                  G Offline
                  G Offline
                  George Tryfonas
                  wrote on last edited by
                  #8

                  I'm confused as to why you're using the UnmanagedFunctionPointerAttribute to get the dll function and then go through all that marshalling mess. Wasn't a simple DllImport enough?

                  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