Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Moved] qt creator build a dll
QtWS25 Last Chance

[Moved] qt creator build a dll

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wtrich
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Please rephrase and elaborate your question.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        How does the question involve Qt Creator?

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wtrich
          wrote on last edited by
          #4

          @

          //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!

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              wtrich
              wrote on last edited by
              #6

              @
              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?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved