Click here to Skip to main content

Managed C++/CLI

 
QuestionHow to activate keys combination with hook ? Pinmemberzayl54317-Aug-12 1:18 
SuggestionRe: How to activate keys combination with hook ? Pinmembersanjaylk12-Sep-12 3:00 
Questionvoid GetFieldValue( LPCTSTR lpszName, CString& strValue ); Pinmemberlucky_122119-Jul-12 23:21 
Questionsearch in a text fiel Pinmemberbyank18-Jul-12 3:06 
AnswerRe: search in a text fiel PinmvpRichard MacCutchan18-Jul-12 4:28 
GeneralRe: search in a text fiel [modified] Pinmemberbyank19-Jul-12 11:58 
GeneralRe: search in a text fiel PinmvpRichard MacCutchan19-Jul-12 23:05 
QuestionConvert string to integer only such that if it is alphanumeric or float it shouldnot return anything PinmemberAbinash Mohanty17-Jul-12 4:13 
GeneralRe: Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything [modified] PinmemberJohn Schroedl17-Jul-12 4:29 
GeneralRe: Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything PinmemberAbinash Mohanty17-Jul-12 19:13 
QuestionRead char by char in a file after write char by char to another file in vc++. Pinmemberlucky_122117-Jul-12 0:03 
AnswerRe: Read char by char in a file after write char by char to another file in vc++. PinmvpRichard MacCutchan17-Jul-12 0:32 
QuestionC++/CLI - Pin a function pointer Pinmemberkrishter11-Jul-12 0:16 
GeneralRe: C++/CLI - Pin a function pointer [modified] Pinmemberpaul_7115-Jul-12 0:51 
Difficult to say from what you have shown. You can pin a handle to a managed object only temporarily, since a pin_ptr can only be created on the stack. Therefore, if you have something like this:
IntPtr YourClass::GetFuncPointer()
{
    FuncDelegate^ fp = gcnew MessageFuncDelegate(this, &Handler);
    pin_ptr<MessageFuncDelegate^> pinnedFunctionPointer = &fp;
    return ((ProcMessageFunc)pinnedFunctionPointer);
}
then you effectively have only pinned fp for the duration of this method call. A correct way to do this depends on your scenario. I'd recommend something along the lines of the following (I didn't compile it, but you get the idea):
 
public ref class ClientRegistrar abstract sealed {
    static FuncDelegate^ s_ManagedReceiverList;
    
    // ...
    public static void RegisterClient(YourClient^ p_Client)
    {
        // thread safety for free
        s_ManagedReceiverList += 
            gcnew MessageFuncDelegate(p_Client, &YourClient::Handler);
 
        // here I assume that ::Receiver is an unmanaged global function which
        // takes a funtion pointer of type ProcMessageFunc. Don't know
        // if you need to deal with multiple registrations....
        ::Receiever((ProcMessageFunc)Marshal::GetFunctionPointerForDelegate(s_ManagedReceiver));
    }
 
    public static void UnregisterClient(YourClient^ p_Client)
    {
        s_ManagedReceiverList -=
            gcnew  MessageFuncDelegate(p_Client, &YourClient::Handler);
        // use the same schema to unregister from the native receiver
    }
}
 
