- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- GIỚI THIỆU TOOL: DUAL MESSENGER TOOLKIT
- [PHẦN MỀM] Giới thiệu Phần mềm Gmap Extractor
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [Phần mềm] Tải và cài đặt phần mềm Sublime Text 4180 full version
- [C#] Hướng dẫn download file từ Minio Server Winform
- [C#] Hướng dẫn đăng nhập zalo login sử dụng API v4 trên winform
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- GIỚI THIỆU TOOL: DUAL MESSENGER TOOLKIT
- [PHẦN MỀM] Giới thiệu Phần mềm Gmap Extractor
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [Phần mềm] Tải và cài đặt phần mềm Sublime Text 4180 full version
- [C#] Hướng dẫn download file từ Minio Server Winform
- [C#] Hướng dẫn đăng nhập zalo login sử dụng API v4 trên winform
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- GIỚI THIỆU TOOL: DUAL MESSENGER TOOLKIT
- [PHẦN MỀM] Giới thiệu Phần mềm Gmap Extractor
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [Phần mềm] Tải và cài đặt phần mềm Sublime Text 4180 full version
- [C#] Hướng dẫn download file từ Minio Server Winform
- [C#] Hướng dẫn đăng nhập zalo login sử dụng API v4 trên winform
[C#] Hướng dẫn truyền dữ liệu giữa hai ứng dụng
![[C#] Hướng dẫn truyền dữ liệu giữa hai ứng dụng](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)
Bài viết hôm nay, mình sẽ hướng dẫn các cách truyền dữ liệu giữa hai ứng dụng khác nhau trong lập trình C#, sử dụng thư viện Win32 (User32.dll).
Trong các bài viết trước, mình đã có bài viết hướng dẫn cách truyền dữ liệu giữa hai form, các bạn có thể tham khảo ở link bên dưới.
[C#] Hướng dẫn sử dụng delegate để truyền dữ liệu giữa 2 form
Giao diện demo ứng dụng truyền dữ liệu:
- Đầu tiên, các bạn tạo một class với tên MessageHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class MessageHelper
{
[DllImport("User32.dll")]
private static extern int RegisterWindowMessage(string lpString);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(int hWnd);
public const int WM_USER = 0x400;
public const int WM_COPYDATA = 0x4A;
//Used for WM_COPYDATA for string messages
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
public bool bringAppToFront(int hWnd)
{
return SetForegroundWindow(hWnd);
}
public int sendWindowsStringMessage(int hWnd, int wParam, string msg)
{
int result = 0;
if (hWnd > 0)
{
byte[] sarr = Encoding.UTF8.GetBytes(msg);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = msg;
cds.cbData = len + 1;
result = SendMessage(hWnd, WM_COPYDATA, wParam, ref cds);
}
return result;
}
public int sendWindowsMessage(int hWnd, int Msg, int wParam, int lParam)
{
int result = 0;
if (hWnd > 0)
{
result = SendMessage(hWnd, Msg, wParam, lParam);
}
return result;
}
public int getWindowId(string className, string windowName)
{
return FindWindow(className, windowName);
}
}
- Viết code cho App 1:
private void txtMessage_TextChanged(object sender, EventArgs e)
{
MessageHelper msg = new MessageHelper();
int result = 0;
int hWnd = msg.getWindowId(null, "WinApp2 - https://laptrinhvb.net");
result = msg.sendWindowsStringMessage(hWnd, 0, txtMessage.Text);
}
- Viết code cho App 2:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_USER:
break;
case WM_COPYDATA:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
byte[] bytes = Encoding.UTF8.GetBytes(mystr.lpData);
string myString = Encoding.UTF8.GetString(bytes);
label1.Text = myString;
break;
}
base.WndProc(ref m);
}
HAPPY CODING
THÔNG TIN TÁC GIẢ
NGUYỄN THẢO
Founder at LaptrinhVB.net
★★★★★
♥ Tình yêu thương chẳng hề hư mất bao giờ. (Cr 13,4)
=========================================================================
My skills includes .NET(C#, VB.NET), DevExpress, Java, Android, PHP,
Python, Sqlserver, Mysql, Reactjs, Dart, Flutter, API services and lot more...
Phone/Zalo/Telegram/WhatsApp: ☎️ (+84)933.913122
Zalo: https://zalo.me/0933913122
Email: nguyenthao.laptrinhvb@gmail.com
My Github: https://github.com/nguyenthao1988
Facebook: https://fb.com/Lewandowski28031988
Youtube Channel: https://www.youtube.com/@thaomeotv

=========================================================================
BÀI VIẾT LIÊN QUAN
![[C#] Hướng dẫn truyền dữ liệu giữa hai ứng dụng](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)