如何从Qt中的另一个应用程序调用DLL
How to call DLL from another application in Qt
我是Qt开发和开发启动TCP Server的Qt DLL的新手。当我从另一个应用程序调用dll时,它不会收到任何新的连接套接字。因此,如果我做错了任何步骤,请指导我。
服务器.h
extern "C" SERVERSHARED_EXPORT void CallServer();
class SERVERSHARED_EXPORT Server : public QObject
{
Q_OBJECT
public:
Server();
void CallServer();
void CallServer1();
QTcpServer *server;
QTcpSocket *socket ;
signals:
public slots:
void myConnection();
void closingClient();
};
服务器.cpp
Server::Server()
{
}
void CallServer()
{
Server server_Obj;
server_Obj.CallServer1();
while (true)
::sleep(1000);
}
void Server::CallServer1()
{
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()),this, SLOT(myConnection()));
QHostAddress hostadd(ServerIP);
qDebug() << ServerIP;
qDebug() << Port;
if(!server->listen(hostadd,Port.toInt())) qDebug() << "nWeb server could not start";
else qDebug() <<"nWeb server is waiting for a connection";
}
void Server::myConnection()
{
qDebug() << "Detected Connection";
QByteArray Rdata;
socket = server->nextPendingConnection();
qDebug() << "Wait for connect = " << socket->waitForConnected();
while (socket->waitForReadyRead(10))
{
while(socket->bytesAvailable() > 0)
{
Rdata.append(socket->readAll());
}
}
qDebug() << "Final Testing is size = " << Rdata.size();
qDebug() << "Final Testing is" << Rdata;
}
.pro 文件
QT += core
QT += network
QT += widgets
QT -= gui
TARGET = Server
TEMPLATE = lib
DEFINES += SERVER_LIBRARY
SOURCES += server.cpp
HEADERS += server.h
server_global.h
另一个应用程序:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QLibrary library("Server.dll");
if (!library.load())
qDebug() << library.errorString();
if (library.load())
qDebug() << "library loaded";
typedef int(*pf)(void);
pf cwf = (pf)library.resolve("CallServer");
if (cwf) {
int x = cwf();
} else {
qDebug() << "Could not show widget from the loaded library";
}
qDebug() << "After call";
return a.exec();
}
看起来它不起作用,因为您以异步(信号/插槽)方式使用 QTcpSocket(您的服务器类)而没有事件循环 - 所以它不起作用。您应该添加事件循环或以阻塞方式使用套接字。有关更多详细信息,请查看 - http://doc.qt.io/qt-5/qtnetwork-blockingfortuneclient-example.html
相关文章:
- 如何将一个窗口保留在另一个应用程序窗口的前面
- 将库链接到另一个应用程序时,CMAKE 找不到库
- 调试控制台 ChessEngine.exe "used"在另一个应用程序(国际象棋 GUI)中
- 在 Windows 10 中将一个应用程序放在另一个应用程序前面
- 将数据从一个应用程序单向发送到另一个应用程序的最佳做法
- 当电子的窗口隐藏时如何激活另一个应用程序窗口
- 与 Python 中子进程中的另一个应用程序的交互式会话
- 如何将Qt嵌入到另一个应用程序中(非阻塞方式)
- 调试从 Linux 中的另一个C++应用程序启动的 python 脚本
- 另一个应用程序用 c++ 在 Windows 上的线程中破坏 QTimer
- 发送消息WM_SETTEXT不适用于 MFC 应用程序的另一个应用程序
- 在Qt和C++中从另一个应用程序启动一个应用软件会产生问题
- 在另一个应用程序中嵌入python时,如何在子模块(即scipy.optimize.nnls)中导入或调用函数
- 如何从Windows上的Windows API上的另一个应用程序获得应用程序宽度和高度
- QT应用程序在OSX中启动另一个应用程序
- mfc c++通过编辑控件设置另一个应用程序的HWND地址
- 并行运行一个应用程序,同时用 C++ 启动另一个应用程序
- 以弹出窗口的形式启动另一个应用程序
- 如何将消息从一个 Windows 控制台应用程序发送到另一个应用程序
- 从另一个应用程序读取状态栏