顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2011年5月1日

在 C# 中取得 process 中載入的 dll的相關資訊

其實是為了某個最近剛出沒多久的 SLG 遊戲… 雖然有 SSG 檔,但是少一個插座很不方便有些地方很不方便,所以才想要自己寫一個,不過雖然參考 SSG 可以抓位置,但是實際上連 SSG 都還看不是很懂呢… 總之先備份一下… 感覺之前寫其他遊戲的修改器也常用到

processPtr = FindWindow(@"AliceStartWindowClass", null);
GetWindowThreadProcessId(processPtr, out ProcessId);
ProcessHandle = OpenProcess(0x1F0FFF, true, ProcessId);

EnumProcessModules(ProcessHandle, ModuleHandles, 20, out requireSize);
ModuleHandles = new IntPtr[requireSize/4];
EnumProcessModules(ProcessHandle, ModuleHandles, requireSize, out requireSize);

// 剛好最後一個 dll 是我要找的那個
dllHandle = ModuleHandles[requireSize/4 - 1];

GetModuleFileNameEx(processPtr, dllHandle, fileName, 260);
GetModuleInformation(ProcessHandle, dllHandle, out ModInfo, 12);

基本上是參考這篇 VB 的改寫而來的…

接下來就是要看懂 SSG 了啊~
--
C# 中引用 win32 api 的 list

2008年7月30日

Training:Socket Programming on C#

教育訓練第二彈,下午的課程就是在教如何使用 .NET 架構寫一個 Socket 的程式。此範例是使用 C#。 所有程式碼可從這邊取得。 (不過程式碼有點亂…而且沒有加註解,請斟酌使用。) Server 端 using System; using System.Collections.Generic; using System.Text; // include Socket library using System.Net; using System.Net.Sockets; namespace tcpServerStarter { class Program { static void Main(string[] args) { // 建立 tcpServer 物件並啟動 server tcpServer server = new tcpServer(IPAddress.Any, 520); server.startServer(); } } // tcpServer Class public class tcpServer { IPAddress _ip; // 要 listen 的 ip int _port; // 要 listen 的 port bool _serverAlive = false; // 檢查 server 是否還存活 // tcpServer construtor public tcpServer(IPAddress ip, int port) { this._ip = ip; this._port = port; } // 啟動 server 的 method public void startServer() { 將 _serverAlive 的 flag 設為 true _serverAlive = true; try { // 建立一個 TcpListener 的物件,並且 listen 在 _ip 與 _port TcpListener listen = new TcpListener(this._ip, this._port); // 開始 listen listen.Start(); // 如果 Server 還活著 while (_serverAlive) { // 取得由 TcpListener 得到的 TcpClient TcpClient tcpClient = listen.AcceptTcpClient(); // 由 TcpClient 建立一個 NetworkStream NetworkStream netStream = tcpClient.GetStream(); // 建立一個大小為 1024 的 byte 陣列來放收到的資料 byte[] reciBuffer = new byte[1024]; int count = 0; // 將經由 Stream 讀取道的資料放入 readBuffer 中 // 並且將長度存到 count while ((count = netStream.Read(readBuffer, 0, readBuffer.Length)) != 0) { // 將 byte 陣列轉成 string string reciStr = System.Text.Encoding.UTF8.GetString(reciBuffer, 0, count); Console.WriteLine("recieve data:" + reciStr); // 傳出的字串 = 收到的字串 string sendStr = reciStr; // 將 string 轉成 byte 陣列 byte[] sendBuffer = System.Text.Encoding.UTF8.GetBytes(sendStr); // 將資料送出 netStream.Write(sendbuffer, 0, sendBuffer.Length); Console.WriteLine("send data:" + sendstr); } } } catch { } } } } Client 端 建立 TextBox1、TextBox2、TextBox3、Button1 TextBox1 為要連接的 server ip TextBox2 為要連接的 port TextBox3 為從 server 收到的資料 Button1 為連接 server 的按鈕 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; // include Socket Library using System.Net; using System.Net.Sockets; namespace tcpClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 建立一個 TcpClient 物件連接到 ip, port TcpClient client = new TcpClient(textBox1.Text, int.Parse(textBox2.Text)); // 由 TcpClient 建立一個 Stream NetworkStream netStream = client.GetStream(); // 自訂要送出的資料為 123456 string sendMsg = "123456"; // 將字串轉為 byte 陣列 byte[] sendBuffer = System.Text.Encoding.UTF8.GetBytes(sendMsg); // 送出給 server netStream.Write(sendBuffer, 0, sendBuffer.Length ); // 建立一個大小為 1024 的 byte 陣列來放收到的資料 byte[] readBuffer = new byte[1024]; // 讀取從 server 來的資料並放到 readBuffer 中 netStream.Read(readBuffer, 0, readBuffer.Length); // 將 byte 陣列轉為 string string readStr = System.Text.Encoding.UTF8.GetString(readBuffer, 0, readBuffer.Length); // 將讀取到的字串顯示在 textBox3 textBox3.Text = readStr; } } }