 |
|
 |
Is it possible to send a function(-pointers) over IPC to a hook dll, which executes the function when the hook is called (the function pointer is in the main exe and needs information from the main exe)?
|
|
|
|
 |
|
 |
Hi Alex, Thanks for your great paper. I learned a lot from it. I was trying to use mutex between different processes (they need to use shared memory) in my project, i found that when process1 and process2 use different mutex name when calling m_hMutex = CreateMutex( NULL, FALSE, sxMutex); they can still communicate with each other. For this i am not sure if they are really locking the shared memory. I tried your example in "step 3", and set sxMutex as one value compiled and built SameText1.exe, then set sxMutex as ANOTHER value compiled and built SameText2.exe. When i run SameText1 and SameText2, sames they also get the same results as building them with the same mutex name. Could you please tell me why or where i am wrong? Many Thanks! Vic Thanks! vic
|
|
|
|
 |
|
 |
Hi,
Different instances of this application communicate through Windows messages and memory-mapped files. Mutex is used only for synchronozation. Making two mutexes with different names means that programs remain unsynchronized, as in step 2. They still work, but there is small chance of crash because access to shared resource is not synchronized.
BTW, looking at this article years after it is written, I would prefer to remove Step 4 because it is not necessary. It is quite enough to stop at step 3.
|
|
|
|
 |
|
 |
Can I use a similar method to perform the following?
Application launches, and detects that the nessessary thread is not running in memory, and launches it.
New child thread configures itself and stays memory resident.
Once the new child is spawned, attach to it using events/properties to connumicate with it (like most multithreaded applications would)
Now the tricky part, start a differant application and connect to the same instance of the child thread started by the first application and listen to the same events occuring and such??
-CS
|
|
|
|
 |
|
 |
