|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionCreating a delegate in .NET is extremely simple. However, in a rare case, you might have to create an object of the .NET BackgroundThe problem I faced was: In an ATL COM environment, I had to create an object of the .NET I solved this problem by creating a helper class in .NET which wraps an unmanaged function pointer (sent from the unmanaged world) as an Using the codeStep 1. In .NET, create an interface //Interface exposed to COM world
[ComVisible(true),
GuidAttribute("2E9B141F-F876-4141-BCA3-F8B58DA0FE4F")]
public interface IDelegateHelper
{
AsyncCallback
AsynCallbackDelegateWrapper(IntPtr funcPtr_in);
}
//Class exposed to COM world
[ComVisible(true)]
public class DelegateHelperCLass : IDelegateHelper
{
public AsyncCallback
AsynCallbackDelegateWrapper(IntPtr funcPtr_in)
{
Delegate aAsyncCallbackDelegate =
Marshal.GetDelegateForFunctionPointer(funcPtr_in,
typeof(AsyncCallback));
return aAsyncCallbackDelegate as AsyncCallback;
}
}
Step 2. Export this .NET DLL into a TLB for use in COM. Use the tlbexp.exe utility to do the same. Register the .NET DLL using regasm. tlbexp.exe DelegateHelper.dll
/out:F:\DelegateHelper.tlb
regasm.exe DelegateHelper.dll
Step 3. To use this in the unmanaged world (COM C++, here), create an instance of //Import the TLBs #import "C:\WINDOWS\Microsoft.NET\Framework" "\v2.0.50215\mscorlib.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids, rename("_Module", "ReflectionModule"), rename("ReportEvent", "ReportEventModule") #import "F:\DelegateHelper.tlb" raw_interfaces_only named_guids, no_namespace //Add this to the library block in IDL //file for recognizing IAsyncResult* importlib("C:\WINDOWS\Microsoft.NET\" "Framework\v2.0.50215\mscorlib.tlb"); //Create a function pointer FuncPtr that has //a signature same as that of the delgate methods typedef void(*FuncPtr)(IAsyncResult*); //CClient code that calls the helper methods STDMETHODIMP CClient::CreateAsyncCallbackDelegate(void) { //First create an instance of DelegateHelper class CComPtr<IDelegateHelper> aHelper; HRESULT hr = aHelper.CoCreateInstance(CLSID_DelegateHelperCLass); if(FAILED(hr)) { return E_FAIL; } //AsyncCallback delegate instnace //that is to be populated with _AsyncCallback* aCallBack = NULL; FuncPtr aPtr = TestMethod; hr = aHelper->AsynCallbackDelegateWrapper((long)aPtr, &aCallBack); if(FAILED(hr)) { return E_FAIL; } //************************************* //Now aCallBack can be used like //a regular AsyncCallback instance!! //************************************* return S_OK; } //Some static method whose delegate is to be created void CClient::TestMethod(IAsyncResult* asyncResult_in) { //Your Callback code }
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||