Click here to Skip to main content
15,914,222 members
Home / Discussions / C#
   

C#

 
AnswerRe: TcpClient - Detecting closed connections Pin
jschell11-Oct-11 9:21
jschell11-Oct-11 9:21 
GeneralRe: TcpClient - Detecting closed connections Pin
Grimes11-Oct-11 19:21
Grimes11-Oct-11 19:21 
QuestionCompare 2 Datatables Pin
jojoba2010-Oct-11 6:13
jojoba2010-Oct-11 6:13 
AnswerRe: Compare 2 Datatables Pin
André Kraak10-Oct-11 8:27
André Kraak10-Oct-11 8:27 
GeneralRe: Compare 2 Datatables Pin
jojoba2010-Oct-11 8:30
jojoba2010-Oct-11 8:30 
GeneralRe: Compare 2 Datatables Pin
André Kraak10-Oct-11 9:30
André Kraak10-Oct-11 9:30 
QuestionFire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils10-Oct-11 4:27
Tonkpils10-Oct-11 4:27 
AnswerRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
DaveyM6910-Oct-11 7:09
professionalDaveyM6910-Oct-11 7:09 
Firstly, if you have control over the C++ code, I would change the last parameter to a pointer type
C++
CAN_SetClientParam(
        WORD        wParam,   //specifies kind of parameter
        DWORD       dwValue); //Event handle

as it will work on a 32 bit system (DWORD is 32 bits) but possibly fail on a 64 bit system where a handle/pointer is 64 bits.

If I understand correctly, the C++ code calls the callback and you want to raise an event in C# on that callback?
That is quite easily done by creating a delegate with the correct signature for the C++ callback and just passing that directly in your C# prototype instead of marshalling. The function that the delegate points to can then raise an event as normal. Something like this (untested, written without an IDE so may have small errors!)

C#
internal delegate void YourDelegate();

C#
internal static class NativeMethods
{
    [DllImport("canapi2.dll", EntryPoint="CAN_SetClientParam")]
    public static extern uint SetClientParam(
        ushort wParam,
        YourDelegate dwValue);
}

C#
public class YourWrapper
{
    public event EventHandler YourEvent;

    private YourDelegate yourDelegate;
    private uint lastCallbackResult;

    public YourWrapper()
    {
        yourDelegate = new YourDelegate(OnYourDelegate);
    }

    public uint LastCallbackResult
    {
        get{ return lastCallbackResult; }
    }

    public void CallUnmanaged(ushort wParam)
    {
        lastCallbackResult = NativeMethods.SetClientParam(wParam, yourDelegate);
    }

    private void OnYourDelegate()
    {
        OnYourEvent(EventArgs.Empty);
    }
    protected virtual void OnYourEvent(EventArgs e)
    {
        EventHandler eh = YourEvent;
        if(eh != null)
            eh(this, e);
    }
}

You may need to change parameters, return types and possibly a custom EventArgs derived class to pass any data that is required from the callback, but the basic layout will be the same.

CallUnmanaged(); will trigger the C#/C++ interop and fire any handlers you attach to YourEvent, the callback result will be available in LastCallbackResult (I would move this to a custom event args rather that caching the value).
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils13-Oct-11 4:37
Tonkpils13-Oct-11 4:37 
AnswerRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
TheGreatAndPowerfulOz10-Oct-11 12:13
TheGreatAndPowerfulOz10-Oct-11 12:13 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils11-Oct-11 1:01
Tonkpils11-Oct-11 1:01 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
TheGreatAndPowerfulOz14-Oct-11 4:51
TheGreatAndPowerfulOz14-Oct-11 4:51 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils18-Oct-11 0:17
Tonkpils18-Oct-11 0:17 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
TheGreatAndPowerfulOz18-Oct-11 8:23
TheGreatAndPowerfulOz18-Oct-11 8:23 
QuestionHow to highlight all occurrences of a word in a Word document?? Pin
fiaolle10-Oct-11 4:20
fiaolle10-Oct-11 4:20 
AnswerRe: How to highlight all occurrences of a word in a Word document?? Pin
Pete O'Hanlon10-Oct-11 4:52
mvePete O'Hanlon10-Oct-11 4:52 
GeneralRe: How to highlight all occurrences of a word in a Word document?? Pin
fiaolle10-Oct-11 5:08
fiaolle10-Oct-11 5:08 
GeneralRe: How to highlight all occurrences of a word in a Word document?? Pin
Pete O'Hanlon10-Oct-11 5:19
mvePete O'Hanlon10-Oct-11 5:19 
GeneralRe: How to highlight all occurrences of a word in a Word document?? Pin
fiaolle10-Oct-11 6:25
fiaolle10-Oct-11 6:25 
GeneralRe: How to highlight all occurrences of a word in a Word document?? Pin
Shameel11-Oct-11 0:32
professionalShameel11-Oct-11 0:32 
AnswerRe: How to highlight all occurrences of a word in a Word document?? Pin
jschell10-Oct-11 8:08
jschell10-Oct-11 8:08 
GeneralRe: How to highlight all occurrences of a word in a Word document?? Pin
fiaolle10-Oct-11 9:07
fiaolle10-Oct-11 9:07 
QuestionIs there a way to run nunit Test class 50 times in a loop ? Pin
Subin Mavunkal10-Oct-11 0:59
Subin Mavunkal10-Oct-11 0:59 
AnswerRe: Is there a way to run nunit Test class 50 times in a loop ? Pin
Pete O'Hanlon10-Oct-11 1:14
mvePete O'Hanlon10-Oct-11 1:14 
GeneralRe: Is there a way to run nunit Test class 50 times in a loop ? Pin
Subin Mavunkal10-Oct-11 1:24
Subin Mavunkal10-Oct-11 1:24 

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.