Click here to Skip to main content
15,905,071 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: CClientDc Pin
Stephen Hewitt16-Jun-10 19:23
Stephen Hewitt16-Jun-10 19:23 
AnswerRe: CClientDc Pin
Aescleal16-Jun-10 22:50
Aescleal16-Jun-10 22:50 
QuestionMs PowerPoint password protected File Pin
MsmVc16-Jun-10 18:11
MsmVc16-Jun-10 18:11 
AnswerRe: Ms PowerPoint password protected File Pin
MsmVc16-Jun-10 23:48
MsmVc16-Jun-10 23:48 
QuestionSave to a file Pin
thaiguy_md16-Jun-10 12:58
thaiguy_md16-Jun-10 12:58 
AnswerRe: Save to a file Pin
Rick York16-Jun-10 16:37
mveRick York16-Jun-10 16:37 
GeneralRe: Save to a file Pin
ThatsAlok16-Jun-10 23:55
ThatsAlok16-Jun-10 23:55 
QuestionSetWindowsHookEx and Callback Pin
AlmightyEdge16-Jun-10 10:26
AlmightyEdge16-Jun-10 10:26 
Hello,

Let me first start by telling you I have very little (read: none, ive read some parts in an ebook but..) experience with c++.
I'm trying to manage a callback to a more 'trusted' managed environment.

I'm trying to use SetWindowsHookEx(WH_CBT) on a specified application to monitor and alter window behavior.
I've created the HookProc in unmanaged c++ (see below) together with a Initialize function that takes as argument void* so I can call the function inside my managed application.
I pass the function of my managed application with Managed.getFunctionPointerforDelegate().
This seems to work.

