Click here to Skip to main content
15,887,892 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//Define Code internal static class UnsafeMethods { [DllImport("kernel32.dll", SetLastError = true)] internal extern static IntPtr LoadLibrary(string libraryName); [DllImport("kernel32.dll", SetLastError = true)] internal extern static bool FreeLibrary(IntPtr hModule); [DllImport("kernel32.dll", SetLastError = true)] internal extern static IntPtr GetProcAddress(IntPtr hModule, string procName); }

public class Simulator : IDisposable { //Define Delegates. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void PopulateInputsDelegate(int nbrInputArgs, double[] inp); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void GetOutputsDelegate(double[] inputArgs); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void LoadCalsDelegate(string outputFileName, string inputFileName, int writeOutputFile); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void InitializeMethods_Sim(); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void TimeBasedEvent();

    //Define Function Pointers.
    PopulateInputsDelegate PopulateInputs;
    GetOutputsDelegate GetOutputs;
    LoadCalsDelegate LoadCals;
    InitializeMethods_Sim funcInitializeMethods_Sim;


    TimeBasedEvent MngEPTR_EngineCycle;
    TimeBasedEvent MngEPTR_ExhTMP_12p5ms;
    TimeBasedEvent MngEPTR_ExhTMP_100ms;

    //Define Handles
    IntPtr dllHandle;
    IntPtr funcaddrPopulateInputs;
    IntPtr funcaddrGetOutputs;
    IntPtr funcaddrLoadCals;
    IntPtr funcaddrInitializeMethods_Sim;
    IntPtr funcaddrMngEPTR_ExhTMP_12p5ms;
    IntPtr funcaddrMngEPTR_ExhTMP_100ms;
    IntPtr funcaddrMngEPTR_EngineCycle;
public void InitializeSimulator() { try { //Load this.dllHandle = UnsafeMethods.LoadLibrary(this.model.OutputDLLPath); if (dllHandle == IntPtr.Zero) { int errorCode = Marshal.GetLastWin32Error(); throw new Exception(string.Format("Failed to load library (ErrorCode: {0})", errorCode)); };

            funcaddrPopulateInputs = UnsafeMethods.GetProcAddress(dllHandle, "PopulateInputs");
            PopulateInputs = Marshal.GetDelegateForFunctionPointer(funcaddrPopulateInputs, typeof(PopulateInputsDelegate)) as PopulateInputsDelegate;

            funcaddrGetOutputs = UnsafeMethods.GetProcAddress(dllHandle, "GetOutputs");
            GetOutputs = Marshal.GetDelegateForFunctionPointer(funcaddrGetOutputs, typeof(GetOutputsDelegate)) as GetOutputsDelegate;

            funcaddrLoadCals = UnsafeMethods.GetProcAddress(dllHandle, "LoadCals");
            LoadCals = Marshal.GetDelegateForFunctionPointer(funcaddrLoadCals, typeof(LoadCalsDelegate)) as LoadCalsDelegate;

            funcaddrInitializeMethods_Sim = UnsafeMethods.GetProcAddress(dllHandle, "InitializeMethods_Sim");
            funcInitializeMethods_Sim = Marshal.GetDelegateForFunctionPointer(funcaddrInitializeMethods_Sim, typeof(InitializeMethods_Sim)) as InitializeMethods_Sim;

            funcaddrMngEPTR_ExhTMP_12p5ms = UnsafeMethods.GetProcAddress(dllHandle, "_12p5Methods_Sim");
            MngEPTR_ExhTMP_12p5ms = Marshal.GetDelegateForFunctionPointer(funcaddrMngEPTR_ExhTMP_12p5ms, typeof(TimeBasedEvent)) as TimeBasedEvent;

            funcaddrMngEPTR_ExhTMP_100ms = UnsafeMethods.GetProcAddress(dllHandle, "_100Methods_Sim");
            MngEPTR_ExhTMP_100ms = Marshal.GetDelegateForFunctionPointer(funcaddrMngEPTR_ExhTMP_100ms, typeof(TimeBasedEvent)) as TimeBasedEvent;

            funcaddrMngEPTR_EngineCycle = UnsafeMethods.GetProcAddress(dllHandle, "_EngineCycle_Sim");
            MngEPTR_EngineCycle = Marshal.GetDelegateForFunctionPointer(funcaddrMngEPTR_EngineCycle, typeof(TimeBasedEvent)) as TimeBasedEvent;

        }
private void SimulateEventBasedGroup(MeasurementData inputMeasuredData) { // Log the method entry. Logger.Instance.Debug(Constants.FUNCTION_ENTERED_LOG);

        try
        {
            this.inputMeasuredData = new MeasurementData();
            this.inputMeasuredData = inputMeasuredData;

            ////Load Cals
            //this.LoadCals.Invoke(this.GetCalsArray());

            int rowCount = this.inputMeasuredData.SignalGroup.RowCount;

            //Load Data & execute EngineCycle
            for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                this.PopulateInputs.Invoke(this.xSignals.Count, this.GetInputMeasuredSignalArray());

                //Execute Engine Cycle
                this.MngEPTR_EngineCycle.Invoke(); //MngEPTR_EngineCycle - C code method giving exception - Help needed

                this.SetOutputMeasurementData();
                //this.SetEngineCycleOutput();
            }
        }
        catch 
        {

        }
} }



C#
Exceptions caught - Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\Application.exe'. Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x719a9a14, on thread 0x2f84. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Sometimes I get : 1.) First chance execption at a 0x********. Access Violation Written Location 0x*****. 2.) R6013 - Attempt to initialize the CRT more than once.

Please help me in resolving the issue on urgent basis. Thanks in advance.


What I have tried:

Tried enable unmanaged code debug & GC.Clear. Dont know what else to try.
Posted
Comments
Richard MacCutchan 28-Nov-16 12:59pm    
You first need to find out where the error occurs. Once you have that you can trap it in the debugger and look at the stack and other variables to find out what is going on. One comment I would make is that you are not checking any of the return values from your calls to GetProcAddress, so it is always possible that one of those is returning a null or invalid address.
Member 11254844 28-Nov-16 14:16pm    
Thanks a lot for the reply.

I am getting the error at -
//Execute Engine Cycle
this.MngEPTR_EngineCycle.Invoke();


this.MngEPTR_EngineCycle is defined in the C code, other methods of C code are running well, only this is giving the exception.

Also there are no return values from all the methods. They are all void return types.
Richard MacCutchan 29-Nov-16 3:28am    
Well at a guess I would say either the function pointer is null, or the code in the function itself has a bug. But the only way to find out is by using the debugger to trace through the code.
Member 11254844 30-Nov-16 13:02pm    
Hi Richard,

I tried correcting all inputs given to C code method. But now I am facing some different issue.

Getting ExecutionEngineException, which I cant trace where is it coming.
It sometimes comes from System.Drawing, sometimes from Mscordlib. Don't know the exact reason.
Richard MacCutchan 30-Nov-16 14:04pm    
Sorry, impossible to guess from here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900