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

Posted on at


#pragma once
#include <Windows.h>

#include <string>

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

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::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::TextBox^  textBox1;

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

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

System::ComponentModel::Container ^components;
System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

// cerco la finestra

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

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->textBox2 = (gcnew System::Windows::Forms::TextBox());

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

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

this->SuspendLayout();

// 

// textBox2

// 

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

this->textBox2->Multiline = true;

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

this->textBox2->Size = System::Drawing::Size(329, 152);

this->textBox2->TabIndex = 0;

// 

// textBox1

// 

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

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

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

this->textBox1->TabIndex = 1;

// 

// button1

// 

this->button1->Location = System::Drawing::Point(267, 188);

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

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

this->button1->TabIndex = 2;

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

this->button1->UseVisualStyleBackColor = true;

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

// 

// MyForm

// 

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

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

this->ClientSize = System::Drawing::Size(353, 219);

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

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

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

this->Name = L"MyForm";

this->Text = L"Server";

this->ResumeLayout(false);

this->PerformLayout();
}

#pragma endregion

};

}



About the author

darkrei

cool filmannex user

Subscribe 0
160