1

I need to show a live data on text field in C#.basically whenever a value changes, below c++ function under LTscriptenginedll.dll

ConnectLiveData(void* pCallBack, const char** pszTagNames, const int** pnTagAddress,
        bool bdiscconectConnection, bool bPyRequest)

gives new value . I need to create a C# function which will call and takes value from this C++ function everytime value changes in function.I tried but so for I am still struggling.

Below is my code.

     public unsafe float StartLiveInfo(string tagNameFromRequest)
    {
        string tagName = tagNameFromRequest;//"Motor_1.OutPut.VI";
        byte[] tagNameByte = Encoding.ASCII.GetBytes(tagName);
        IntPtr tagNamePtr = Marshal.UnsafeAddrOfPinnedArrayElement(tagNameByte, 0);


        IntPtr[] tagNameIntPtr = new IntPtr[2];
        tagNameIntPtr[0] = tagNamePtr;
        tagNameIntPtr[1] = IntPtr.Zero;

        int tagValueTemp = 0;
        int*[] tagValuePtr = new int*[2];
        tagValuePtr[0] = &tagValueTemp;
        tagValuePtr[1] = null;
        float test;

        fixed (int** tagValue1 = tagValuePtr)
        {

            fixed (IntPtr* tagName1 = tagNameIntPtr)
            {
                try
                {
                    myCallBackPtr obj = new myCallBackPtr(CallMethod);
                    int finalTagValue = ConnectLiveData(obj, tagName1, tagValue1,false,true);
                    test = Convert.ToSingle(*tagValue1[0]);
                    return test;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }
    }

This is how I have called dll function and using delegate.

    public delegate void myCallBackPtr();
    [DllImport("LTscriptenginedll.dll")]
    public static extern unsafe int ConnectLiveData(myCallBackPtr ps,IntPtr* test, int** temp,bool bdiscconectConnection, bool bPyRequest);

Here is callMethod

        public static void CallMethod()
    {
        Console.WriteLine("callbackcalled");
    }

I am calling this StartLiveInfo function from main.aspx on buttonclick event and showing return value in text field.

below it the code:

        protected void Button1_Click(object sender, EventArgs e)
    {
        float retValue = oraDal.StartLiveInfo("M1.Speed");
        TextBox1.Text = Convert.ToString(retValue);
    }
Alok Sharma
  • 95
  • 1
  • 12
  • What exactly is not working ? Might help to diagnose, if you've got an exception / details on the error. And if someone could throw up a dummy c++ project to test it out, I'd be willing to invest some time in trying to figure it out. – Irwene Aug 25 '22 at 13:36
  • And did you check : [PInvoke C#: Function takes pointer to function as argument](https://stackoverflow.com/a/5235549/2245256) ? – Irwene Aug 25 '22 at 13:38

3 Answers3

0

You might need to apply the UnmanagedFunctionPointerAttribute to your myCallbackPtr delegate.

user700390
  • 2,287
  • 1
  • 19
  • 26
0

If your requirement is add C++ code into C# Code, you will need to do some steps:

  1. At solution explorer, right-click in your Project and go to Properties;
  2. In the Build menu, go to General sub-menu and check the option "Unsafe code" like this image bellow: enter image description here
  3. If you end these steps above, good luck! You'll may add C++ code into your C# project without impediments.
Antonio Leonardo
  • 1,805
  • 1
  • 8
  • 18
  • The C++ function of the OP is external (in another DLL that is called through `DllIMport`), you do NOT need to enable unsafe to make those calls. You might need it if your workflow required you to to calculations on pointer, but most of the usual interactions can be done through IntPtr... And OP seems to already have this enabled (see the unsafe in their method's signature) – Irwene Aug 25 '22 at 13:28
  • But check the code shared in the question: the method is declared with `unsafe` modified access and contains C++ code and calculations into IntPtr, the unsafe check marked is required. – Antonio Leonardo Aug 25 '22 at 15:30
  • Yes, but as the issue is not that the code won't build, one can assume the flag is already defined. And that's regular if not uncommon C# code, not C++, he's just tossing some unsafe pointers around :) – Irwene Aug 25 '22 at 15:32
0

Make sure you have the DLL located in the same directory that your executable is in, so that at runtime it can actually find and load the DLL containing your c++ function.