Comunicazione client server con le API di windows postmessage e sendmessage parte client in C++

Posted on at


#pragma once
#include <Windows.h>

#include <vector>
namespace VSIPCMessageClient {
using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace ystem::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;
public ref class MyForm : public System::Windows::Forms::Form

{

public: MyForm(void) {

InitializeComponent();

}
protected: ~MyForm() {

if (components) {

delete components;

}

}

virtual void WndProc(System::Windows::Forms::Message %m) override {

if (m.Msg == WM_COPYDATA) {

COPYDATASTRUCT* dati = (COPYDATASTRUCT*)m.LParam.ToPointer();
std::string messaggio((char*)dati->lpData, dati->cbData);

textBox2->Text = textBox2->Text+"\r\n"+gcnew String(messaggio.c_str());

}
System::Windows::Forms::Form::WndProc(m);

}
private: System::Windows::Forms::Button^  button1;

private: System::Windows::Forms::TextBox^  textBox2;

private: System::ComponentModel::Container ^components;

private: System::Windows::Forms::TextBox^  textBox1;

System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

// cerco la finestra

HWND finestra = FindWindow(NULL, L"Server");

std::string testo;
ConvertiStringa(textBox1->Text, testo);
COPYDATASTRUCT messaggio; //creo il messaggio

messaggio.lpData = (PVOID)testo.c_str();

messaggio.cbData = sizeof(char) * testo.size();

messaggio.dwData = 0;

// invio il messaggio

SendMessage(finestra, WM_COPYDATA, 0, (LPARAM)&messaggio);}

private: void ConvertiStringa(String ^ s, std::string& os) {

using namespace Runtime::InteropServices;

const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();

os = chars;

Marshal::FreeHGlobal(IntPtr((void*)chars)); }
#pragma region Windows Form Designer generated code void initializeComponent(void) {

this->textBox1 = (gcnew System::Windows::Forms::TextBox());

this->button1 = (gcnew System::Windows::Forms::Button());

this->textBox2 = (gcnew System::Windows::Forms::TextBox());

this->SuspendLayout();

// 

// textBox1

//

this->textBox1->Location = System::Drawing::Point(12, 226);

this->textBox1->Name = L"textBox1";

this->textBox1->Size = System::Drawing::Size(246, 20);

this->textBox1->TabIndex = 0;

// 

// button1

// 

this->button1->Location = System::Drawing::Point(264, 223);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(75, 23);

this->button1->TabIndex = 1;

this->button1->Text = L"Invia";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);

// 

// textBox2

// 

this->textBox2->Location = System::Drawing::Point(12, 13);

this->textBox2->Multiline = true;

this->textBox2->Name = L"textBox2";

this->textBox2->Size = System::Drawing::Size(327, 184);

this->textBox2->TabIndex = 2;

// 

// MyForm

// 

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(351, 252);

this->Controls->Add(this->textBox2);

this->Controls->Add(this->button1);

this->Controls->Add(this->textBox1);

this->Name = L"MyForm";

this->Text = L"Client";

this->ResumeLayout(false);

this->PerformLayout();
}

#pragma endregion

};

}



About the author

darkrei

cool filmannex user

Subscribe 0
160