[Moved] qt creator build a dll



  • Inside the network connections of Qt and thread made ​​of a DLL library, use the code to use the Qt library can listen to the connection and send data, but on the inside C # to call to die, do not know why? Which can help me? Thank you!



  • Please rephrase and elaborate your question.


  • Moderators

    How does the question involve Qt Creator?



  • @

    //qtcpdll.pro
    QT += network core

    QT -= gui

    TARGET = qtcpdll
    TEMPLATE = lib

    DEFINES += QTCPDLL_LIBRARY

    SOURCES += qtcpdll.cpp
    thread.cpp

    HEADERS += qtcpdll.h
    thread.h

    ///////////////////////////////////////////////////////////////
    //qtcpdll.h
    #ifndef QTCPDLL_H
    #define QTCPDLL_H

    #define MYDLLEXPORT extern "C" __declspec(dllexport)

    MYDLLEXPORT int start();

    #endif // QTCPDLL_H

    /////////////////////////////////////////////////////////////
    //thread.h
    #ifndef THREAD_H
    #define THREAD_H

    #include <QThread>

    class thread : public QThread
    {
    Q_OBJECT
    public:
    explicit thread(QObject *parent = 0);

    void run();
    

    };

    #endif // THREAD_H

    /////////////////////////////////////////////////////////////
    //qtcpdll.cpp
    #include "qtcpdll.h"

    #include "thread.h"

    #include <QtCore/QCoreApplication>

    thread *t;

    MYDLLEXPORT int start()
    {
    // int r = 1;
    // QCoreApplication a(r, 0);
    t = new thread;

    t->start();
    
    return 9999999;
    

    // return a.exec();
    }

    ////////////////////////////////////////////////////////////////
    //thread.cpp

    #include "thread.h"

    thread::thread(QObject *parent) :
    QThread(parent)
    {
    qWarning("thread is constructed!");
    }

    void thread::run()
    {
    for(int i = 0; i < 1000; i++)
    {
    qWarning("%d", i);
    //sleep(1000);
    }
    }
    @

    This is an example I wrote, ready to call in C # inside.
    I call that in C # which exported functions, the thread can not function properly.
    thanks!



  • As said, please elaborate your question. Are there any error messages or debug messages? Does the application crash? Does your thread even start? How does your C# application look like? Does your code work when called from C++, not C#?

    "thread can not function properly" is no useful piece of information.



  • @
    C# code
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace ConsoleApplication1
    {
    public static class Test
    {
    [DllImport("qtcpdll.dll", EntryPoint="start", CallingConvention=CallingConvention.Cdecl)]
    public extern static int start();
    }
    }

    ///////////////////////////////////////////

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace ConsoleApplication1
    {
    class Program
    {

        static void Main(string[] args)
        {
            int res = Test.start();
    
            Console.WriteLine(res);
    
            Console.Read();
         }
     }
    

    }

    ///////////////////////////////////////////
    QT CODE
    tcpserver

    #ifndef SERVER_H
    #define SERVER_H

    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>

    class server : public QObject
    {
    Q_OBJECT
    public:
    explicit server(QObject *parent = 0);

    QTcpServer *tserver;
    QTcpSocket *socket;
    

    public slots:
    void newConnect();
    void receiveMSG();

    };

    #endif // SERVER_H

    //server.cpp
    #include "server.h"

    server::server(QObject *parent) :
    QObject(parent)
    {
    tserver = new QTcpServer;
    if(tserver->listen(QHostAddress::Any, 9999))
    qWarning("listing on 9999");

    connect(tserver, SIGNAL(newConnection()), this, SLOT(newConnect()));
    

    }

    void server::newConnect()
    {
    socket = tserver->nextPendingConnection();
    connect(socket, SIGNAL(readyRead()), this, SLOT(receiveMSG()));
    }

    void server::receiveMSG()
    {
    QByteArray data;
    data.resize(socket->bytesAvailable());
    socket->read(data.data(), data.size());
    qWarning("receive# %s", data.data());
    }

    @
    Thread running normally now. I was in the project which added a "tcpserver", used to monitor the connection and receive messages. In C # to call when the client "socket" shows the state is "already connected", but in C # where not receive data, "incomingConnection" function has not been called. If I use QT to use the DLL functions can be done, then, C # in the die. Is no event loop ah?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.