Solved

Message between apps

Posted on 2000-04-17
13
214 Views
Last Modified: 2010-04-10
Problem:
console app and win32 app (which may incl MFC) must communicate.

ex. win32 sends cmd A to console app, console app responds cmd A complete, etc...
Note the commands are nothing more than a character.

I have looked at the _pipe syntax but am unsure.
I have decided if all else fails, I can just use a file.

I will increase pts if this is more complex than I expect.
0
Comment
Question by:Paullkha
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

by:xLs
ID: 2723637
pipes is quite a good choise.

you could write a nice pipeclass that handles the comunications using
CreateNamedPipe(..);
PeekNamedPipe(..);
ReadFile(..);
WriteFile(..);

When you have a nice clas you will derive that, and add your application specific messages and calls.

i hope this will help you decide.

if you need more help just give us a note :)

0
 

Expert Comment

by:yuxiliu
ID: 2723652
Pipe only works in NT
0
 

Expert Comment

by:yuxiliu
ID: 2723685
Its a IPC problem, there are many way you can use. Mutex, Semorphone, Event and Memory map.You can see some samples in MSDN or if you'd like I can give you some very simple sample later
0
 
LVL 2

Expert Comment

by:xLs
ID: 2723762
pipes works under win9x. and i dont see the use of using mutex/events and semarphores. those are process/thread based and might work if you obtain eachothers process, but that wont work under win2k the same way since.

yuxily if you have some more info how mutex/semaphores and events can be usefull in this issue, please explain , it might be interesting.
0
 
LVL 2

Author Comment

by:Paullkha
ID: 2724824
I'll look in to it tommorrow at work xls.
0
 
LVL 1

Expert Comment

by:mahno
ID: 2725851
Hi Paullkha,

U can also use a registred messages:
UINT mymsg = RegisterWindowMessage("RM_NAME");
....and so on...

mahno


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Accepted Solution

by:
mayab earned 50 total points
ID: 2726860
1. File mapping is the association of a file's contents with a portion of the virtual address space of a process.
The system creates a file-mapping object to maintain this association. A file view is the portion of virtual
address space that the process uses to access the file's contents. Processes read from and write to the file
view using pointers, just as they would with dynamically allocated memory. Processes can also manipulate the
file view with the virtual memory function VirtualProtect.
The file-mapping functions allow a process to create file-mapping objects and file views to easily access and
share data. The following illustration shows the relationship between the file on disk, a file-mapping object,
and a file view.
The file on disk can be any file that you want to map into memory, or it can be the system pagefile.
The file-mapping object can consist of all or only part of the file. It is backed by the file on disk.
This means that when the system swaps out pages of the file-mapping object, any changes made to the file-mapping
object are written to the file. When the pages of the file-mapping object are swapped back in, they are restored
from the file.
A file view can consist of all or only part of the file-mapping object. A process manipulates the file through
the file views. A process can create multiple views for a file-mapping object.
Windows NT/2000: The file views created by each process reside in the virtual address space of that process.
Windows 95: All file views reside in the shared address space. The shared address space exists in the
range between 2 and 3 gigabytes in the virtual address space for each process. It contains the 16-bit heap and
shared system DLLs, as well as file views.
When multiple processes use the same file-mapping object to create views for a local file, the data is coherent.
That is, the views contain identical copies of the file on disk. The file cannot reside on a remote computer
if you want to share memory between multiple processes.

using this method:

Creating Named Shared Memory
The first process calls the CreateFileMapping function to create a file-mapping object and give it the name MyFileMappingObject. By using the PAGE_READWRITE flag, the processes will have read/write permission to the memory through any file views that are created.

HANDLE hMapFile;

hMapFile = CreateFileMapping(hFile,    // Current file handle.
    NULL,                              // Default security.
    PAGE_READWRITE,                    // Read/write permission.
    0,                                 // Max. object size.
    0,                                 // Size of hFile.
    "MyFileMappingObject");            // Name of mapping object.
 
if (hMapFile == NULL)
{
    ErrorHandler("Could not create file-mapping object.");
}
The process then uses the file-mapping object handle returned by CreateFileMapping in the call to MapViewOfFile
to create a view of the file in the process's address space. The MapViewOfFile function returns a pointer to the
file view.

LPVOID lpMapAddress;
lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
    FILE_MAP_ALL_ACCESS,               // Read/write permission
    0,                                 // Max. object size.
    0,                                 // Size of hFile.
    0);                                // Map entire file.
 
if (lpMapAddress == NULL)
{
    ErrorHandler("Could not map view of file.");
}
The second process calls the OpenFileMapping function with the name MyFileMappingObject to use the same file-mapping object as the first process. Like the first process, the second process uses the MapViewOfFile function to obtain a pointer to the file view.

HANDLE hMapFile;
LPVOID lpMapAddress;

hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission.
    FALSE,                             // Do not inherit the name
    "MyFileMappingObject");            // of the mapping object.
 
if (hMapFile == NULL)
{
    ErrorHandler("Could not open file-mapping object.");
}
 
lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
    FILE_MAP_ALL_ACCESS,               // Read/write permission.
    0,                                 // Max. object size.
    0,                                 // Size of hFile.
    0);                                // Map entire file.
 
if (lpMapAddress == NULL)
{
    ErrorHandler("Could not map view of file.");
}


2. an alternative solution is to use the SendMessage method:

SendMessage
The SendMessage function sends the specified message to a window or windows. It calls the window procedure
for the specified window and does not return until the window procedure has processed the message.

LRESULT SendMessage(
  HWND hWnd,      // handle to destination window
  UINT Msg,       // message
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

just set the hWnd to be the handle to the other application window handle and you
can send messages between does apps.
0
 
LVL 2

Author Comment

by:Paullkha
ID: 2727907
mayab-

Q1:Sendmessage ... UINT msg
msg = RegisterWindowMessage from mahno?

Q2: Before I start delving into the MSDN discs, are you certain this includes communication between dos console exe and win32 exe?

Development under Win98
Launch on WinNT server
0
 
LVL 2

Author Comment

by:Paullkha
ID: 2728006
xls? mayab?
I was looking at the CreateNamedPipe function and it seems easier to implement (or at least understand). However, as mentioned above, I am developing under win98? CreateNamedPipe is not supported under Win98 according to the docs??? Is this option out?
0
 
LVL 2

Expert Comment

by:xLs
ID: 2729012
paullkha,  FileMapping is quite close to the namedpipe solution, shouldn't be too hard to understand. unfortunelly i was wrong, namedpipes doesnt work under win9x, i meant the filemapping.

thus Sending Messages to windows will probably not work bothways, since you dont have any window thus no message handler in the console unlessyou have a dummy window in the background (invisible).


and if you get really desperate check out BroadcastSystemMessage :), i wouldnt recomend it.
0
 

Expert Comment

by:mayab
ID: 2729754
working with the dos console application is like working with windows the consle application is attached with an handle like any other window on the system.you can not have a window app and a dos app runnnig at the same time in diffrent modes so i guess you mean a consle app created by windows so evenrything should work the same.
0
 
LVL 2

Expert Comment

by:xLs
ID: 2729898
right
0
 
LVL 2

Author Comment

by:Paullkha
ID: 2730729
Thanks all. Now I know the two possible solutions.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now