Results 1 to 10 of 10

Thread: How to use Qt inner process communication to return value to native win32 application

  1. #1
    Join Date
    May 2009
    Posts
    83

    Default How to use Qt inner process communication to return value to native win32 application

    I have native win32 application that starts Qt exe application with (probably ) CreateProcess function
    The Qt application doing some work , now when the Qt application done its job I need it somehow to signal back with returned parameter
    String type . what is the best way to implement such thing ?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    it depends to how the "launcher" expects to receive results.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    May 2009
    Posts
    83

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    im flexible because i have no idea what choices do i have?
    i know i can't open port for tcp connection , and that i have native win32 application that need to start Qt application and receive response

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    If you want to return string, why not just output it to standard output and let the parent app read that result?

  5. #5
    Join Date
    May 2009
    Posts
    83

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    any pointers on how to do this?
    you mean to open cmd window from Qt application and output to stdout astring ?
    then read this string from the cmd window? with the win32 application ?

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    Yes, exactly. Since you are going to use CreateProcess API you can create a pipe (WinAPI CreatePipe()), provide that pipes handle to the STARTUPINFO structure of CreateProcess and then just read the pipe with standard WinAPI I/O functions like ReadFile().

  7. #7
    Join Date
    May 2009
    Posts
    83

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    do i have to create cmd window when the Qt application executes ?
    this is something i like to avoid .

  8. #8
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    To use input and output functions you don't need a console. You just need to use the pipe like squidge said.

    But, there's another way too. You can use shared memory. To do this you can use QSharedMemory in the Qt application and in the second process you can use the combination of CreateFileMapping, OpenFileMapping, MapViewOfFile.

    You can find an example here:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    But read the comments, try to change the name to a name that doesn't use the Global namespace.
    Use Local\ or simple don't type use a name that doesn't have the Global\ or Local\ like "myappsharedmem"

    *edit:
    And you have the alternative to create a dll with a shared section. So you load the .dll in the two processes and use a function in the dll that returns a pointer to a shared section variable.

    To have a shared section you just need to do this.

    Qt Code:
    1. #pragma data_seg(".shared")
    2.  
    3. char sharedMemBuf[1024] = ""; //This buffer will be the shared memory used for read/write.
    4.  
    5. #pragma data_seg()
    6.  
    7. char* pointerToSharedMem() // Just get a pointer to the buffer.
    8. {
    9. return sharedMemBuf;
    10. }
    To copy to clipboard, switch view to plain text mode 

    To see the changes in the buffer just add a timer or check if in some event.
    To write just use change it by the normal way.

    You can use a function in the dll that copies memory for write and send a message to registered windows, so you can simple find when the buffer is changed.

    Example:

    Qt Code:
    1. /*Dll code*/
    2.  
    3. #define MYAPP_SHARED_EVENT WM_USER + 36
    4. // Just typed a WM_USER (custom message) and a random number lol
    5.  
    6. vector<HWND> wndList;
    7.  
    8. void registerShareEventForWindow(HWND wndList)
    9. {
    10. wndList.push(wndList);
    11. }
    12.  
    13. void copyMemoryToBuffer(char* data, int size)
    14. {
    15. memcpy(sharedMemBuf, data, size);
    16.  
    17. vector<HWND>::iterator wnds = wndList.begin();
    18. for (; wnds != wndList.end(); wnds++) SendMessage(*wnds, MYAPP_SHARED_EVENT, (WPARAM)sharedMemBuf, 0);
    19.  
    20. // Here i'm using wparam to store the pointer for the buffer.
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /* qt and non-qt application code */
    2.  
    3. switch(msg->message) // use this inside winEvent or wndProc function
    4. {
    5. case MYAPP_SHARED_EVENT:
    6. onSharedBufferChanged((char*)msg->wParam);
    7. break;
    8. }
    To copy to clipboard, switch view to plain text mode 

    NOTE: This isn't thread-safe function so you can use a "wait" system or just only respond to the events without changing the buffer in the same time.

    It's just a normal array pointing to a heap section that's shared between all processes that load that dll.

    To prevent other processes from loading the dll you can just use a signature system to check if it's your process.
    Last edited by rsilva; 6th May 2011 at 13:07.

  9. #9
    Join Date
    May 2009
    Posts
    83

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    Thanks very much for your answer the first solution seams to be easer to implement
    i will try to do that .
    update;
    to try to implement the sharedmemory thing , i was reading the Qsharedmemory tutorial in :
    http://doc.trolltech.com/main-snapsh...redmemory.html
    and as you suggested in :
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    now one thing for start i dont understand in the Qt side with QSharedmemory where i set the name of the memory so that my win32 will access it by name?
    also in the win32 application is there any way to signal when the memory is with value without doing while loop with timer ?
    Last edited by umen; 6th May 2011 at 13:57. Reason: update

  10. #10
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Qt inner process communication to return value to native win32 applica

    You can also use the name pipe like squidge said, it's a easy solution too and I recommend using it, but I don't know how, I never used it ^^.

    -

    About the name of the memory, it's in the tutorial, in the constructor:

    Qt Code:
    1. Dialog::Dialog(QWidget *parent)
    2. : QDialog(parent), sharedMemory("QSharedMemoryExample")
    To copy to clipboard, switch view to plain text mode 

    And from the tutorial:

    "Note that "QSharedMemoryExample" is passed to the QSharedMemory() constructor to be used as the key. This will be used by the system as the identifier of the underlying shared memory segment."

    The "QSharedMemoryExample" is the name that they are using as the name.
    Use the same name that you're using in your win32 app.

    And you question about the signal the changes you can use the CreateEvent function.
    http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

    edit:
    How to do:

    - CreateEvent in the launcher; CreateEvent Function

    - Create a thread to wait the event using WaitForSingleObject; WaitForSingleObject Function

    - In the Qt Process use OpenEvent to get the handle of the event; OpenEvent Function

    - Write data to the SharedMemory.

    - Use SetEvent in the handle, so the WaitForSingleObject will return now; SetEvent Function

    * If you're using a "single-shot" read, in the launcher call CloseHandle in the event.

    * If you want a two-way communication you can set two events and use Wait to monitor the changes, and using Set and Reset Event (ResetEvent Function) to notify that some work was done or need to wait before reading/writing.
    Last edited by rsilva; 6th May 2011 at 23:55.

Similar Threads

  1. Replies: 3
    Last Post: 6th January 2010, 16:55
  2. Native QFileDialog position under Win32
    By no_ghost in forum Qt Programming
    Replies: 0
    Last Post: 28th May 2009, 14:07
  3. Replies: 0
    Last Post: 10th April 2009, 15:28
  4. Open File /w Native Application
    By bpetty in forum Newbie
    Replies: 4
    Last Post: 27th October 2006, 22:19
  5. Inter Process Communication
    By yellowmat in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2006, 11:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.