[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

jan.loucka

6/25/2007 3:02:00 AM

Hi,
Is there any way in .NET how to capture WIN API messages that belong
to different application? We have a Windows Form app written in .NET
2.0 and from our application we're running another application called
MapInfo using Interop. We need to be able to somehow figure out when
the user exits the MapInfo applicaiton so we can close our own app as
well.
I was looking on SetWindowsHookEx function but couldn't make it work.
Thanks for any help
Jan

3 Answers

Peter Duniho

6/25/2007 3:25:00 AM

0

On Sun, 24 Jun 2007 20:02:10 -0700, <jan.loucka@gmail.com> wrote:

> Hi,
> Is there any way in .NET how to capture WIN API messages that belong
> to different application?

You can always use p/invoke. I'm not aware of a general-purpose mechanism
that allows you to hook window messages the way you can with the native
Win32 API. That said...

> We have a Windows Form app written in .NET
> 2.0 and from our application we're running another application called
> MapInfo using Interop. We need to be able to somehow figure out when
> the user exits the MapInfo applicaiton so we can close our own app as
> well.

If you are using the Process class to start the other application, you
should be able to use that Process instance to track the activity of the
other application and detect when it's been closed. You can subscribe to
the Process.Exited event to receive notification of the application
exiting.

> I was looking on SetWindowsHookEx function but couldn't make it work.

What did you try? What about it didn't work?

Pete

jan.loucka

6/25/2007 4:23:00 AM

0

On Jun 25, 11:25 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sun, 24 Jun 2007 20:02:10 -0700, <jan.lou...@gmail.com> wrote:
> > Hi,
> > Is there any way in .NET how to capture WIN API messages that belong
> > to different application?
>
> You can always use p/invoke. I'm not aware of a general-purpose mechanism
> that allows you to hook window messages the way you can with the native
> Win32 API. That said...
>
> > We have a Windows Form app written in .NET
> > 2.0 and from our application we're running another application called
> > MapInfo using Interop. We need to be able to somehow figure out when
> > the user exits the MapInfo applicaiton so we can close our own app as
> > well.
>
> If you are using the Process class to start the other application, you
> should be able to use that Process instance to track the activity of the
> other application and detect when it's been closed. You can subscribe to
> the Process.Exited event to receive notification of the application
> exiting.
>
> > I was looking on SetWindowsHookEx function but couldn't make it work.
>
> What did you try? What about it didn't work?
>
> Pete

I'm running the new application by creating a new instance
this.mi = new MapInfo.MapInfoApplicationClass();
my project has a reference to a MapInfo dll - and when I create this
instance it starts the MapInfo app. It's separate process and
therefore I'm expecting it's running on separate thred - on the other
side - my app halts until the loading of MapInfo finishes so maybe
they're both running on the same thread?
I can get the MapInfo process using (I have a pointer - handler to
it):
Int32 pid = win32.GetWindowProcessID(this.miWin.ToInt32());
mapInfoProcess = Process.GetProcessById(pid);
but when I subscribe to Exited event it never gets fired?

I also tried to "subscribe" to the messages coming from MapInfo so I
can catch WM_CLOSE message but I can't get it working.
My code looks like this:
public delegate IntPtr MessageProc(int code, IntPtr wParam, IntPtr
lParam);
IntPtr hookHandle = SetWindowsHookEx(WH_GETMESSAGE,
hookFunction,IntPtr.Zero, AppDomain.GetCurrentThreadId());
public IntPtr NameOfYourFunction(int code, IntPtr wParam, IntPtr
lParam)
{
Message test =
(Message)Marshal.PtrToStructure(lParam,typeof(Message));
return new IntPtr();
}


Jayme.Pechan

6/25/2007 6:29:00 AM

0

I have had a problem with a 3rd party out-of-process com object that has a
tendency to crash. Of course, it causes problems when this exe crashes, so
I had to watch to determine when the exe exited. All I did was spin up a
thread that used something simliar to the following:

public void ThreadFunc()
{
Process[] processList =
System.Diagnostics.Process.GetProcessesByName("PROCESS.EXE");
if (processList.length > 0)
{
processList[0].WaitForExit();
// The process has exited
}
else
{
// could not find the process
}
}

Not sure if this is what you are looking for but hope this helps.

Jayme


<jan.loucka@gmail.com> wrote in message
news:1182745389.499988.40250@j4g2000prf.googlegroups.com...
> On Jun 25, 11:25 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> wrote:
>> On Sun, 24 Jun 2007 20:02:10 -0700, <jan.lou...@gmail.com> wrote:
>> > Hi,
>> > Is there any way in .NET how to capture WIN API messages that belong
>> > to different application?
>>
>> You can always use p/invoke. I'm not aware of a general-purpose
>> mechanism
>> that allows you to hook window messages the way you can with the native
>> Win32 API. That said...
>>
>> > We have a Windows Form app written in .NET
>> > 2.0 and from our application we're running another application called
>> > MapInfo using Interop. We need to be able to somehow figure out when
>> > the user exits the MapInfo applicaiton so we can close our own app as
>> > well.
>>
>> If you are using the Process class to start the other application, you
>> should be able to use that Process instance to track the activity of the
>> other application and detect when it's been closed. You can subscribe to
>> the Process.Exited event to receive notification of the application
>> exiting.
>>
>> > I was looking on SetWindowsHookEx function but couldn't make it work.
>>
>> What did you try? What about it didn't work?
>>
>> Pete
>
> I'm running the new application by creating a new instance
> this.mi = new MapInfo.MapInfoApplicationClass();
> my project has a reference to a MapInfo dll - and when I create this
> instance it starts the MapInfo app. It's separate process and
> therefore I'm expecting it's running on separate thred - on the other
> side - my app halts until the loading of MapInfo finishes so maybe
> they're both running on the same thread?
> I can get the MapInfo process using (I have a pointer - handler to
> it):
> Int32 pid = win32.GetWindowProcessID(this.miWin.ToInt32());
> mapInfoProcess = Process.GetProcessById(pid);
> but when I subscribe to Exited event it never gets fired?
>
> I also tried to "subscribe" to the messages coming from MapInfo so I
> can catch WM_CLOSE message but I can't get it working.
> My code looks like this:
> public delegate IntPtr MessageProc(int code, IntPtr wParam, IntPtr
> lParam);
> IntPtr hookHandle = SetWindowsHookEx(WH_GETMESSAGE,
> hookFunction,IntPtr.Zero, AppDomain.GetCurrentThreadId());
> public IntPtr NameOfYourFunction(int code, IntPtr wParam, IntPtr
> lParam)
> {
> Message test =
> (Message)Marshal.PtrToStructure(lParam,typeof(Message));
> return new IntPtr();
> }
>
>