Usage (C#):
 
YourClient tClient = new YourClient();
// ...
ClientRegistrar.RegisterClient(tClient);
// ...
 

s_ManagedReceiverList and any YourClient instance you register via RegisterClient will be kept alive as long as the current application domain will exist. Implement UnregisterClient if you need more fine grained control. A delegate does not need to be pinned, the CLR takes care of proper handling of that. It just needs to be kept alive.
 
Cheers,
 
Paul

modified 17-Jul-12 10:14am.

QuestionHow to Make and Use Resource DialogBoxes in VC++ 2008 PinmemberICEFLOWER226-Jun-12 5:07 
AnswerRe: How to Make and Use Resource DialogBoxes in VC++ 2008 PinmvpRichard MacCutchan26-Jun-12 5:31 
GeneralRe: How to Make and Use Resource DialogBoxes in VC++ 2008 PinmemberICEFLOWER226-Jun-12 8:53 
GeneralRe: How to Make and Use Resource DialogBoxes in VC++ 2008 PinmvpRichard MacCutchan26-Jun-12 9:24 
QuestionGetting MdiParent Instance From MdiChild - Windows Forms Application PinmemberParamu197312-Jun-12 2:50 
AnswerRe: Getting MdiParent Instance From MdiChild - Windows Forms Application PinmvpRichard MacCutchan12-Jun-12 3:15 
GeneralRe: Getting MdiParent Instance From MdiChild - Windows Forms Application [modified] PinmemberParamu197312-Jun-12 3:30 
GeneralRe: Getting MdiParent Instance From MdiChild - Windows Forms Application PinmvpRichard MacCutchan12-Jun-12 4:03 
GeneralRe: Getting MdiParent Instance From MdiChild - Windows Forms Application PinmemberParamu197315-Jun-12 2:30 
QuestionHow to convert from 'WCHAR' to 'System::String ^' Managed C++ Pinmemberptr_Electron7-Jun-12 21:08 
AnswerRe: How to convert from 'WCHAR' to 'System::String ^' Managed C++ PinmvpRichard MacCutchan7-Jun-12 22:58 
Questionint x = (Int32) thisisObjectType is boxing, if the type is already checked ? Pinmemberptr_Electron7-Jun-12 19:15 
AnswerRe: int x = (Int32) thisisObjectType is boxing, if the type is already checked ? PinmvpRichard MacCutchan8-Jun-12 1:34 
QuestionVS2010 Error()- Method or Property 'VCFilter::get_Url Behavior' deprecated and nolonger implemented [modified] PinmemberParamu19737-Jun-12 0:57 
QuestionC++ CX and WPF PinmemberSuper Lloyd6-Jun-12 16:04 
QuestionStrong vs static typing Pinmemberelelont25-Jun-12 10:37 
AnswerRe: Strong vs static typing PinmemberPaul Conrad5-Jun-12 11:37 
GeneralRe: Strong vs static typing PinmvpLuc Pattyn5-Jun-12 12:13 
GeneralRe: Strong vs static typing PinmemberPaul Conrad5-Jun-12 13:10 
AnswerRe: Strong vs static typing PinmvpRichard MacCutchan5-Jun-12 22:08 
GeneralRe: Strong vs static typing Pinmemberelelont26-Jun-12 6:09 
GeneralRe: Strong vs static typing PinmvpRichard MacCutchan6-Jun-12 6:21 
QuestionImage Integral PinmemberMUJIS29-May-12 5:40 
QuestionHow to get the total length of elements in ##__VA_ARGS__ Pinmemberelelont222-May-12 4:10 
AnswerRe: How to get the total length of elements in ##__VA_ARGS__ Pinmemberjschell28-May-12 8:57 
QuestionA calendar look like iCal of MacOsX PinmemberXRay221-May-12 5:30 
Questionc++ A Cessna Skyhawk Skeleton for Further Development in OpenGL (GLUT) Pinmemberody kurniawan18-May-12 12:55 
QuestionType casting Pinmembergivinisha17-May-12 7:50 
AnswerRe: Type casting PinmemberJohn Schroedl17-May-12 9:01 
QuestionVideo Display hangs in Windows after unlocking PinmemberFaez Shingeri16-May-12 22:37 
AnswerRe: Video Display hangs in Windows after unlocking - Repost ... PinmvpRichard MacCutchan17-May-12 0:12 
GeneralRe: Video Display hangs in Windows after unlocking - Repost ... PinmemberFaez Shingeri17-May-12 0:16 
GeneralRe: Video Display hangs in Windows after unlocking - Repost ... PinmemberJohn Schroedl17-May-12 4:28 
AnswerRe: Video Display hangs in Windows after unlocking Pinmemberjschell17-May-12 12:26 
QuestionFinding a pointer from a list of pointers Pinmemberelelont226-Apr-12 21:21 
AnswerRe: Finding a pointer from a list of pointers PinmvpRichard MacCutchan26-Apr-12 23:28 

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

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


Advertise | Privacy | Mobile
Web01 | 2.8.150326.1 | Last Updated 13 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid