1

Topic: Transmission of the delegate to C library ++ as parameter

Good afternoon. I work from a C #. There is a standard library microsoft pinvokelib.dll, written on a C ++: https://msdn.microsoft.com/ru-ru/library/as6wyhwt.aspx I found its implementation in the Internet: http://files.rsdn.org/82761/pinvokelib.dll Among other it contains examples of functions which accept as arguments of the link to functions. The example of implementation of a call of such functions from a C # also is in MSDN: https://msdn.microsoft.com/ru-ru/library/5zwkzwf4.aspx I implemented above described example at myself. On 2 different versions of Studio (2012 and 2015). In both cases there is an EVENT (NOT the EXCEPTION):" Event PinvokeStackImbalance "Function invocation PInvoke" ConsoleApplication5 is revealed! ConsoleApplication5.Program:: TestCallBack "disbalanced a stack. Possibly, it is caused by that controlled signature PInvoke does not coincide with the uncontrollable target signature. Be convinced that the calling convention and parameters of signature PInvoke coincide with the uncontrollable target signature. The transferred functions are successfully caused and transfer correct results in library. Since it not the exception the program is not interrupted and, like as, completely and in this case is correctly fulfilled. To avoid the given message, I tried to add some attributes for the given code in a C #: public class App {[UnmanagedFunctionPointer (CallingConvention. Cdecl)] [return: MarshalAs (UnmanagedType. U1)] public delegate bool FPtr ([MarshalAs (UnmanagedType. I4)] Int32 value); [UnmanagedFunctionPointer (CallingConvention. Cdecl)] [return: MarshalAs (UnmanagedType. U1)] public delegate bool FPtr2 ([MarshalAs (UnmanagedType. LPStr)] string value);//Declares managed prototypes for unmanaged functions.//[DllImport (". \\LIB \\PinvokeLib.dll")] [DllImport ("PinvokeLib.dll")] public static extern void TestCallBack (FPtr cb, [MarshalAs (UnmanagedType. I4)] Int32 value);//public static extern void TestCallBack (IntPtr cb, [MarshalAs (UnmanagedType. I4)] Int32 value);//[DllImport (". \\LIB \\PinvokeLib.dll")] [DllImport ("PinvokeLib.dll")] public static extern void TestCallBack2 (FPtr2 cb2, [MarshalAs (UnmanagedType. LPStr)] String value);//public static extern void TestCallBack2 (IntPtr cb2, [MarshalAs (UnmanagedType. LPStr)] String value); public static void Main () {FPtr cb = new FPtr (App. DoSomething);//LibWrap. TestCallBack (cb, 99); TestCallBack (cb, 99);//TestCallBack (Marshal. GetFunctionPointerForDelegate (cb), 99); FPtr2 cb2 = new FPtr2 (App. DoSomething2);//LibWrap. TestCallBack2 (cb2, "abc"); TestCallBack2 (cb2, "abc");//TestCallBack2 (Marshal. GetFunctionPointerForDelegate (cb2), "abc"); Console. ReadLine ();} [return: MarshalAs (UnmanagedType. U1)] public static bool DoSomething ([MarshalAs (UnmanagedType. I4)] Int32 value)//public static bool DoSomething (int value) {Console. WriteLine ("\nCallback called with param: {0}", value);//... return true;} [return: MarshalAs (UnmanagedType. U1)] public static bool DoSomething2 ([MarshalAs (UnmanagedType. LPStr)] String value)//public static bool DoSomething2 (String value) {Console. WriteLine ("\nCallback called with param: {0}", value);//... return true;} } Thus after reversal and performance of function DoSomething function TestCallBack produces an exception: AccessViolationException was unhandled An unhandled exception of type ' System. AccessViolationException ' occurred in ConsoleApplication1.exe Additional information: read or write Attempt in the protected storage. It often testifies that other storage is damaged. What do I do not so? How it is correct to cause the given functions that was not neither messages, nor exceptions??