Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently using delegate from c# to C++ dll i am passing the ref struct to get the value of struct from c++ dll,with out using call back i can able to get the struct value using marshalling.but for dynamic purpose i am using Delegate callback.
while i am working i dont know in callback what i need to use i am facing the issue

if i use without ref in update() method
Quote:
Error CS1676 Parameter 1 must be declared with the 'ref' keyword

if i am not using ref in update() method
Quote:
Error CS0246 The type or namespace name 'strNode' could not be found


What I have tried:

In C#
C#
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct NODE
    {
        public ulong ullSector;
        public ulong ullSize;

        unsafe public NODE* pNext;
        unsafe public NODE* pChild;
        unsafe public NODE* pParent;

        public uint dwID;
        public uint dwSelfNum;
        public uint dwParentNum;

        public IntPtr pwcName;
}


above one is my struct


C#
public delegate void ProgressCallback(ref NODE strNode);
    public delegate string GetFilePathCallback(string filter);

  public async void update()
        {
         

            ProgressCallback callback =
                (  ref strNode) => //Here the issues rising
                {
                    Application.Current.Dispatcher.Invoke((Action)delegate
                    {

                        
                      Console.WriteLine("Progress = {0}", strNode.Name.ToString());
                    });
                };

            // call DoWork in C code
            DoWork(callback);
        }


in my c++ dll
XXXX.h

C++
#ifdef __cplusplus
extern "C"
{
#endif

#define DLL __declspec(dllexport)
    typedef void(__stdcall* ProgressCallback)(NODE*);
    typedef char* (__stdcall* GetFilePathCallback)(char* filter);

    DLL void DoWork(ProgressCallback progressCallback);
    DLL void ProcessFile(GetFilePathCallback getPath);

#ifdef __cplusplus
}
#endif


XXXX.cpp
C++
DLL void DoWork(ProgressCallback progressCallback)
{
        // do the work...

        if (progressCallback)
        {
            Sleep(100);
//The data which i want to share
           
        }
       
    }
}
Posted
Updated 22-Feb-21 0:28am

1 solution

You need to specify the parameter type again:
C#
ProgressCallback callback = (ref NODE strNode) =>
{
    ...
NB: There is an open suggestion[^] to improve C# by allowing you to omit the type name with ref/out/in parameters in lambda methods. It has not been implemented yet.
 
Share this answer
 
v2
Comments
Fazi_13 22-Feb-21 6:52am    
Yes now app is running but there is no use,without using ref/out how to get the value of struct from c# to c++ dll.
will u please explain me?
Richard Deeming 22-Feb-21 6:57am    
Your C++ code is passing a pointer to a NODE to your C# code.

If you declare NODE as a struct, then you have to declare the NODE* parameter as ref.

If you don't want to use ref, then make NODE a class instead.
Fazi_13 22-Feb-21 8:14am    
Yes thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900