There's this external function I call in order to receive data updates from a DLL. The DLL basically sends something as soon as sth gets updated on the DLL side.
// Definition:
partial class DLLConnector
{
// Initializing the DLL:
[DllImport(dll_path, CallingConvention = CallingConvention.StdCall)]
public static extern sbyte InitializeDLL(TUpdatedDataCallback updateDataCallback);
// External function that generates the updated messages:
[DllImport(dll_path, CallingConvention = CallingConvention.StdCall)]
public static extern sbyte GetDataUpdate([MarshalAs(UnmanagedType.LPWStr)] string dataToBeSubscribed);
// Delegate handling:
public delegate void TUpdatedDataCallback([MarshalAs(UnmanagedType.LPWStr)] string updatedData);
public static TUpdatedDataCallback _updatedDataCallBack = new TUpdatedDataCallback(DataUpdateCallback);
}
// DLL can be called by GetData():
partial class DLLConnector
{
string activeDataPipeName = "dataPipe1";
public static void GetData(string dataPipeName)
sbyte retVal = GetDataUpdate(activeDataPipeName) // retVal: just an info whether the call was made successfully or not
}
A delegate is properly declared in order to handle the message received from the DLL (which, by the way, is being shown here a simple string just for the sake of simplicity) and I'm able to process it with the following function:
public static void DataUpdateCallback([MarshalAs(UnmanagedType.LPWStr)] string updatedData)
{
Console.Writeline(updatedData); // This is working fine
// Other code
}
What I'm trying to do is to store this data by adding each message received from the DLL to a List that is declared somewhere outside the callback function declaration.
The GetDataUpdate
function only returns an sbyte that informs whether the call to the DLL has successfully been made, so there's no way to handle this by using its return.
My understanding is that, once the DLL calls DataUpdateCallback
every time there's an updated message to be sent over, it wouldn't make a sense to create an instance of some Class within its code block (as shown below), as this would be instantiating a similar object (and with the same name) everytime the DLL calls this function.
public static void DataUpdateCallback([MarshalAs(UnmanagedType.LPWStr)] string updatedData)
{
Console.Writeline(updatedData);
MessageReceived messageList = new();
messageList.AddMessage(a);
}
class MessageReceived
{
List<string> messages;
public MessageReceived()
{
messages = new();
{
public void AddMessage(string a)
{
messages.Add(a);
}
}
Could anyone assist, please? I'm not sure how to figure out this scoping issue, so I could store these messages and allow other functions to retrieve those from the List.
Thanks!