none
C++ COM Component to C# RRS feed

  • Question

  • Hi

    Can anyone please help how can i use C++ COM component into C# Project

    Can you please help with the sample project?

    Thank you for your help!!


    Regards, Ravi Raj Nukala

    Wednesday, January 16, 2019 12:56 PM

Answers

  • A ULONG in C++ is a 32-bit unsigned integral value. Since it is a pointer I'm assuming that the calling function will set the value. Therefore you'd pass the value by reference. A 32-bit unsigned integral value in C# is uint. Something like this perhaps.

    delegate int SetCallbackEvent ( ref uint callEvent );


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, January 26, 2019 6:06 AM
    Moderator

All replies

  • Using COM in C# is fully documented, with examples, in MSDN. Have you looked at the examples and tried them? Basically you add a reference to the COM type and then just use the auto-generated .NET type.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, January 16, 2019 4:34 PM
    Moderator
  • Thank you for the article. It helped!!

    Now i have another problem when calling a callback from the Interop.

    can you please let me know how can i call the callback with the ulong pointer as a argument?

    ex:

    function in C++:

    HRESULT _stdcall SetCallbackEvent(/* [in] */ ULONG *callEvent) = 0;

    How can i call this callback with ulong pointer in c#??

    Please help...Thank you!!


    Regards, Ravi Raj Nukala

    Saturday, January 26, 2019 5:41 AM
  • A ULONG in C++ is a 32-bit unsigned integral value. Since it is a pointer I'm assuming that the calling function will set the value. Therefore you'd pass the value by reference. A 32-bit unsigned integral value in C# is uint. Something like this perhaps.

    delegate int SetCallbackEvent ( ref uint callEvent );


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, January 26, 2019 6:06 AM
    Moderator
  • Thank you for your inputs

    whats the ref i need to send as a parameter?

    how can i see the event triggered when there callback  executed?

    in c++ i am sending event handler(CreateEvent) as a prameter to the SetCallbackEvent. 

    what can be sent as a ref in c# and how can i send as a ref 

    any example will help me a lot.. Thank you once again for the inputs...


    Regards, Ravi Raj Nukala

    Saturday, January 26, 2019 12:10 PM
  • Hi

    Using code help add Unmanaged Dll .

    [DllImport("youDll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int subtract(int a, int b);

            private void button2_Click(object sender, EventArgs e)
            {
                int x = Convert.ToInt32(textBox1.Text);
                int y = Convert.ToInt32(textBox2.Text);
                int z = subtract(x, y);
        
            }

    Regards.

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

     

    Saturday, January 26, 2019 12:27 PM
  • "in c++ i am sending event handler(CreateEvent) as a prameter to the SetCallbackEvent. "
    Wait what? Even in C++ you couldn't pass a function to that callback and have it called. The function is requiring a ULONG*, not a function delegate. If you did a typecast it might work because you're using a pointer but both sides would have to do a cast and it wouldn't make sense.

    Can you post the original C++ code you are trying to replicate in C# including the example of how it is called in C++ and how the callback function works?


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, January 26, 2019 4:42 PM
    Moderator