However. If I hook the external process. (Read: Inject all of its threads seperately) it doesn't seem todo the callback to my managed application.
Winapi Override shows my unmanaged dll as being linked. (If I however can't find it through System.Diagnostics.Process.Modules)

The code:
The unmanaged part:

<FONT COLOR="#000099">#include "Hook.h"
</FONT><FONT COLOR="#999999">
// Callback...
</FONT><FONT COLOR="#FF6633">int</FONT><FONT COLOR="#663300"> (*</FONT>pfnCallback<FONT COLOR="#663300">) (</FONT><FONT COLOR="#FF6633">void</FONT><FONT COLOR="#663300">);</FONT><FONT COLOR="#FF6633">

void</FONT> Initialize<FONT COLOR="#663300">(</FONT>HANDLE retFunc<FONT COLOR="#663300">)
{</FONT><FONT COLOR="#FF0000">
	if</FONT><FONT COLOR="#663300"> (</FONT>retFunc<FONT COLOR="#663300"> !=</FONT> INVALID_HANDLE_VALUE<FONT COLOR="#663300">)
	{</FONT>

		pfnCallback<FONT COLOR="#663300"> = (</FONT><FONT COLOR="#FF6633">int</FONT><FONT COLOR="#663300"> (</FONT>_cdecl<FONT COLOR="#663300"> *) (</FONT><FONT COLOR="#FF6633">void</FONT><FONT COLOR="#663300">))</FONT> retFunc<FONT COLOR="#663300">;</FONT><FONT COLOR="#FF6633">
		int</FONT> a<FONT COLOR="#663300"> =</FONT> ThrowMessage<FONT COLOR="#663300">();</FONT><FONT COLOR="#FF0000">

		if</FONT><FONT COLOR="#663300"> (</FONT>a<FONT COLOR="#663300">==</FONT><FONT COLOR="#999900">5</FONT><FONT COLOR="#663300">)
		{</FONT>
			ThrowMessage<FONT COLOR="#663300">();
		}</FONT>
		ThrowMessage<FONT COLOR="#663300">();
	}
}</FONT><FONT COLOR="#FF6633">

int</FONT> ThrowMessage<FONT COLOR="#663300">()
{</FONT><FONT COLOR="#999999">
	// Do a callback..
</FONT><FONT COLOR="#FF0000">	return</FONT><FONT COLOR="#663300"> (*</FONT>pfnCallback<FONT COLOR="#663300">)(); 
}</FONT><FONT COLOR="#FF6633">

int</FONT> HookProc<FONT COLOR="#663300">(</FONT><FONT COLOR="#FF6633">int</FONT> nCode<FONT COLOR="#663300">,</FONT> WPARAM wParam<FONT COLOR="#663300">,</FONT> LPARAM lParam<FONT COLOR="#663300">)
{</FONT>

	ThrowMessage<FONT COLOR="#663300">();</FONT><FONT COLOR="#FF0000">
	return</FONT> CallNextHookEx<FONT COLOR="#663300">(</FONT>NULL<FONT COLOR="#663300">,</FONT>nCode<FONT COLOR="#663300">,</FONT>wParam<FONT COLOR="#663300">,</FONT>lParam<FONT COLOR="#663300">);
}</FONT>


The c# part:
<FONT COLOR="#990000">    public class</FONT> HookManager<FONT COLOR="#663300">
    {</FONT><FONT COLOR="#990000">

        private</FONT> GCHandle gch<FONT COLOR="#663300">;</FONT><FONT COLOR="#999999">               // Prevents garbage collection
</FONT><FONT COLOR="#990000">        private</FONT> delegate<FONT COLOR="#FF6633"> int</FONT> HookProc<FONT COLOR="#663300">();</FONT><FONT COLOR="#999999">   // Delegate that unmanaged api calls

</FONT><FONT COLOR="#990000">        private static</FONT> HookProc hookProc<FONT COLOR="#663300">;</FONT><FONT COLOR="#999999">   // HookProc variable ..
</FONT><FONT COLOR="#990000">        
        private static</FONT> Stack<FONT COLOR="#663300"><</FONT>IntPtr<FONT COLOR="#663300">></FONT> hookList<FONT COLOR="#663300"> =</FONT><FONT COLOR="#990000"> new</FONT> Stack<FONT COLOR="#663300"><</FONT>IntPtr<FONT COLOR="#663300">>();</FONT><FONT COLOR="#990000">

        public</FONT> HookManager<FONT COLOR="#663300">()
        {</FONT>
            hookProc<FONT COLOR="#663300"> =</FONT><FONT COLOR="#990000"> new</FONT> HookProc<FONT COLOR="#663300">(</FONT>HookEvent<FONT COLOR="#663300">);</FONT>

            gch<FONT COLOR="#663300"> =</FONT> GCHandle<FONT COLOR="#663300">.</FONT>Alloc<FONT COLOR="#663300">(</FONT>hookProc<FONT COLOR="#663300">,</FONT> GCHandleType<FONT COLOR="#663300">.</FONT>Normal<FONT COLOR="#663300">);</FONT>

            IntPtr managedReference<FONT COLOR="#663300"> =</FONT> Marshal<FONT COLOR="#663300">.</FONT>GetFunctionPointerForDelegate<FONT COLOR="#663300">(</FONT>hookProc<FONT COLOR="#663300">);</FONT>

            Poseidon<FONT COLOR="#663300">.</FONT>Initialize<FONT COLOR="#663300">(</FONT>managedReference<FONT COLOR="#663300">);
        }</FONT><FONT COLOR="#990000">

        public</FONT><FONT COLOR="#FF6633"> void</FONT> HookIt<FONT COLOR="#663300">(</FONT>Process hook<FONT COLOR="#663300">)
        {</FONT><FONT COLOR="#999999">
            // Now we will install hook
</FONT>            IntPtr hModule<FONT COLOR="#663300"> =</FONT> Win32<FONT COLOR="#663300">.</FONT>GetModuleHandle<FONT COLOR="#663300">(</FONT><FONT COLOR="#009900">"Poseidon.dll"</FONT><FONT COLOR="#663300">);</FONT>

            IntPtr ProcAddress<FONT COLOR="#663300"> =</FONT> Win32<FONT COLOR="#663300">.</FONT>GetProcAddress<FONT COLOR="#663300">(</FONT>hModule<FONT COLOR="#663300">,</FONT><FONT COLOR="#009900"> "HookProc"</FONT><FONT COLOR="#663300">);</FONT>

            ProcessThread<FONT COLOR="#663300">[]</FONT> threads<FONT COLOR="#663300"> =</FONT><FONT COLOR="#990000"> new</FONT> ProcessThread<FONT COLOR="#663300">[</FONT>hook<FONT COLOR="#663300">.</FONT>Threads<FONT COLOR="#663300">.</FONT>Count<FONT COLOR="#663300">];</FONT>

            hook<FONT COLOR="#663300">.</FONT>Threads<FONT COLOR="#663300">.</FONT>CopyTo<FONT COLOR="#663300">(</FONT>threads<FONT COLOR="#663300">,</FONT><FONT COLOR="#999900"> 0</FONT><FONT COLOR="#663300">);</FONT>

            foreach<FONT COLOR="#663300"> (</FONT>ProcessThread thread in threads<FONT COLOR="#663300">)
            {</FONT>

                IntPtr HHOOK<FONT COLOR="#663300"> =</FONT> Win32<FONT COLOR="#663300">.</FONT>SetWindowsHookEx<FONT COLOR="#663300">(</FONT>Win32<FONT COLOR="#663300">.</FONT>HookType<FONT COLOR="#663300">.</FONT>WH_CBT<FONT COLOR="#663300">,</FONT> ProcAddress<FONT COLOR="#663300">,</FONT> hModule<FONT COLOR="#663300">, (</FONT>uint<FONT COLOR="#663300">)</FONT>thread<FONT COLOR="#663300">.</FONT>Id<FONT COLOR="#663300">);</FONT>

                Console<FONT COLOR="#663300">.</FONT>WriteLine<FONT COLOR="#663300">(</FONT><FONT COLOR="#009900">"{0}, {1}, {2}"</FONT><FONT COLOR="#663300">,</FONT> hModule<FONT COLOR="#663300">,</FONT> ProcAddress<FONT COLOR="#663300">,</FONT> HHOOK<FONT COLOR="#663300">);</FONT>

                hookList<FONT COLOR="#663300">.</FONT>Push<FONT COLOR="#663300">(</FONT>HHOOK<FONT COLOR="#663300">);
            }
		}</FONT><FONT COLOR="#990000">

        public</FONT><FONT COLOR="#FF6633"> int</FONT> HookEvent<FONT COLOR="#663300">()
        {</FONT>

            Console<FONT COLOR="#663300">.</FONT>WriteLine<FONT COLOR="#663300">(</FONT><FONT COLOR="#009900">"Hookevent called!"</FONT><FONT COLOR="#663300">);</FONT><FONT COLOR="#FF0000">
            return</FONT><FONT COLOR="#999900"> 5</FONT><FONT COLOR="#663300">;
        }</FONT><FONT COLOR="#990000">

        private static class</FONT> Poseidon<FONT COLOR="#663300">

        {
            [</FONT>DllImport<FONT COLOR="#663300">(</FONT><FONT COLOR="#009900">"Poseidon.dll"</FONT><FONT COLOR="#663300">)]</FONT><FONT COLOR="#990000">
            public static extern</FONT><FONT COLOR="#FF6633"> void</FONT> Initialize<FONT COLOR="#663300">(</FONT>IntPtr callback<FONT COLOR="#663300">);
        }
	}</FONT>



Thanks already for reading this far. Does anyone have any idea this is ? Or suggestions?
I desperately try to get this to work. It's driving me nuts right now. D'Oh! | :doh:
AnswerRe: SetWindowsHookEx and Callback [modified] Pin
Hristo-Bojilov16-Jun-10 11:14
Hristo-Bojilov16-Jun-10 11:14 
GeneralRe: SetWindowsHookEx and Callback Pin
AlmightyEdge16-Jun-10 12:48
AlmightyEdge16-Jun-10 12:48 
GeneralRe: SetWindowsHookEx and Callback Pin
AlmightyEdge18-Jun-10 2:59
AlmightyEdge18-Jun-10 2:59 
AnswerRe: SetWindowsHookEx and Callback Pin
Stephen Hewitt16-Jun-10 19:19
Stephen Hewitt16-Jun-10 19:19 
GeneralRe: SetWindowsHookEx and Callback Pin
AlmightyEdge18-Jun-10 3:00
AlmightyEdge18-Jun-10 3:00 
GeneralRe: SetWindowsHookEx and Callback Pin
Stephen Hewitt21-Jun-10 15:04
Stephen Hewitt21-Jun-10 15:04 
Questionwrap the wininet dll Pin
alto16-Jun-10 7:41
alto16-Jun-10 7:41 
AnswerRe: wrap the wininet dll Pin
ThatsAlok16-Jun-10 23:57
ThatsAlok16-Jun-10 23:57 
GeneralRe: wrap the wininet dll Pin
Moak17-Jun-10 3:43
Moak17-Jun-10 3:43 
Questionvalid date and time Pin
MKC00216-Jun-10 6:01
MKC00216-Jun-10 6:01 
AnswerRe: valid date and time Pin
CPallini16-Jun-10 6:19
mveCPallini16-Jun-10 6:19 
QuestionFloodFill question Pin
ForNow16-Jun-10 5:04
ForNow16-Jun-10 5:04 
AnswerRe: FloodFill question Pin
Chris Losinger16-Jun-10 5:39
professionalChris Losinger16-Jun-10 5:39 
AnswerRe: FloodFill question Pin
Niklas L16-Jun-10 6:27
Niklas L16-Jun-10 6:27 
Questionbetter linear algebra Pin
VeganFanatic16-Jun-10 5:03
VeganFanatic16-Jun-10 5:03 
QuestionNew In c and c++. Downloading data from a server Pin
Eli Nurman16-Jun-10 4:26
Eli Nurman16-Jun-10 4:26 
AnswerRe: New In c and c++. Downloading data from a server Pin
Maximilien16-Jun-10 4:46
Maximilien16-Jun-10 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.