После запуска программы с dll-библиотеками выдает: "В GDI+ возникла ошибка общего вида." C#
Запускаю программу, включаю функцию, все считается, но спустя 5 секунд выдает ошибку "В GDI+ возникла ошибка общего вида." Вот код:
using System;
namespace dll_usage_demo
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
static extern bool FreeLibrary(IntPtr hModule);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int FmasProc_v3(double[] mas1, int size1, double[] mas2, int size2);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int FmasProc_v4(double[] mas1, int size1, double[] mas2, int size2);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int Fmas2Proc(IntPtr[] mas1, int h, int w);
private long FBeginCount, FEndCount;
private long FFrequence;
Double Ftime;
public int sizetimes = 50;
public double[] mas1;
public double[] mas2;
public double[][] masX;
public double[][] masX1;
public int Hs = 700, Ws = 700, size = 100000;
void fillArray1d(double[] arr, int size){
for (int i = 0; i < size; i++){
arr[i] = (double)((i * i + 10) / (i + i + 1));
}
}
void fillArray2d(double[][] arr, int height, int width){
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
arr[i][j] = (double)((i * j + 10) / (i + j + 1));
}
}
}
string[] func_name1 = { "alg1", "alg2", "alg3" };
public Form1()
{
InitializeComponent();
mas1 = new double[size];
mas2 = new double[size];
fillArray1d(mas1, size);
fillArray1d(mas2 , size);
masX = new double[Hs][];
for (int y = 0; y < Hs; y++)
{
masX[y] = new double[Ws];
}
fillArray2d(masX, Hs, Ws);
}
private void button1_Click(object sender, EventArgs e)
{
Dll_demo_work_cdecl("cpplib.dll", func_name1);
}
private void button2_Click(object sender, EventArgs e)
{
Dll_demo_work_cdecl("delphilib.dll", func_name1);
}
private void button3_Click(object sender, EventArgs e)
{
Dll_demo_work_cdecl("cppblib.dll", func_name1);
}
void show_min_max_avg(double [] times, int sizetimes)
{
double min = times[0];
double max = times[0];
double avg = 0.0;
for (int i = 0; i < sizetimes; i++)
{
avg += times[i];
if (min > times[i]) min = times[i];
if (max < times[i]) max = times[i];
}
avg /= 50.0;
richTextBox1.AppendText("Min: " + min.ToString() + '\n');
richTextBox1.AppendText("Max: " + max.ToString() + '\n');
richTextBox1.AppendText("Avg: " + avg.ToString() + '\n'+'\n');
}
void Dll_demo_work_cdecl(string dll_name, string[] func_name)
{
double[] m1 = new double[size];
double[] m2 = new double[size];
for (int i = 0; i < size; i++)
{
m1[i] = 2 * i * Math.Pow((-1), i);
m2[i] = i;
}
int sizetimes = 50;
double [] times = new double[50];
IntPtr pProc;
richTextBox1.Clear();
IntPtr pDll = LoadLibrary(dll_name);
if (pDll == IntPtr.Zero){
richTextBox1.AppendText("ERROR: unable to load DLL: " + dll_name);
return;
}// Проверка на подключение dll
richTextBox1.AppendText("func (" + dll_name + ") Loaded \n");
pProc = GetProcAddress(pDll, func_name[0]);
if (pProc == IntPtr.Zero){
richTextBox1.AppendText("\nERROR: unable to find DLL function (" + func_name[0] + ")");
}// Проверка на присутсвие функции
else{
richTextBox1.AppendText("func (" + func_name[0] + ") Loaded \n");
FmasProc_v3 sub_arrays = (FmasProc_v3)Marshal.GetDelegateForFunctionPointer(pProc, typeof(FmasProc_v3));
for (int w = 0; w < sizetimes; w++){
QueryPerformanceFrequency(out FFrequence);
QueryPerformanceCounter(out FBeginCount);
int pRes = sub_arrays(mas1, size, mas2, size);
QueryPerformanceCounter(out FEndCount);
Ftime = ((FEndCount - FBeginCount) / (double)FFrequence) * 1000;
times[w] = Ftime;
}
show_min_max_avg(times, sizetimes);
}
pProc = GetProcAddress(pDll, func_name[1]);
if (pProc == IntPtr.Zero){
richTextBox1.AppendText("\nERROR: unable to find DLL function (" + func_name[1] + ")");
}// Проверка на присутсвие функции
else{
richTextBox1.AppendText("func (" + func_name[1] + ") Loaded \n");
FmasProc_v4 find_max_neg_num = (FmasProc_v4)Marshal.GetDelegateForFunctionPointer(pProc, typeof(FmasProc_v4));
for (int w = 0; w < sizetimes; w++){
QueryPerformanceFrequency(out FFrequence);
QueryPerformanceCounter(out FBeginCount);
int pRes = find_max_neg_num(mas1, size, mas2, size);
QueryPerformanceCounter(out FEndCount);
Ftime = ((FEndCount - FBeginCount) / (double)FFrequence) * 1000;
times[w] = Ftime;
}
show_min_max_avg(times, sizetimes);
}
int testst;
pProc = GetProcAddress(pDll, func_name[2]);
if (pProc == IntPtr.Zero){
richTextBox1.AppendText("\nERROR: unable to find DLL function (" + func_name[2] + ")");
}// Проверка на присутсвие функции
else{
richTextBox1.AppendText("library (" + func_name[2] + ") Loaded \n");
Fmas2Proc find_intersections_q = (Fmas2Proc)Marshal.GetDelegateForFunctionPointer(pProc, typeof(Fmas2Proc));
unsafe{
IntPtr[] mas_ptrs1 = new IntPtr[Hs];
for (int i = 0; i < Hs; i++){
fixed (void* ptr = masX[i]){
mas_ptrs1[i] = new IntPtr(ptr);
}
}
for (int w = 0; w < sizetimes; w++){
QueryPerformanceFrequency(out FFrequence);
QueryPerformanceCounter(out FBeginCount);
testst = find_intersections_q(mas_ptrs1, Hs, Ws);
QueryPerformanceCounter(out FEndCount);
Ftime = ((FEndCount - FBeginCount) / (double)FFrequence) * 1000;
times[w] = Ftime;
}
}
show_min_max_avg(times, sizetimes);
}
FreeLibrary(pDll);
}
}
}
При отладки данной ошибки не поялвяется, только в релизе
System.Runtime.InteropServices.ExternalException
HResult=0x80004005
Сообщение = В GDI+ возникла ошибка общего вида.
Источник = System.Drawing
Трассировка стека:
at System.Drawing.Graphics.get_Clip()
at System.Drawing.GraphicsContext..ctor(Graphics g)
at System.Drawing.Graphics.Save()
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Источник: Stack Overflow на русском