Click here to Skip to main content
15,888,454 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: search in a text fiel Pin
Richard MacCutchan19-Jul-12 22:05
mveRichard MacCutchan19-Jul-12 22:05 
QuestionConvert string to integer only such that if it is alphanumeric or float it shouldnot return anything Pin
Abinash Mohanty17-Jul-12 3:13
Abinash Mohanty17-Jul-12 3:13 
GeneralRe: Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything Pin
John Schroedl17-Jul-12 3:29
professionalJohn Schroedl17-Jul-12 3:29 
GeneralRe: Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything Pin
Abinash Mohanty17-Jul-12 18:13
Abinash Mohanty17-Jul-12 18:13 
QuestionRead char by char in a file after write char by char to another file in vc++. Pin
lucky_122116-Jul-12 23:03
lucky_122116-Jul-12 23:03 
AnswerRe: Read char by char in a file after write char by char to another file in vc++. Pin
Richard MacCutchan16-Jul-12 23:32
mveRichard MacCutchan16-Jul-12 23:32 
QuestionC++/CLI - Pin a function pointer Pin
krishter10-Jul-12 23:16
krishter10-Jul-12 23:16 
GeneralRe: C++/CLI - Pin a function pointer Pin
Paul Michalik14-Jul-12 23:51
Paul Michalik14-Jul-12 23: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:
C++
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):

C++
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#):

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 Pin
ICEFLOWER226-Jun-12 4:07
ICEFLOWER226-Jun-12 4:07 
AnswerRe: How to Make and Use Resource DialogBoxes in VC++ 2008 Pin
Richard MacCutchan26-Jun-12 4:31
mveRichard MacCutchan26-Jun-12 4:31 
GeneralRe: How to Make and Use Resource DialogBoxes in VC++ 2008 Pin
ICEFLOWER226-Jun-12 7:53
ICEFLOWER226-Jun-12 7:53 
GeneralRe: How to Make and Use Resource DialogBoxes in VC++ 2008 Pin
Richard MacCutchan26-Jun-12 8:24
mveRichard MacCutchan26-Jun-12 8:24 
QuestionGetting MdiParent Instance From MdiChild - Windows Forms Application Pin
Paramu197312-Jun-12 1:50
Paramu197312-Jun-12 1:50 
AnswerRe: Getting MdiParent Instance From MdiChild - Windows Forms Application Pin
Richard MacCutchan12-Jun-12 2:15
mveRichard MacCutchan12-Jun-12 2:15 
GeneralRe: Getting MdiParent Instance From MdiChild - Windows Forms Application Pin
Paramu197312-Jun-12 2:30
Paramu197312-Jun-12 2:30 
GeneralRe: Getting MdiParent Instance From MdiChild - Windows Forms Application Pin
Richard MacCutchan12-Jun-12 3:03
mveRichard MacCutchan12-Jun-12 3:03 
GeneralRe: Getting MdiParent Instance From MdiChild - Windows Forms Application Pin
Paramu197315-Jun-12 1:30
Paramu197315-Jun-12 1:30 
QuestionHow to convert from 'WCHAR' to 'System::String ^' Managed C++ Pin
ptr_Electron7-Jun-12 20:08
ptr_Electron7-Jun-12 20:08 
AnswerRe: How to convert from 'WCHAR' to 'System::String ^' Managed C++ Pin
Richard MacCutchan7-Jun-12 21:58
mveRichard MacCutchan7-Jun-12 21:58 
Questionint x = (Int32) thisisObjectType is boxing, if the type is already checked ? Pin
ptr_Electron7-Jun-12 18:15
ptr_Electron7-Jun-12 18:15 
AnswerRe: int x = (Int32) thisisObjectType is boxing, if the type is already checked ? Pin
Richard MacCutchan8-Jun-12 0:34
mveRichard MacCutchan8-Jun-12 0:34 
QuestionVS2010 Error()- Method or Property 'VCFilter::get_Url Behavior' deprecated and nolonger implemented Pin
Paramu19736-Jun-12 23:57
Paramu19736-Jun-12 23:57 
QuestionC++ CX and WPF Pin
Super Lloyd6-Jun-12 15:04
Super Lloyd6-Jun-12 15:04 
QuestionStrong vs static typing Pin
elelont25-Jun-12 9:37
elelont25-Jun-12 9:37 
AnswerRe: Strong vs static typing Pin
Paul Conrad5-Jun-12 10:37
professionalPaul Conrad5-Jun-12 10:37 

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.