G
Guest
Hi guys,
this is a small test to understand the Marshal.GetFunctionPointerForDelegate
method.
Under unmanage I wrote this DLL:
..h
"// defined with this macro as being exported.
#ifdef PROVEEVENTI_EXPORTS
#define PROVEEVENTI_API __declspec(dllexport)
#else
#define PROVEEVENTI_API __declspec(dllimport)
#endif
extern "C"
{
PROVEEVENTI_API void Set_Evento(HANDLE );
PROVEEVENTI_API void Set_Exit();
PROVEEVENTI_API void Reset_Exit();
PROVEEVENTI_API void Do_Event(void);
}
"
..cpp
"
#include "stdafx.h"
#include "ProveEventi.h"
#include <windows.h>
#include <commctrl.h>
BOOL bExit = false;
HANDLE mRetFunct = INVALID_HANDLE_VALUE;
void (*pfnCallback) (void);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
PROVEEVENTI_API void Set_Evento(HANDLE retFunct)
{
if(retFunct != INVALID_HANDLE_VALUE)
{
pfnCallback = (void (__cdecl *) (void)) retFunct;
}
}
PROVEEVENTI_API void Do_Event()
{
while(!bExit)
{
Sleep(1000);
(*pfnCallback)();
}
}
PROVEEVENTI_API void Set_Exit()
{
bExit = true;
}
PROVEEVENTI_API void Reset_Exit()
{
bExit = false;
}
"
On managed Code I wrote this Dll:
..css
"
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace ProveEventiCLR
{
class EVClass
{
#region PInvoke
[DllImport("ProveEventi.dll")]
private static extern void Set_Evento(IntPtr proc);
[DllImport("ProveEventi.dll")]
private static extern IntPtr Do_Event();
[DllImport("ProveEventi.dll")]
private static extern int Set_Exit();
[DllImport("ProveEventi.dll")]
private static extern int Reset_Exit();
[DllImport("coredll.dll")]
private static extern IntPtr GetModuleHandle(string mod);
#endregion
#region delegates
public delegate void HookProc();
public event HookProc done;
#endregion
private GCHandle gch;
private static HookProc hookProc;
private int catchingCount;
public int Count
{
get { return catchingCount++; }
}
public EVClass()
{
catchingCount = 0;
hookProc = new HookProc(EventCatcher);
gch = GCHandle.Alloc(hookProc, GCHandleType.Pinned);
IntPtr pp = Marshal.GetFunctionPointerForDelegate(hookProc);
Set_Evento(pp);
}
public void Run()
{
Reset_Exit();
Do_Event();
}
public void Stop()
{
Set_Exit();
}
private void EventCatcher()
{
catchingCount++;
done();
}
}
}
"
The small Dll in managed code is instanced inside a form and the event
"event HookProc done" is added, So the function pointer delegate is passed to
unmanaged dll. This is work !!
Clicking a button I call the unmanaged function Reset_Exit and Do_Event:
in the unmanaged dll start a small loop where each 1 second is call my
managed delegate, this work fine !! I can see every second catching count
increasing, but the form class is freezed. If I call each event a Refresh() I
can see a refreshing but buttons or any other object doesn't respond at my
events, like mouse o keyboard.
please help me, thank you fabbrit
this is a small test to understand the Marshal.GetFunctionPointerForDelegate
method.
Under unmanage I wrote this DLL:
..h
"// defined with this macro as being exported.
#ifdef PROVEEVENTI_EXPORTS
#define PROVEEVENTI_API __declspec(dllexport)
#else
#define PROVEEVENTI_API __declspec(dllimport)
#endif
extern "C"
{
PROVEEVENTI_API void Set_Evento(HANDLE );
PROVEEVENTI_API void Set_Exit();
PROVEEVENTI_API void Reset_Exit();
PROVEEVENTI_API void Do_Event(void);
}
"
..cpp
"
#include "stdafx.h"
#include "ProveEventi.h"
#include <windows.h>
#include <commctrl.h>
BOOL bExit = false;
HANDLE mRetFunct = INVALID_HANDLE_VALUE;
void (*pfnCallback) (void);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
PROVEEVENTI_API void Set_Evento(HANDLE retFunct)
{
if(retFunct != INVALID_HANDLE_VALUE)
{
pfnCallback = (void (__cdecl *) (void)) retFunct;
}
}
PROVEEVENTI_API void Do_Event()
{
while(!bExit)
{
Sleep(1000);
(*pfnCallback)();
}
}
PROVEEVENTI_API void Set_Exit()
{
bExit = true;
}
PROVEEVENTI_API void Reset_Exit()
{
bExit = false;
}
"
On managed Code I wrote this Dll:
..css
"
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace ProveEventiCLR
{
class EVClass
{
#region PInvoke
[DllImport("ProveEventi.dll")]
private static extern void Set_Evento(IntPtr proc);
[DllImport("ProveEventi.dll")]
private static extern IntPtr Do_Event();
[DllImport("ProveEventi.dll")]
private static extern int Set_Exit();
[DllImport("ProveEventi.dll")]
private static extern int Reset_Exit();
[DllImport("coredll.dll")]
private static extern IntPtr GetModuleHandle(string mod);
#endregion
#region delegates
public delegate void HookProc();
public event HookProc done;
#endregion
private GCHandle gch;
private static HookProc hookProc;
private int catchingCount;
public int Count
{
get { return catchingCount++; }
}
public EVClass()
{
catchingCount = 0;
hookProc = new HookProc(EventCatcher);
gch = GCHandle.Alloc(hookProc, GCHandleType.Pinned);
IntPtr pp = Marshal.GetFunctionPointerForDelegate(hookProc);
Set_Evento(pp);
}
public void Run()
{
Reset_Exit();
Do_Event();
}
public void Stop()
{
Set_Exit();
}
private void EventCatcher()
{
catchingCount++;
done();
}
}
}
"
The small Dll in managed code is instanced inside a form and the event
"event HookProc done" is added, So the function pointer delegate is passed to
unmanaged dll. This is work !!
Clicking a button I call the unmanaged function Reset_Exit and Do_Event:
in the unmanaged dll start a small loop where each 1 second is call my
managed delegate, this work fine !! I can see every second catching count
increasing, but the form class is freezed. If I call each event a Refresh() I
can see a refreshing but buttons or any other object doesn't respond at my
events, like mouse o keyboard.
please help me, thank you fabbrit