Unmanaged dll call from C#, AccessViolationExceptionMessage [modified]
-
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 -
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 -
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.
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.
-
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.
-
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.
-
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_PATHMake 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.)
-
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_PATHTry 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
-
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_PATHI'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 simpleDllImport
enough?