├── .gitignore ├── HelloDM.pro ├── LICENSE ├── README.md ├── dmwrapper.cpp ├── dmwrapper.h ├── main.cpp ├── widget.cpp ├── widget.h └── widget.ui /.gitignore: -------------------------------------------------------------------------------- 1 | HelloDM.pro.user* 2 | -------------------------------------------------------------------------------- /HelloDM.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-10-02T02:24:06 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui axcontainer 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = HelloDM 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | widget.cpp \ 17 | dmwrapper.cpp 18 | 19 | HEADERS += widget.h \ 20 | dmwrapper.h 21 | 22 | FORMS += widget.ui 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Junren Deng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HelloDM 2 | 3 | Qt调用大漠插件演示 4 | 5 | 6 | ## 简单介绍 7 | 8 | 简单演示Qt调用COM接口的方式。 9 | 10 | 至于大漠插件能用来干嘛,你们懂的。 11 | 12 | 大漠免费版的下载和注册请自行搜索,这里不作提供。 13 | 14 | 15 | ## 程序说明 16 | 17 | 主要方法: 18 | 19 | QAxObject::dynamicCall(); 20 | 21 | ### 值得一提的是 22 | 23 | 1. 有些COM方法里带有输出用的参数(引用),用dynamicCall()调用后,变量并没有被改写。 24 | 25 | 暂时没有找到原因,然后可能也不是Qt特有的问题,所以大漠插件针对这种接口,提供了用返回值方式返回数据的封装。 26 | 27 | 比如: FindPic() 和 FindPicE() 28 | 29 | 各位可以看看大漠接口说明。 30 | 31 | 2. 如果读取内存的接口得到都是空数据,可能是需要提升权限。详看main.cpp里的raisePrivilege() 32 | 33 | 3. 记得用32位Qt调用32位的COM。。。不要问我为什么知道。。。 34 | -------------------------------------------------------------------------------- /dmwrapper.cpp: -------------------------------------------------------------------------------- 1 | #include "dmwrapper.h" 2 | 3 | DmWrapper::DmWrapper(const QString &c, QObject *parent) : 4 | QObject(parent) 5 | { 6 | dm = new QAxObject(c, this); 7 | } 8 | 9 | DmWrapper::~DmWrapper() 10 | { 11 | 12 | } 13 | 14 | bool DmWrapper::isLoaded() 15 | { 16 | return ver().length() > 0; 17 | } 18 | 19 | QString DmWrapper::ver() 20 | { 21 | return dm->dynamicCall("Ver()").toString(); 22 | } 23 | 24 | int DmWrapper::getLastError() 25 | { 26 | return dm->dynamicCall("GetLastError()").toInt(); 27 | } 28 | 29 | int DmWrapper::getMousePointWindow() 30 | { 31 | return dm->dynamicCall("GetMousePointWindow()").toInt(); 32 | } 33 | 34 | QString DmWrapper::getWindowTitle(int hWnd) 35 | { 36 | return dm->dynamicCall("GetWindowTitle(int)", hWnd).toString(); 37 | } 38 | 39 | bool DmWrapper::bindWindow(int hwnd, const QString &display, const QString &mouse, const QString &keypad, int mode) 40 | { 41 | return dm->dynamicCall("BindWindow(int, QString, QString, QString, int)", hwnd, display, mouse, keypad, mode).toInt() == 1; 42 | } 43 | 44 | bool DmWrapper::setPath(const QString &path) 45 | { 46 | return dm->dynamicCall("SetPath(QString)", path).toInt() == 1; 47 | } 48 | 49 | bool DmWrapper::loadPic(const QString &picName) 50 | { 51 | return dm->dynamicCall("LoadPic(QString)", picName).toInt() == 1; 52 | } 53 | 54 | bool DmWrapper::freePic(const QString &picName) 55 | { 56 | return dm->dynamicCall("FreePic(QString)", picName).toInt() == 1; 57 | } 58 | 59 | int DmWrapper::findPic(int x1, int y1, int x2, int y2, const QString &picName, int &outX, int &outY, int dir, double sim, const QString &deltaColor) 60 | { 61 | QString dmRet = dm->dynamicCall("FindPicE(int, int, int, int, QString, QString, double, int)", x1, y1, x2, y2, picName, deltaColor, sim, dir).toString(); 62 | if (!dmRet.isEmpty() && dmRet.split("|").size() == 3) { 63 | QStringList retList = dmRet.split("|"); 64 | int index = retList.at(0).toInt(); 65 | outX = retList.at(1).toInt(); 66 | outY = retList.at(2).toInt(); 67 | return index; 68 | } else { 69 | outX = -1; 70 | outY = -1; 71 | return -1; 72 | } 73 | } 74 | 75 | int DmWrapper::findPic(const QRect &rect, const QString &picName, QPoint &outPoint, int dir, double sim, const QString &deltaColor) 76 | { 77 | int outX = -1; 78 | int outY = -1; 79 | int index = findPic(rect.left(), rect.top(), rect.right(), rect.bottom(), picName, outX, outY, dir, sim, deltaColor); 80 | outPoint.setX(outX); 81 | outPoint.setY(outY); 82 | 83 | return index; 84 | } 85 | 86 | bool DmWrapper::moveTo(int x, int y) 87 | { 88 | return dm->dynamicCall("MoveTo(int, int)", x, y).toInt() == 1; 89 | } 90 | 91 | bool DmWrapper::moveTo(const QPoint &pos) 92 | { 93 | return moveTo(pos.x(), pos.y()); 94 | } 95 | 96 | bool DmWrapper::leftClick() 97 | { 98 | return commonMethod("LeftClick()"); 99 | } 100 | 101 | bool DmWrapper::leftClick(int x, int y) 102 | { 103 | if (moveTo(x, y)) { 104 | return leftClick(); 105 | } 106 | return false; 107 | } 108 | 109 | bool DmWrapper::leftClick(const QPoint &pos) 110 | { 111 | return leftClick(pos.x(), pos.y()); 112 | } 113 | 114 | bool DmWrapper::rightClick() 115 | { 116 | return commonMethod("RightClick()"); 117 | } 118 | 119 | QString DmWrapper::readString(int hwnd, const QString &address, int type, int len) 120 | { 121 | return dm->dynamicCall("ReadString(int, QString, int, int)", hwnd, address, type, len).toString(); 122 | } 123 | 124 | bool DmWrapper::commonMethod(const QString &method) 125 | { 126 | return dm->dynamicCall(method.toLatin1().data()).toInt() == 1; 127 | } 128 | 129 | -------------------------------------------------------------------------------- /dmwrapper.h: -------------------------------------------------------------------------------- 1 | #ifndef DMWRAPPER_H 2 | #define DMWRAPPER_H 3 | 4 | #include 5 | #include 6 | 7 | class DmWrapper : public QObject 8 | { 9 | Q_OBJECT 10 | 11 | public: 12 | explicit DmWrapper(const QString &c, QObject *parent = 0); 13 | ~DmWrapper(); 14 | 15 | bool isLoaded(); 16 | 17 | /**! 18 | * dm COM methods 19 | * 20 | */ 21 | // string Ver() 22 | QString ver(); 23 | 24 | // long GetLastError() 25 | int getLastError(); 26 | 27 | // long GetMousePointWindow() 28 | int getMousePointWindow(); 29 | 30 | // string GetWindowTitle(hwnd) 31 | QString getWindowTitle(int hWnd); 32 | 33 | // long BindWindow(hwnd,display,mouse,keypad,mode) 34 | bool bindWindow(int hwnd, const QString &display = "normal", const QString &mouse = "windows", const QString &keypad = "windows", int mode = 0); 35 | 36 | // long SetPath(path) 37 | bool setPath(const QString &path); 38 | 39 | // long LoadPic(pic_name) 40 | bool loadPic(const QString &picName); 41 | 42 | // long FreePic(pic_name) 43 | bool freePic(const QString &picName); 44 | 45 | // string FindPic(x1, y1, x2, y2, pic_name, delta_color,sim, dir) 46 | int findPic(int x1, int y1, int x2, int y2, const QString &picName, int &outX, int &outY, int dir = 0, double sim = 0.9, const QString &deltaColor = "000000"); 47 | int findPic(const QRect &rect, const QString &picName, QPoint &outPoint, int dir = 0, double sim = 0.9, const QString &deltaColor = "000000"); 48 | 49 | // long MoveTo(x,y) 50 | bool moveTo(int x, int y); 51 | bool moveTo(const QPoint &pos); 52 | 53 | // long LeftClick() 54 | bool leftClick(); 55 | bool leftClick(int x, int y); 56 | bool leftClick(const QPoint &pos); 57 | 58 | // long RightClick() 59 | bool rightClick(); 60 | 61 | // string ReadString(hwnd,addr,type,len) 62 | QString readString(int hwnd, const QString &address, int type, int len); 63 | 64 | private: 65 | // common methods without parameters(and return 0 for failed, 1 for success) 66 | bool commonMethod(const QString &method); 67 | 68 | private: 69 | QAxObject *dm; 70 | }; 71 | 72 | #endif // DMWRAPPER_H 73 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | #include 3 | 4 | #include 5 | 6 | static void raisePrivilege() 7 | { 8 | // to read memory of some processes, we need this 9 | HANDLE hToken; 10 | TOKEN_PRIVILEGES tp; 11 | tp.PrivilegeCount = 1; 12 | tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 13 | if(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken)) { 14 | if(LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid)) { 15 | AdjustTokenPrivileges(hToken, FALSE, &tp, NULL, NULL, 0); 16 | } 17 | } 18 | if(hToken) { 19 | CloseHandle(hToken); 20 | } 21 | } 22 | 23 | int main(int argc, char *argv[]) 24 | { 25 | raisePrivilege(); 26 | 27 | QApplication a(argc, argv); 28 | Widget w; 29 | w.show(); 30 | 31 | return a.exec(); 32 | } 33 | -------------------------------------------------------------------------------- /widget.cpp: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | #include "ui_widget.h" 3 | #include "dmwrapper.h" 4 | 5 | #include 6 | #include 7 | 8 | Widget::Widget(QWidget *parent) : 9 | QWidget(parent), 10 | ui(new Ui::Widget), 11 | dm(0) 12 | { 13 | ui->setupUi(this); 14 | } 15 | 16 | Widget::~Widget() 17 | { 18 | delete ui; 19 | } 20 | 21 | void Widget::keyPressEvent(QKeyEvent *event) 22 | { 23 | Q_UNUSED(event); 24 | 25 | if (!dm) { 26 | return; 27 | } 28 | 29 | if (event->key() == Qt::Key_F1) { 30 | int hWnd = dm->getMousePointWindow(); 31 | QString title = dm->getWindowTitle(hWnd); 32 | ui->lineEdit_hwnd->setText(title); 33 | } 34 | } 35 | 36 | void Widget::on_pushButton_loadDm_clicked() 37 | { 38 | if (dm) { 39 | return; 40 | } 41 | 42 | // load ActiveX 43 | CoInitializeEx(NULL, COINIT_MULTITHREADED); 44 | dm = new DmWrapper(ui->lineEdit_dmClsId->text(), this); 45 | 46 | if (dm->isLoaded()) { 47 | QMessageBox::information(this, "DM", QString("dm is loaded, version: %1").arg(dm->ver())); 48 | 49 | // disable 50 | ui->pushButton_loadDm->setEnabled(false); 51 | // enable others 52 | ui->lineEdit_hwnd->setEnabled(true); 53 | } else { 54 | QMessageBox::warning(this, "DM", "can not load dm, please make sure it's registered"); 55 | delete dm; 56 | dm = 0; 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /widget.h: -------------------------------------------------------------------------------- 1 | #ifndef WIDGET_H 2 | #define WIDGET_H 3 | 4 | #include 5 | #include 6 | 7 | #include "dmwrapper.h" 8 | 9 | namespace Ui { 10 | class Widget; 11 | } 12 | 13 | class Widget : public QWidget 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit Widget(QWidget *parent = 0); 19 | ~Widget(); 20 | 21 | protected: 22 | void keyPressEvent(QKeyEvent *event); 23 | 24 | private slots: 25 | void on_pushButton_loadDm_clicked(); 26 | 27 | private: 28 | Ui::Widget *ui; 29 | 30 | DmWrapper *dm; 31 | }; 32 | 33 | #endif // WIDGET_H 34 | -------------------------------------------------------------------------------- /widget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Widget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 499 10 | 133 11 | 12 | 13 | 14 | DM 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 大漠CLSID: 25 | 26 | 27 | 28 | 29 | 30 | 31 | 窗口: 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | false 41 | 42 | 43 | 44 | 1 45 | 0 46 | 47 | 48 | 49 | 50 | 51 | 52 | false 53 | 54 | 55 | 56 | 57 | 58 | 59 | * 鼠标移到其他窗口,按下F1获取窗口标题 60 | 61 | 62 | 63 | 64 | 65 | 66 | Qt::Horizontal 67 | 68 | 69 | 70 | 40 71 | 20 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | {26037A0E-7CBD-4FFF-9C63-56F2D0770214} 84 | 85 | 86 | 87 | 88 | 89 | 90 | 加载大漠 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | Qt::Vertical 104 | 105 | 106 | 107 | 20 108 | 40 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | --------------------------------------------------------------------------------