public static void SetStreamFunction(Streams.PlStreamType streamType, StreamsFunction functionType, Delegate function) { //int size_of_IOSTREAM = 136; PrologCLR.PinObject(function); int size_of_IOSTREAM; int offset_to_poninter_of_IOFUNCTIONS; int size_of_pointer; if (Is64Bit()) { size_of_IOSTREAM = 232; offset_to_poninter_of_IOFUNCTIONS = 104; size_of_pointer = 8; } else { size_of_IOSTREAM = 144; offset_to_poninter_of_IOFUNCTIONS = 72; size_of_pointer = 4; } #if USESAFELIB IntPtr callbackFunctionPtr = Marshal.GetFunctionPointerForDelegate(function); IntPtr address_std_stream_array; if (PrologCLR.IsLinux) { address_std_stream_array = NativeMethodsLinux.GetProcAddress(m_hLibrary, "S__iob"); } else { address_std_stream_array = NativeMethodsWindows.GetProcAddress(m_hLibrary, "S__iob"); } IntPtr function_array_out = Marshal.ReadIntPtr(address_std_stream_array, (size_of_IOSTREAM * (int)streamType) + offset_to_poninter_of_IOFUNCTIONS); #if false #if _PL_X64 Marshal.WriteIntPtr(new IntPtr(function_array_out.ToInt64() + (size_of_pointer * (int)functionType)), callbackFunctionPtr); #else Marshal.WriteIntPtr(new IntPtr(function_array_out.ToInt32() + (size_of_pointer * (int)functionType)), callbackFunctionPtr); #endif // _PL_X64 #endif // false if (Is64Bit()) { Marshal.WriteIntPtr(new IntPtr(function_array_out.ToInt64() + (size_of_pointer * (int)functionType)), callbackFunctionPtr); } else { Marshal.WriteIntPtr(new IntPtr(function_array_out.ToInt32() + (size_of_pointer * (int)functionType)), callbackFunctionPtr); } #endif // USESAFELIB }
internal static void SetStreamFunction(Streams.PlStreamType streamType, StreamsFunction functionType, Delegate function) { // to calculate to following values use the C++ Project CalcStreamSize #if _PL_X64 #warning _PL_X64 is defined const int sizeOfIostream = 256; const int offsetToPoninterOfIofunctions = 104; const int sizeOfPointer = 8; #else #warning _PL_X64 is NOT defined const int sizeOfIostream = 160; const int offsetToPoninterOfIofunctions = 72; const int sizeOfPointer = 4; #endif IntPtr callbackFunctionPtr = Marshal.GetFunctionPointerForDelegate(function); IntPtr addressStdStreamArray = NativeMethods.GetPoninterOfIoFunctions(_hLibrary); IntPtr functionArrayOut = Marshal.ReadIntPtr(addressStdStreamArray, (sizeOfIostream * (int)streamType) + offsetToPoninterOfIofunctions); #if _PL_X64 Marshal.WriteIntPtr(new IntPtr(functionArrayOut.ToInt64() + (sizeOfPointer * (int)functionType)), callbackFunctionPtr); #else Marshal.WriteIntPtr(new IntPtr(functionArrayOut.ToInt32() + (sizeOfPointer * (int)functionType)), callbackFunctionPtr); #endif }