• 7611阅读
  • 4回复

两个程序之间如何通信? [复制链接]

上一主题 下一主题
离线sunqing
 

只看楼主 倒序阅读 楼主  发表于: 2009-03-16
— 本帖被 XChinux 执行加亮操作(2009-03-16) —
麻烦各位达人!
我用vc++写一个程序,需要给一个QT程序发消息,让这个QT程序做某一件事情,我在QT的那个程序里如何操作?
如:vc++程序main.exe,给一个QT程序uploadFile.exe发消息:
  1. HWND hUpLoad = ::FindWindow(NULL, "上传文件工具");
  2. if (hUpLoad) //我们假设已经找到了
  3. {    
  4.    ::PostMessageBox(WM_UPLOADFILE, wParam, lParam);
  5. }

我把消息发过去了,在QT的那个程序应该如何怎么接收、处理这个消息呢?
谢谢!

----------------------------------------------------------------------------------------------------
今天我把问题解决了,因为这只有业余时间才有能研究一下,所以拖了好久。我用的IDE是Eclipse,在调试的时候总是会莫名其妙的崩溃,
是IDE自己就退出了,所以调了很多次都调不好。
这里给出解决方法,希望给需要的朋友一点提示,有不妥之处还请指出,大家共同进步.
就是使用bool winEvent ( MSG * message, long * result);
这里要感谢楼下的兄弟。
在头文件里增加这个函数的声明。以下是定义。
  1. bool TestWindowsMSG::winEvent( MSG * message, long * result){
  2. pWIN_MSG msg = new WIN_MSG;
  3. if (!msg) return false;
  4. memcpy(msg, message, sizeof(WIN_MSG)); // 这里无法直接得到入口参数message的成员变量,不知道怎么回事
  5. //但调试的时候却能在Variables里看到成员变量的值
  6. if (WM_FROM_MFC == msg->message){ // 前面的消息是我在头文件里定义的一个,需要在1000与65535之间
  7. QMessageBox::information(this, tr("Message"), tr("Receive message from other program!"));
  8. return true;
  9. }
  10. delete msg;
  11. return false;
  12. }这里的pWIN_MSG是一个结构体:
  13. typedef struct _WIN_MSG_{ //这里与MFC的定义是一样的
  14. HWND hwnd;
  15. UINT message;
  16. WPARAM wParam;
  17. LPARAM lParam;
  18. DWORD time;
  19. POINT pt;
  20. }WIN_MSG, *pWIN_MSG;

----------------------------------------------------------------------------------------------------
[ 此帖被sunqing在2009-03-24 09:31重新编辑 ]
I am starving!
只看该作者 1楼 发表于: 2009-03-16
Qt provides several ways to implement Inter-Process Communication (IPC) in Qt applications.


  1. D-Bus The QtDBus module is a Unix-only library you can use to implement IPC using the D-Bus protocol. It extends Qt's Signals and Slots mechanism to the IPC level, allowing a signal emitted by one process to be connected to a slot in another process. This Introduction to D-Bus page has detailed information on how to use the QtDBus module.
  2. TCP/IP The cross-platform QtNetwork module provides classes that make network programming portable and easy. It offers high-level classes (e.g. QHttp, QFtp) that implement specific application-level protocols, and lower-level classes (e.g. QTcpSocket, QTcpServer, QSslSocket) for implementing protocols.
  3. Shared Memory The cross-platfrom shared memory class (QSharedMemory) provides access to the operating system's shared memory implementation. It allows safe access to shared memory segments by multiple threads and processes.
  4. Qt COmmunications Protocol (QCOP) The QCopChannel class implements a protocol for transferring messages between client processes across named channels. QCopChannel is only available in Qt for Embedded Linux. Like the QtDBus module, QCOP extends Qt's Signals and Slots mechanism to the IPC level, allowing a signal emitted by one process to be connected to a slot in another process, but unlike QtDBus, QCOP does not depend on a 3rd party library.
1只能在linux上,4只能在Embedded上,3是数据共享,老老实实用2吧

要么你用这个
bool QWidget::winEvent ( MSG * message, long * result )   [virtual protected]
This special event handler can be reimplemented in a subclass to receive native Windows events which are passed in the message parameter.
In your reimplementation of this function, if you want to stop the event being handled by Qt, return true and set result to the value that the window procedure should return. If you return false, this native event is passed back to Qt, which translates the event into a Qt event and sends it to the widget.
[ 此帖被都市无名者在2009-03-16 15:05重新编辑 ]
离线sunqing

只看该作者 2楼 发表于: 2009-03-16
那第二个不太适合,我试试那个QWidget::winEvent
谢谢!
I am starving!
离线fq1986614
只看该作者 3楼 发表于: 2010-04-12
有谁知道  怎么从Qt 中 发送 信号到 vs编写的程序中啊, 就是反过来 我就不知道了~
离线luoyes

只看该作者 4楼 发表于: 2010-04-13
感兴趣,先mark了
快速回复
限100 字节
 
上一个 下一个