[心得] C++/CLR 使用IntPtr、呼叫Windows API教學(崩潰、懺悔)
↖水☆晶▲顯㊣靈↗
速抄下球裡發出的那個光。
傳原原Po在批踢踢之C_and_CPP版發問此問題:「
為什麼用findwindow找combobox的handler都找不到,可是button卻可以??
」
並於遭到質疑後附上了程式碼片段:「
IntPtr mWin = FindWindow("ThunderRT6FormDC", "POSDLLDemo");
IntPtr mFrame = FindWindowEx(mWin, IntPtr.Zero, "ThunderRT6Frame", "Port Config");
currChild = FindWindowEx(mFrame, prevChild, "ThunderRT6ComboBox", "COM1");
」
神旨傳令快,如有抄錯請見諒。
讓我們重現一下歷史,請看:
首先他說是用C++寫的,好,再來他用到了今天令眾生起蒸液的IntPtr,
也就是說這是一個用到.net Framework,或者說得更精確,是C++/CLI的程式。
如何在Visual Studio上建立一個C++/CLI的專案呢?
首先開啟Visual Studio,按下「New Project」,語言選擇「Visual C++」,
接著從「CLR」建立一個「CLR Console Application」。
http://i.imgur.com/6oPxs8E.png
出來了!傑尼……不對,出來了!程式碼。
http://i.imgur.com/ZREqNEz.png
我們發現原PO使用的攻擊技能是Windows API,
Windows的API都是以C-style導出函數,
並出沒在Windows上各種系統DLL,
比如今天的「FindWindow」以及「FindWindowEx」都是在「user32.dll」中,
怎麼知道神奇寶貝的詳細資料?看圖鑑就對了!
Windows開發人員必備的圖鑑就是「MSDN」,讓我們來看看:
FindowWindow在
https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms633499(v=vs.85).aspx
縮:http://bit.ly/FindWindow
FindWindowEx在
https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms633500(v=vs.85).aspx
縮:http://bit.ly/FindWindowEx
我們將網頁往下滑找到「Requirements」的表格,
就可以看到這裡有說明此API位在哪個DLL,要引用哪個header,支援的系統版本等。
好,查完了圖鑑,就可以開始收服了!
首先在.net中想要呼叫外部DLL的函數,必須用到一項技術叫「Platform invoke」,
有時也會看到「P/Invoke」這樣的寫法,
雖然C++/CLR可以直接#include <Windows.h>來呼叫Windows API,
但是這樣就無法在不用轉型的狀況下重現IntPtr了。
要呼叫Unmanaged DLL提供的函數,必需定義他的名稱、參數,以及DLL,
但首先必須 using namespace System::Runtime::InteropServices;
[DllImport ("user32")]
IntPtr FindWindow(String ^, String ^);
[DllImport ("user32")]
IntPtr FindWindowEx(IntPtr, IntPtr, String ^, String ^);
我已省略參數名稱,如不確定還是可以上MSDN查詢。
接著就可以把程式貼上去看看會發生什麼事了。
https://i.imgur.com/k3P3670.png
看到了嗎?可是,瑞凡,你……
什麼!?「IntPtr.Zero」,這不是C#嗎?
好的,辛苦了這麼久,原來是C#啊,我還以為是C++……除非是用打的筆誤。
完。
後記:
如果是C++的話,應該打「IntPtr::Zero」才對。
附上完整程式碼:https://i.imgur.com/IHnATV4.png
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.8.94.89
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1468873954.A.47B.html
→
07/19 04:33, , 1F
07/19 04:33, 1F
推
07/19 04:39, , 2F
07/19 04:39, 2F
→
07/19 04:40, , 3F
07/19 04:40, 3F
→
07/19 04:40, , 4F
07/19 04:40, 4F
→
07/19 04:40, , 5F
07/19 04:40, 5F
→
07/19 04:43, , 6F
07/19 04:43, 6F
→
07/19 04:46, , 7F
07/19 04:46, 7F
推
07/19 04:51, , 8F
07/19 04:51, 8F
推
07/19 07:24, , 9F
07/19 07:24, 9F
推
07/19 08:00, , 10F
07/19 08:00, 10F
→
07/19 08:00, , 11F
07/19 08:00, 11F
→
07/19 08:01, , 12F
07/19 08:01, 12F
→
07/19 08:04, , 13F
07/19 08:04, 13F
→
07/19 08:04, , 14F
07/19 08:04, 14F
→
07/19 11:56, , 15F
07/19 11:56, 15F
推
07/19 21:05, , 16F
07/19 21:05, 16F
推
07/19 23:09, , 17F
07/19 23:09, 17F
推
07/20 00:03, , 18F
07/20 00:03, 18F
→
07/20 00:03, , 19F
07/20 00:03, 19F
→
07/20 00:03, , 20F
07/20 00:03, 20F
→
07/20 05:06, , 21F
07/20 05:06, 21F
→
07/20 05:07, , 22F
07/20 05:07, 22F
→
07/20 05:07, , 23F
07/20 05:07, 23F
推
07/20 05:15, , 24F
07/20 05:15, 24F
→
07/20 05:39, , 25F
07/20 05:39, 25F