Hi there !
This is a really good example on this topic. I have tested out the demo, and it seems to be working nice. The only thing is that I think that the MS guys could have implemented more standard ways of IPC in their new framework. It seems like the good old stuff must be used to gain speed.
However, I have to ensure that the readers get all the data from the writers. So a synchronization mechanism must be implemented. The windowsmessage only tells the readers that new data is arrived. The readers can not say when they have received the data. I implemented two counters, one number of subscribers, and a counter for how many that are finished reading. This seems to be working, but is a little bit tricky when readers dies ( if it is possible that a windows app do so
Do you have any suggestion on how to implement such a synchronization mechanism on top of this good framework ?
Tore
|
|
|
|
 |
|
 |
First i would like to say this is an excellent article. It has certainly helped me.
I would like to ask if I may use your CIPCReadWriteLock class in the source code to an article I am writing for CP. I must point out that I have modified the code and merged it into my own class, though I have given you the credit in the source code and the article.
I would also like to ask if there are any commercial restrictions placed on using this class?
Regards
|
|
|
|
 |
|
 |
Thanks. All source code published in CodeProject is free for use without any restrictions.
|
|
|
|
 |
|
 |
Thanks for that. You know, I always give credit where it's due, even in release builds, I will add other coders names into the credits somwehere. It annoys me to see apps where the creator has obviously ripped a library or two, and takes the credit himself.
|
|
|
|
 |
|
 |
The class seems like giving me problem in new compiler.(above VC6)
Like will crash in the destructor.
To solve this, I just set HANDLE member variables as NULL in the constructor.
CIPCReadWriteLock::CIPCReadWriteLock(
LPCTSTR sName, // [in] name
DWORD dwMilliseconds) // [in] maximum number of milliseconds to wait
// or INFINITE
{
// Generate names for mutex and semaphores.
// Names start from sName string and finish with
// _Mutex, _SemRd and _SemWr.
// If number of instances of this class are created
// in the same process with the same name (also NULL),
// they will guard the same resource.
int n = 0;
if ( sName )
n = lstrlen(sName);
n += c_nLen + 1;
m_sMutexName = new TCHAR[n];
m_sSemReadersName = new TCHAR[n];
m_sSemWritersName = new TCHAR[n];
m_sMemFileName = new TCHAR[n];
*m_sMutexName = *m_sSemReadersName = *m_sSemWritersName = 0;
if ( sName )
{
lstrcpy(m_sMutexName, sName);
lstrcpy(m_sSemReadersName, sName);
lstrcpy(m_sSemWritersName, sName);
lstrcpy(m_sMemFileName, sName);
}
lstrcat(m_sMutexName, c_sMutexName);
lstrcat(m_sSemReadersName, c_sSemReader);
lstrcat(m_sSemWritersName, c_sSemWriter);
lstrcat(m_sMemFileName, c_sMemFile);
// keep time interval
m_dwMilliseconds = dwMilliseconds;
//////////////////////////////////////////////////////////////////////////
//NULLify all
m_hMutex = NULL;
m_hsemReaders = NULL;
m_hsemWriters = NULL;
m_hFileMapping = NULL;
//////////////////////////////////////////////////////////////////////////
m_bInitSucceded = InitSyncObjects();
}
Sonork 100.41263:Anthony_Yio
|
|
|
|
 |
|
 |
Here is the example. You have 2 processes. The first process calls WaitForWrite(), and let's say it has a lot to write. The second process calls WaitForWrite() also and waits for the first process to finish. BUT, all of a sudden the first process crashes! Thus, the second process will never wake up, because someone (the first process) has to call the function Done().
The solution here is to wait for mutex (instead of semaphore) until it returns WAIT_OBJECT_0 or WAIT_ABANDONED (btw, the provided code does not process this return for the mutex that can happen one day).
Dima.
|
|
|
|
 |
|
 |
Hello,
I have the next problem : I'm writing IE toolbar with ATL Com Wizard (non MFC application). So I have a dll with the toolbar which is used from IE.I have a button on the toolbar which state can be connected/not connected (this is determined by the user). It is possible more than one IE be opened at the same time. I want when user changes state of the button, the change be invalidated in all other opened explorer's tollbars. That's why I need a mechanism similar to this here, but it doesn't work in an ATL project.Can anyone tell me why?
Thanks very much
|
|
|
|
 |
|
 |
I have done a wh_getmessage hook
and in that i got wparam character message
say wparam= 'c'
{
Then i have to post another character using PostMessage
PostMessage(msg->hwnd,WM_CHAR, dwPrevChar, 1);
msg->hwnd is the handle of the active window AND dwPrevChar is the character which i am sending
}
Let us assume
dwPrevChar='r' then i got the r printed on the active applications
but these are my problems
in notepad i got cr
in wordpad i got crr
in winword i got crrr
in photoshop i got crr
i couldnt figure out why is this happening
Please help me if there is somebody who know why is this problem occurs.
Thanks in advance
vimal
|
|
|
|
 |
|
 |
i think // Ensure exclusive access to the member variables WaitForSingleObject(m_hMutex, m_dwMilliseconds) can return timeout... and you will not change intrinsic variables ie writer allways think, what reader still reading ) and can not get access to memory use WaitForSingleObject(m_hMutex, INFINITE) in Done
|
|
|
|
 |
|
 |
I never use INFINITE in wait operations. This is my own preference - some programmers prefer to use INFINITE wait. In this particular case, if wait operation returns timeout, writer thinks that reader is still reading. However, writer has it's own timeout, and it is not blocked. I try to write program by such way, that if some thread is locked (because of my bug or something else), all other threads/processes are not locked. This allows to keep program responsive in any case.
|
|
|
|
 |
|
 |
Folks,
Just wondering if any one of you help me in a particular problem. I have 2 programs, one a VC++ program that generates numbers (coordinates) at the rate of 50 per second, and the second, a flash program that reads in these coordinates 50 times a second and displays a 3D graph accordingly. My question is, how do I make the 2 programs communicate? One obivious solution would be to keep writing to a file and have the flash prog read from this file (Actually, the flash program needs input in the form of a file). In this case, how do I synchronise the 2 programs. And, are there any other alternatives?
In the initial phase of development, we do not have the flash program ready, to test the system. So, we'll have to test the coordinate generator with a stub program which just reads and displays the values generated by the first vc++ program.
The intended flow is like:
(VC++ prog generating cordinates) ---> (medium for inter-program communication) ---> (a stub program that just reads/displays these values, to be replaced later by the flash prog as soon as it is ready).
Here, the middle part "medium for inter program communication" is the part we are not too sure about. If we use an ascii file into which the coordinates are written into/read from, how do we synchronise it so that the read happens only after the write. Can a Windows equivalent of Unix semaphores be implemented.
Thanks,
Hans
|
|
|
|
 |
|
 |
You can use windows messages passing the required numbers in wParam and lParam or memory-mapped file. In the second case use named mutex for synchronization.
|
|
|
|
 |
|
 |
good,
but can i send a pointer to a string between application?
application A:
HWND hAppWnd; // handle of other application
...
UINT myMsg = RegisterWindowMessage( "xyz" );
...
bRet = SendMessage( hAppWnd,myMsg,strlen( sParam ),(LPARAM)sParam );
...
application B
void myFunction( WPARAM wLen,LPARAM lMessage ) {
char *sParam;
sParam = (char *)malloc(wLen,sizeof( char ) );
sprintf( sParam,"%s",(LPSTR)lMessage ); // Error
}
sParam and lMessage have the same address.
where is the problem? i have read the some function cannot send a pointer. ok. but WM_SETTEXT how it works?
|
|
|
|
 |
|
 |
It's impossible to send pointers between applications because each process has it's own address space. WM_SETTEXT doesn't work between processes. WM_GETTEXT works between processes, because internally it creates memory-mapped file. To send strings between applications you need to use memory-mapped files, exactly as described in this article.
|
|
|
|
 |
|
 |
I know it sound stupid...
Can we used the same method between the exe and dll files...
:_Rocket_:
|
|
|
|
 |
|
 |
You don't need this since exe and dll are in the same process.
|
|
|
|
 |
|
 |
:Thankz for reply...but i still don't get what are you try to say here....Can you explain further more.
Thankz
img src="/script/Forums/Images/smiley_cry.gif" align="top" alt="Cry | :((" />
:_Rocket_:
|
|
|
|
 |
|
 |
What he meant was: when an "exe" loads a dll, they share the same address space, so all you have to do is pass any data you want from the exe to the dll (we talk as they were different entitys, but when they are running they are linked in the same process), using a simple function call.
We only use IPC when we want to make communication between 2 different process, that don´t share the same address space.
Best regards,
Paulo Lima, Brazil.
|
|
|
|
 |
|
 |
Does anyone know of a class that does read-write guarding like CIPCReadWriteLock but uses Critical Sections rather than Mutexes and Semaphores? I have an in-process resource that could benefit from the read-write guarding, but because its not inter-process the performance would be better if only Critical Sections were used....
|
|
|
|
 |
|
 |
I didn't see read-write guard using only critical sections. The book "Multithreading Applications in Win32" (James E.Beveridge, Robert Wiener) contains very simple read-write lock algorithm in the sample READWRIT. It uses one mutex and one semaphore. The sample is written in C but can be easily converted to class.
|
|
|
|
 |
|
 |
I have hidden one part of my program with the same technique as discussed here. And this part is not receiving HWND_BROADCAST messages. Anyone have an idea how to resolve this?
--
David
|
|
|
|
 |