protected override void OnExiting(Object sender, EventArgs args) { base.OnExiting(sender, args); if (Sim_INF_DLL.SimDLL_Handle != IntPtr.Zero) { DLL_Methods.FreeLibrary(Sim_INF_DLL.SimDLL_Handle); } // Stop the threads }
public static void LoadSimDLL() { if (SimDLL_Handle != IntPtr.Zero) { DLL_Methods.FreeLibrary(SimDLL_Handle); SimDLL_Handle = (IntPtr)0; } string pathtoexe = Directory.GetCurrentDirectory(); SimDLL_Handle = DLL_Methods.LoadLibrary(pathtoexe + @"\SIM_CODE\maincode.dll"); IntPtr AddressOfFunc_InitSimulation = DLL_Methods.GetProcAddress(SimDLL_Handle, "InitSimulation"); InitSimulation = (InitSimulation_prototype)Marshal.GetDelegateForFunctionPointer( AddressOfFunc_InitSimulation, typeof(InitSimulation_prototype)); IntPtr AddressOfFunc_DLL_SimOneStep = DLL_Methods.GetProcAddress(SimDLL_Handle, "DLL_SimOneStep"); DLL_SimOneStep = (DLL_SimOneStep_prototype)Marshal.GetDelegateForFunctionPointer( AddressOfFunc_DLL_SimOneStep, typeof(DLL_SimOneStep_prototype)); }
public static void GenerateDllCodeAndCompile() { // Generating DLL Code and DLLs string code_original = File.ReadAllText(@"SIM_CODE\original.c"); int compfuncspos = code_original.IndexOf("#define _COMPFUNCS_"); string code_withcompfuncs = code_original.Remove(compfuncspos, 19); for (int i = 0; i < Sim_Component.Components_Data.Count; ++i) { CompData curdata = Sim_Component.Components_Data[i]; code_withcompfuncs = code_withcompfuncs.Insert(compfuncspos, curdata.Code_Sim); } int initfuncpos = code_withcompfuncs.IndexOf("int _INITFUNCS_;"); string code_withinitfuncs = code_withcompfuncs.Remove(initfuncpos, 16); for (int i = Sim_Component.Components_Data.Count - 1; i >= 0; --i) { CompData curdata = Sim_Component.Components_Data[i]; code_withinitfuncs = code_withinitfuncs.Insert(initfuncpos, "compfuncs[index++] = " + curdata.Code_Sim_FuncName + ";\n"); } //int afterupdatefuncpos = code_withinitfuncs.IndexOf("#define _AFTERUPDATEFUNCS_"); //string code_withafterupdatefuncs = code_withinitfuncs.Remove(afterupdatefuncpos, 26); //for (int i = Sim_Component.Components_Data.Count - 1; i >= 0; --i) //{ // CompData curdata = Sim_Component.Components_Data[i]; // if (curdata.IsUpdateAfterSim) // { // code_withafterupdatefuncs = code_withafterupdatefuncs.Insert(afterupdatefuncpos, curdata.Code_AfterSim); // } //} if (SimDLL_Handle != IntPtr.Zero) { DLL_Methods.FreeLibrary(SimDLL_Handle); SimDLL_Handle = (IntPtr)0; } string pathtoexe = Directory.GetCurrentDirectory(); File.WriteAllText(pathtoexe + @"\SIM_CODE\maincode.c", code_withinitfuncs); string driveletter = Path.GetPathRoot(Environment.SystemDirectory); //string command = "/c " + driveletter + @"GCC\mingw64\bin\g++"; //command += " -c -m64 -DBUILDING_EXAMPLE_DLL "; //command += "\"" + pathtoexe + @"\SIM_CODE\maincode.c" + "\" -o \""; //command += pathtoexe + @"\SIM_CODE\maincode.o" + "\""; //Extensions.ExecuteProgram("cmd", command); string GCC_Path = Path.GetFullPath(Config.GCC_Compiler_PATH); if (GCC_Path.Last() == '\\') { GCC_Path = GCC_Path.Remove(GCC_Path.Length - 1, 1); } string command = "/c " + "\"" + "\"" + GCC_Path + @"\g++" + "\"" + " -m64 -shared \""; command += pathtoexe + @"\SIM_CODE\maincode.dll" + "\" \""; command += pathtoexe + @"\SIM_CODE\maincode.c" + "\"" + "\""; Extensions.ExecuteProgram("cmd", command); LoadSimDLL(); }