Click here to Skip to main content
15,891,951 members
Home / Discussions / C#
   

C#

 
GeneralRe: String value in C# Pin
Pierre besquent5-Jul-11 0:47
Pierre besquent5-Jul-11 0:47 
GeneralRe: String value in C# Pin
mabo425-Jul-11 2:45
mabo425-Jul-11 2:45 
GeneralRe: String value in C# Pin
Pierre besquent5-Jul-11 5:55
Pierre besquent5-Jul-11 5:55 
GeneralRe: String value in C# Pin
Richard MacCutchan5-Jul-11 6:35
mveRichard MacCutchan5-Jul-11 6:35 
GeneralRe: String value in C# Pin
BobJanova5-Jul-11 6:02
BobJanova5-Jul-11 6:02 
Questiondynamic group by via LINQ Pin
devvvy4-Jul-11 18:16
devvvy4-Jul-11 18:16 
AnswerRe: dynamic group by via LINQ Pin
Abhinav S4-Jul-11 20:30
Abhinav S4-Jul-11 20:30 
QuestionUnmanaged dll call from C#, AccessViolationExceptionMessage [modified] Pin
marca2923-Jul-11 22:24
marca2923-Jul-11 22:24 
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();
MAPI_RESULT apiRes = fnMGetApnData(ref path, MAPI_APN_TYPE.MAPI_APN_ACTIVATION, ref data);


Best regards
Olof

modified on Monday, July 4, 2011 4:54 AM

AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
Richard MacCutchan3-Jul-11 22:37
mveRichard MacCutchan3-Jul-11 22:37 
GeneralRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
marca2923-Jul-11 22:47
marca2923-Jul-11 22:47 
GeneralRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
Richard MacCutchan3-Jul-11 23:22
mveRichard MacCutchan3-Jul-11 23:22 
GeneralRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
marca2923-Jul-11 23:26
marca2923-Jul-11 23:26 
AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
BobJanova4-Jul-11 0:38
BobJanova4-Jul-11 0:38 
AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
Pete O'Hanlon4-Jul-11 1:15
mvePete O'Hanlon4-Jul-11 1:15 
AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
George Tryfonas5-Jul-11 3:33
George Tryfonas5-Jul-11 3:33 
Questiondatabase connection code in c# for asp .net Pin
Arvind Vishwakarma2-Jul-11 6:09
Arvind Vishwakarma2-Jul-11 6:09 
AnswerRe: database connection code in c# for asp .net Pin
Richard MacCutchan2-Jul-11 6:47
mveRichard MacCutchan2-Jul-11 6:47 
AnswerRe: database connection code in c# for asp .net Pin
PIEBALDconsult2-Jul-11 6:54
mvePIEBALDconsult2-Jul-11 6:54 
AnswerRe: database connection code in c# for asp .net Pin
RaviRanjanKr2-Jul-11 8:34
professionalRaviRanjanKr2-Jul-11 8:34 
AnswerRe: database connection code in c# for asp .net Pin
Abhinav S2-Jul-11 23:33
Abhinav S2-Jul-11 23:33 
Questionhow to "inject" string Pin
igalep1322-Jul-11 5:53
igalep1322-Jul-11 5:53 
AnswerRe: how to "inject" string Pin
PIEBALDconsult2-Jul-11 6:06
mvePIEBALDconsult2-Jul-11 6:06 
AnswerRe: how to "inject" string PinPopular
Mark Salsbery2-Jul-11 6:09
Mark Salsbery2-Jul-11 6:09 
AnswerRe: how to "inject" string Pin
Philippe Mori2-Jul-11 12:37
Philippe Mori2-Jul-11 12:37 
AnswerRe: how to "inject" string Pin
#realJSOP3-Jul-11 5:59
mve#realJSOP3-Jul-11 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.