Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PZ.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
2.24 Mб
Скачать

Список используемых источников

  1. http://google.ru/

  2. Электронный on-line справочник MSDN.

  3. Рихтер Д., CLR via C#. Программирование на платформе Microsoft .NET Framework 2.0 на языке С#. – М.: Издательство «Русская Редакция»; СПб.: Питер, 2007. – 656 с.: ил.

  4. Рихтер Д., Создание эффективных Win32-приложений с учётом специфики 64-разрядной версии Windows. – М.: Издательство «Русская Редакция»; СПб.: Питер, 2008. – 743 с.: ил.

Приложение а Листинги программы Приложение а.1 Файл «Program.Cs»

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace Interface

{

static class Program

{

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

}

Приложение а.2 Файл «Message.Cs»

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Interface

{

struct message_type

{

public static byte connect = 0;

public static byte disconnect = 1;

public static byte file = 2;

public static byte file_to_proccessor = 3;

public static byte kill_proccessor = 4;

public static byte error = 5;

};

class message

{

public message(byte _type, string _message_string)

{

type = _type;

message_string = _message_string;

}

public Byte[] Send_formating()

{

return System.Text.Encoding.Default.GetBytes((type).ToString() + message_string);

}

private byte type;

private string message_string;

}

}

Приложение а.3 Файл «Form1.Cs»

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace Interface

{

public partial class Form1 : Form

{

string imagepatch = null;

IntPtr BOX;

byte[] Data = new byte[424];

byte[] RetValue;

uint bytesWritten;

int iMsgSize = 0;

System.Threading.NativeOverlapped stnOverlap = new System.Threading.NativeOverlapped();

#region Импорт API функций

[DllImport("kernel32.dll")]

static extern IntPtr CreateMailslot(string lpName, uint nMaxMessageSize, uint lReadTimeout, IntPtr lpSecurityAttributes);

[DllImport("kernel32.dll")]

static extern bool GetMailslotInfo(IntPtr hMailslot, int lpMaxMessageSize, ref int lpNextSize, IntPtr lpMessageCount, IntPtr lpReadTimeout);

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]

static extern IntPtr CreateFile(string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, [MarshalAs(UnmanagedType.U4)] FileShare fileShare, int securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, int flags, IntPtr template);

[DllImport("kernel32.dll", SetLastError = true)]

private unsafe static extern bool ReadFile(IntPtr hFile, void* lpBuffer, int nNumberOfBytesToRead, int* lpNumberOfBytesRead, int overlapped);

[DllImport("kernel32.dll", SetLastError = true)]

static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, [In] ref System.Threading.NativeOverlapped lpOverlapped);

[DllImport("kernel32.dll", SetLastError = true)]

[return: MarshalAs(UnmanagedType.Bool)]

static extern bool CloseHandle(IntPtr hObject);

#endregion

public Form1()

{

InitializeComponent();

}

private void CONNECT_Click(object sender, EventArgs e)

{

BOX = CreateMailslot("\\\\.\\mailslot\\Interface", (uint)Data.Length, 0, IntPtr.Zero);

if (BOX.ToInt32() == -1)

{

MessageBox.Show("Ошибка создания. Попробуйте чуть позже.");

}

else

{

timer1.Enabled = true;

IntPtr SERVER = CreateFile("\\\\.\\mailslot\\Server", FileAccess.ReadWrite, FileShare.ReadWrite, 0, FileMode.Open, 0, IntPtr.Zero);

message msg = new message(message_type.connect, null);

Byte[] SEND = msg.Send_formating();

WriteFile(SERVER, SEND, (uint)SEND.Length, out bytesWritten, ref stnOverlap);

CONNECT.Enabled = false;

EDIT.Enabled = true;

pictureBox1.Enabled = true;

numericUpDown1.Enabled = true;

}

}

private void pictureBox1_Click(object sender, EventArgs e)

{

openFileDialog1.FileName = "";

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

imagepatch = openFileDialog1.FileName;

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

pictureBox1.Load(imagepatch);

pictureBox1.Enabled = true;

numericUpDown1.Enabled = true;

}

}

private void EDIT_Click(object sender, EventArgs e)

{

if (imagepatch != null)

{

pictureBox1.Image = null;

IntPtr SERVER = CreateFile("\\\\.\\mailslot\\Server", FileAccess.ReadWrite, FileShare.ReadWrite, 0, FileMode.Open, 0, IntPtr.Zero);

message msg = new message(message_type.file, numericUpDown1.Value.ToString() + '$' + imagepatch);

Byte[] SEND = msg.Send_formating();

WriteFile(SERVER, SEND, (uint)SEND.Length, out bytesWritten, ref stnOverlap);

EDIT.Enabled = false;

numericUpDown1.Enabled = false;

}

else

{

MessageBox.Show("Изображение не выбрано");

}

}

private void timer1_Tick(object sender, EventArgs e)

{

int iRead;

GetMailslotInfo(BOX, 0, ref iMsgSize, IntPtr.Zero, IntPtr.Zero);

if (iMsgSize > 0)

{

#region Извлечение сообщения

unsafe

{

fixed (byte* p = Data)

{

ReadFile(BOX, p, iMsgSize, &iRead, 0);

RetValue = new byte[iMsgSize];

System.Array.Copy(Data, RetValue, iMsgSize);

}

}

#endregion

#region Результат подключения

if ((RetValue[0]) == message_type.connect)

{

EDIT.Enabled = true;

pictureBox1.Visible = true;

numericUpDown1.Enabled = true;

}

#endregion

#region Готово

if ((RetValue[0]) == message_type.file)

{

string temp = imagepatch;

int pos = imagepatch.LastIndexOf('.');

temp = imagepatch.Substring(0, pos);

temp += "(копия).";

temp += imagepatch.Substring(pos + 1, imagepatch.Length - pos - 1);

pictureBox1.Load(temp);

EDIT.Enabled = true;

pictureBox1.Enabled = true;

numericUpDown1.Enabled = true;

}

#endregion

#region Ошибка

if ((RetValue[0]) == message_type.error)

{

MessageBox.Show("Ошибка на сервере");

EDIT.Enabled = true;

pictureBox1.Enabled = true;

numericUpDown1.Enabled = true;

}

#endregion

}

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

if (BOX.ToInt32() > 0)

{

DialogResult dialogResult = MessageBox.Show("Вы хотите отключиться?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

switch (dialogResult)

{

case DialogResult.Yes:

IntPtr SERVER = CreateFile("\\\\*\\mailslot\\Server", FileAccess.ReadWrite, FileShare.ReadWrite, 0, FileMode.Open, 0, IntPtr.Zero);

message msg = new message(message_type.disconnect, null);

Byte[] SEND = msg.Send_formating();

WriteFile(SERVER, SEND, (uint)SEND.Length, out bytesWritten, ref stnOverlap);

break;

case DialogResult.No:

e.Cancel = true;

break;

}

}

}

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]