class Machine
{
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate int delConnectionCallback(IntPtr caller, int MachineIndex);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate int delDisconnectionCallback(IntPtr caller, int MachineIndex));
private static void Run()
{
// random machine for test purposes
int machineIndex = 12;
// Return the memory address of the functions where the callback will happen
// These will be passed to the MachineDriverDLL class so that the C++ Driver DLL
// knows where to return the call
IntPtr ptrConnect = Marshal.GetFunctionPointerForDelegate(OnConnCallback);
IntPtr ptrDisconn = Marshal.GetFunctionPointerForDelegate(OnDiscCallback);
// map the machine dll object
var m = new MachineDriverDll();
// connect to the machine
m.Connect(machineIndex, ptrConnect, ptrDisconnect);
}
// Connect call from the DLL driver
private static delConnectionCallback OnConnCallback = (caller, MachineIndex) =>
{
Log(MachineIndex);
// more long code here
return 0;
};
// Disconnect Call from the DLL driver
private static delDisconnectionCallback OnDiscCallback = (caller, MachineIndex) =>
{
Log(MachineIndex);
// more long code here
return 0;
};
}
OnConnCallback and OnDiscCallback are called from a C++ DLL. How do I structure the code so that the two functions are invoked in separate threads, in an asynchronous manner, without interrupting the for loop?
Currently, the for loop stops counting when either of the callbacks fire.
all you need to do is to update this line of code -
m.Connect(machineIndex, ptrConnect, ptrDisconnect);
to
System.Threading.Tasks.Task.Factory.StartNew(() => m.Connect(machineIndex, ptrConnect, ptrDisconnect));
This way, it starts your C++ code on a separate thread instead of on current running thread. Just make sure that you exit/dispose your C++ code cleanly before you exit your C# code.
Related
I am using a c++ dll to do some background computation and I am trying to get it to report progress back to my calling C# code.
To do that, I've registered a callback method that accepts a StringBuilder as a parameter (found on the web that this was a proper way of doing it).
Here is my ´c++´ code :
// --------------------------------------------
// ----------------- C++ CODE -----------------
// --------------------------------------------
// ----------------- dll api methods
// a custom class to contain some progress report stuff... basically, most important is
// that it contains the callback as ProgressCallback _callback;
CustomEventHandler* _eventHandler = NULL;
// definition of the callback type
typedef void(__stdcall* ProgressCallback)(char* log);
// method to register the callback method
int __stdcall SetCallbackFunction(ProgressCallback callback) {
// from https://stackoverflow.com/a/41910450/2490877
#pragma EXPORT_FUNCTION
// I encapsulated the callback into a custom class
_eventHandler = new CustomEventHandler();
_eventHandler->setEventHandler(callback);
// test all is ok => no problem at this stage, all works great, the
// passed-in callback is called with correct message.
logToCallback("All is ok while testing the method. So far so good!!");
return 0;
}
// the long and slow method (note that I might call it several times from c# during the
// one run
int __stdcall DoLooongStuff() {
// from https://stackoverflow.com/a/41910450/2490877
#pragma EXPORT_FUNCTION
// ------ this is a LOOOONG method that regualrly logs stuff via the callback,
// here an example....
char buf[1000];
sprintf_s(buf, "This is a sample progress log with some formats :%i %i %g", 1, 2, 3.1415);
logToCallback(buf);
// --- the above works a few times without any problem
return 0;
}
//--- this is a static method I use to send progress messages back
static void logToCallback(char* message) {
if (_eventHandler) {
_eventHandler->logToCallback(message);
}
}
// --------------- CustomEventHandlerClass
// ------- class declaration ------
class CustomEventHandler {
public:
void setEventHandler(ProgressCallback callback);
void logToCallback(char* message);
protected:
ProgressCallback _callback;
}
// ----- class implementation
// set the callback method
void CustomEventHandler::setEventHandler(ProgressCallback callback) {
_callback = callback;
}
void CustomEventHandler::logToCallback(char* message) {
if (_callback) {
_callback(message); // <========= this is where the debugger stops:
// no more info than the annoying System.ExecutionEngineException...
// I've tried to pass a constant message like "1234" but got the same issue...
//_callback("1234");
// if however I remove the call to the callback, I don't get errors
// (I know this doesn't mean I don't have any...)
}
}
Now for the calling c# code, I'm using the following code:
// --------------------------------------------
// ----------------- C# CODE ------------------
// --------------------------------------------
// ----- the delegate type to be passed to the dll
public delegate bool CallbackFunction([MarshalAs(UnmanagedType.LPStr)] StringBuilder log);
// ----- prepare to load the dll's methods (I only show the SetCallback code here, other api methods
// are declared and loaded the same way)
private delegate int _SetCallbackFunction_(CallbackFunction func);
private _SetCallbackFunction_ SetCallbackFunction_Dll;
public int SetCallbackFunction(CallbackFunction func) {
return SetCallbackFunction_Dll(func);
}
// loading methods
private T GetDelegate<T>(string procName) where T : class {
IntPtr fp = GetProcAddress(_dllHandle, procName);
return fp != IntPtr.Zero ? Marshal.GetDelegateForFunctionPointer(fp, typeof(T)) as T : null;
}
async Task loadDllMethods() {
// load the delegates => great, it works!
SetCallbackFunction_Dll = GetDelegate<_SetCallbackFunction_>("SetCallbackFunction");
// set callback in DLL, calls the delegate once successfully...
SetCallbackFunction(cSharpCallback);
await doTask();
}
async Task doTask() {
// start the long dll method, that fires the callback to monitor progress
while (someConditionIsMet) {
DoLooongStuff(); // => calls the dll with registered callback!
}
}
// the actual callback
bool cSharpCallback(StringBuilder strBuilder) {
// this is called a few times successfully with the expected message!
Console.WriteLine(strBuilder.ToString());
return true;
}
I've searched over differrent threads to find out the error. I had an error due to too small ´buf´ sizes, so I just made sure it is large enough. I've also teted that ´&_callback´ is always pointing at the same place (it is!).
I am out of search options, any help will be much appreciated. Note that I am note an expert in dll integration, Marshalling etc, I put references I found the hints!
You need to insure the delegate you pass to C++ is alive as long as the callback is being used in C++. You are responsible for holding delegate C# object alive as long as the corresponding C++ callback is used:
someField = new CallbackFunction(cSharpCallback);
SetCallbackFunction(someField);
Better yet, just use Scapix Language Bridge, it generates C++ to C# bindings (including callbacks), completely automatically. Disclaimer: I am the author of Scapix Language Bridge.
I found the answer to my question, thanks to this post:
In order to keep the unmanaged function pointer alive (guarding against GC), you need to hold an instance of the delegate in a variable
So the modified code is ONLY in C#
// -------------- PREVIOUS CODE
async Task loadDllMethods() {
// load the delegates => great, it works!
SetCallbackFunction_Dll = GetDelegate<_SetCallbackFunction_>("SetCallbackFunction");
// set callback in DLL, calls the delegate once successfully...
SetCallbackFunction(cSharpCallback);
await doTask();
}
// -------------- WORKING CODE!!!!
// add static reference....
static CallbackFunction _callbackInstance = new CallbackFunction(cSharpCallback); // <==== Added reference to prevent GC!!!
async Task loadDllMethods() {
// load the delegates => great, it works!
SetCallbackFunction_Dll = GetDelegate<_SetCallbackFunction_>("SetCallbackFunction");
// create callback!!!
SetCallbackFunction(_callbackInstance); // <====== pass the instance here, not the method itself!!!
await doTask();
}
NOTE: I also changed StringBuilder to string !
I am wrapping a C++ library to be used from .NET. There are functions in the C++ API that return std::future. I want to have the .NET proxy classes return System.Threading.Tasks.Task.
I thought of adding a replacement method on the C++ side that would take in a function pointer in addition to the methods parameters. I could then start up a new thread (using std::async for example) and have that wait on std::future::get. Once std::future::get returned I could call the passed in function pointer. On the C# side I would pass the a pointer to a function that would complete the returned Task. Something like:
Task CallCpp(int a, int b) {
TaskCompletionSource<int> tcs;
Action callback = () => tcs.SetResult(0);
IntPtr callbackPtr = Marshal.GetFunctionPointerForDelegate(callback);
CppMethod(callbackPtr, a, b);
return tcs.Task;
}
[DllExport]
external void CppMethod(IntPtr callback, int a, int b);
void CppMethod(void (*callback)(), int a, int b) {
std::future f = realmethod(a, b);
std::async([&]{ f.get; callback(); });
}
std::future realmethod(int a, int b);
(Yes, the code has memory management issues. It should be enough to get the idea across though)
Marshalling asynchronous completion or failure is relatively easy part. I would personally use a COM interface implemented in C#, marked [InterfaceType( ComInterfaceType.InterfaceIsIUnknown )], with 2 methods like setCompleted() and setFailed( HRESULT status ), and pass it when I start the task, but that’s a matter of taste.
The problem is that C++ multithreading implementation is lacking.
Boost is better in that regard, it’s future class has then method that can be used to propagate the status without blocking the thread.
Currently, standard C++ futures don’t offer anything comparable. I think you're out of luck.
If you can modify that C++ library, change the API to something that makes more sense, i.e. can notify about the completion in non-blocking manner.
Without COM, lifetime management is complicated. After you’ve launched the async task but before it completes, someone on C# side needs to retain that function pointer. Here’s a possible solution, note how it uses GCHandle to make callback outlive the task:
[UnmanagedFunctionPointer( CallingConvention.Cdecl )]
delegate void pfnTaskCallback( int hResult );
[DllImport( "my.dll" )]
static extern void launch( [MarshalAs( UnmanagedType.FunctionPtr )] pfnTaskCallback completed );
public static async Task runAsync()
{
var tcs = new TaskCompletionSource<bool>();
pfnTaskCallback pfn = delegate ( int hr )
{
if( hr >= 0 ) // SUCEEDED
tcs.SetResult( true );
else
tcs.SetException( Marshal.GetExceptionForHR( hr ) );
};
var gch = GCHandle.Alloc( pfn, GCHandleType.Normal );
try
{
launch( pfn );
await tcs.Task;
}
finally
{
gch.Free();
}
}
I passed a delegate (logging function) to unmanaged code and it works fine, unless it's called from another thread, which is internal to the C++ library.
I read this, but I can't find any information on what shall I expect when C++ threads are involved. It's definitely threading problem, since delegate works when I call it from the same thread which called unmanaged function from Unity.
C#
public class SomeClass : MonoBehaviour
{
public delegate void LibLogHandler(int a);
private LibLogHandler handlerDelegate;
[DllImport ("mylibrary")]
private static extern bool library_init (string arg1, string arg2, LibLogHandler logHandler);
public void goNow()
{
this.handlerDelegate = new LibLogHandler (this.logHandler);
bool res = library_init ("someArg1", "someArg2", this.handlerDelegate);
Debug.Log ("library init: " + res);
}
void logHandler(int a)
{
Debug.Log ("[library] " + a);
}
}
C++ header
extern "C" {
typedef void (*LibLog) (int a);
bool library_init(const char* arg1, const char* arg3, LibLog libLog);
}
C++ source
static boost::asio::io_service IoService;
static boost::thread aThread;
static boost::shared_ptr<boost::asio::io_service::work> ThreadWork;
bool library_init(const char* arg1, const char* arg2, LibLog libLog)
{
#ifdef THREAD_ENABLED
ThreadWork.reset(new boost::asio::io_service::work(IoService));
aThread = boost::thread([](){
IoService.run();
});
IoService.dispatch([libLog]{
libLog(100);
});
#else
libLog(10);
#endif
return true;
}
So, when THREAD_ENABLED is defined, application silently crashes with no additional information.
I figured out the problem - this article was useful to understand the peculiarities of callbacks invoked from native plugins. In short, in managed code (C#) one just need to make sure the delegate object is persistent (referenced) for the period while it can be invoked from unmanaged code. No need to worry about pinning the memory using GCHandle, since managed runtime environment automatically creates an unmanaged stub handle for the delegate object, which maintains its' address regardless of garbage collector operations. This stub handle is passed to the unmanaged function behind the scenes.
Unity doesn't support threading and you can't use it's API in another threads.
Try using
bool res;
try{
res = library_init ("someArg1", "someArg2", this.handlerDelegate);
}catch (System.Exception e){
Debug.LogError(e.Message);
throw;
}
to get whats wrong with your thread.
EDIT
I wonder if my first statement is true in your case. I think Debug.Log() should be safe. If that's the case you should debug your C++ thread and look why it's crashing.
What's a callback and how is it implemented in C#?
I just met you,
And this is crazy,
But here's my number (delegate),
So if something happens (event),
Call me, maybe (callback)?
In computer programming, a callback is executable code that is passed as an argument to other code.
—Wikipedia: Callback (computer science)
C# has delegates for that purpose. They are heavily used with events, as an event can automatically invoke a number of attached delegates (event handlers).
A callback is a function that will be called when a process is done executing a specific task.
The usage of a callback is usually in asynchronous logic.
To create a callback in C#, you need to store a function address inside a variable. This is achieved using a delegate or the new lambda semantic Func or Action.
public delegate void WorkCompletedCallBack(string result);
public void DoWork(WorkCompletedCallBack callback)
{
callback("Hello world");
}
public void Test()
{
WorkCompletedCallBack callback = TestCallBack; // Notice that I am referencing a method without its parameter
DoWork(callback);
}
public void TestCallBack(string result)
{
Console.WriteLine(result);
}
In today C#, this could be done using lambda like:
public void DoWork(Action<string> callback)
{
callback("Hello world");
}
public void Test()
{
DoWork((result) => Console.WriteLine(result));
DoWork(Console.WriteLine); // This also works
}
Definition
A callback is executable code that
is passed as an argument to other code.
Implementation
// Parent can Read
public class Parent
{
public string Read(){ /*reads here*/ };
}
// Child need Info
public class Child
{
private string information;
// declare a Delegate
delegate string GetInfo();
// use an instance of the declared Delegate
public GetInfo GetMeInformation;
public void ObtainInfo()
{
// Child will use the Parent capabilities via the Delegate
information = GetMeInformation();
}
}
Usage
Parent Peter = new Parent();
Child Johny = new Child();
// Tell Johny from where to obtain info
Johny.GetMeInformation = Peter.Read;
Johny.ObtainInfo(); // here Johny 'asks' Peter to read
Links
more details for C#.
A callback is a function pointer that you pass in to another function. The function you are calling will 'callback' (execute) the other function when it has completed.
Check out this link.
If you referring to ASP.Net callbacks:
In the default model for ASP.NET Web
pages, the user interacts with a page
and clicks a button or performs some
other action that results in a
postback. The page and its controls
are re-created, the page code runs on
the server, and a new version of the
page is rendered to the browser.
However, in some situations, it is
useful to run server code from the
client without performing a postback.
If the client script in the page is
maintaining some state information
(for example, local variable values),
posting the page and getting a new
copy of it destroys that state.
Additionally, page postbacks introduce
processing overhead that can decrease
performance and force the user to wait
for the page to be processed and
re-created.
To avoid losing client state and not
incur the processing overhead of a
server roundtrip, you can code an
ASP.NET Web page so that it can
perform client callbacks. In a client
callback, a client-script function
sends a request to an ASP.NET Web
page. The Web page runs a modified
version of its normal life cycle. The
page is initiated and its controls and
other members are created, and then a
specially marked method is invoked.
The method performs the processing
that you have coded and then returns a
value to the browser that can be read
by another client script function.
Throughout this process, the page is
live in the browser.
Source: http://msdn.microsoft.com/en-us/library/ms178208.aspx
If you are referring to callbacks in code:
Callbacks are often delegates to methods that are called when the specific operation has completed or performs a sub-action. You'll often find them in asynchronous operations. It is a programming principle that you can find in almost every coding language.
More info here: http://msdn.microsoft.com/en-us/library/ms173172.aspx
Dedication to LightStriker:
Sample Code:
class CallBackExample
{
public delegate void MyNumber();
public static void CallMeBack()
{
Console.WriteLine("He/She is calling you. Pick your phone!:)");
Console.Read();
}
public static void MetYourCrush(MyNumber number)
{
int j;
Console.WriteLine("is she/he interested 0/1?:");
var i = Console.ReadLine();
if (int.TryParse(i, out j))
{
var interested = (j == 0) ? false : true;
if (interested)//event
{
//call his/her number
number();
}
else
{
Console.WriteLine("Nothing happened! :(");
Console.Read();
}
}
}
static void Main(string[] args)
{
MyNumber number = Program.CallMeBack;
Console.WriteLine("You have just met your crush and given your number");
MetYourCrush(number);
Console.Read();
Console.Read();
}
}
Code Explanation:
I created the code to implement the funny explanation provided by LightStriker in the above one of the replies. We are passing delegate (number) to a method (MetYourCrush). If the Interested (event) occurs in the method (MetYourCrush) then it will call the delegate (number) which was holding the reference of CallMeBack method. So, the CallMeBack method will be called. Basically, we are passing delegate to call the callback method.
Please let me know if you have any questions.
Probably not the dictionary definition, but a callback usually refers to a function, which is external to a particular object, being stored and then called upon a specific event.
An example might be when a UI button is created, it stores a reference to a function which performs an action. The action is handled by a different part of the code but when the button is pressed, the callback is called and this invokes the action to perform.
C#, rather than use the term 'callback' uses 'events' and 'delegates' and you can find out more about delegates here.
callback work steps:
1) we have to implement ICallbackEventHandler Interface
2) Register the client script :
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
String callbackScript = "function UseCallBack(arg, context)" + "{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallBack", callbackScript, true);
1) from UI call Onclient click call javascript function for EX:- builpopup(p1,p2,p3...)
var finalfield= p1,p2,p3;
UseCallBack(finalfield, ""); data from the client passed to server side by using UseCallBack
2) public void RaiseCallbackEvent(string eventArgument) In eventArgument we get the passed data
//do some server side operation and passed to "callbackResult"
3) GetCallbackResult() // using this method data will be passed to client(ReceiveServerData() function) side
callbackResult
4) Get the data at client side:
ReceiveServerData(text) , in text server response , we wil get.
A callback is a function passed as an argument to another function. This technique allows a function to invoke the parameter function argument and even to pass a value back to the caller. A callback function can be designed to run before/after the function has finished and can pass a value.
It is a kind of construct where you call a long running function and ask him to call you back once it has finished with can return a parameter result to the caller.
It's like someone calls you in the middle of your work asking for status and you say "you know what give me 5 min and i will call you back" and at the end you call him to update. If you are a function the caller just added and passed another function that you invoked at the end. This can simpley be written in C# as:
public void VinodSrivastav(Action statusUpdate){
//i am still here working..working
//i have finished, calling you
statusUpdate();
}
//invokes
stackoverflow.VinodSrivastav((cam) => {
Console.Write("Is it finished");
});
The one simple example is the iterator function where the return will be multiple times, one can argue that we have yield for it:
public void IntreationLoop(int min, int max,Action<int> Callback)
{
for(int i = min;i<= max;i++)
Callback(i);
}
//call
IntreationLoop(5,50,(x) => { Console.Write(x); }); //will print 5-50 numbers
In the code above the function return type is void but it has an Action<int> callback which is called and sends each item from the loop to the caller.
The same thing can be done with if..else or try..catch block as:
public void TryCatch(Action tryFor,Action catchIt)
{
try{
tryFor();
}
catch(Exception ex)
{
Console.WriteLine($"[{ex.HResult}] {ex.Message}");
catchIt();
}
}
And call it as:
TryCatch(()=>{
int r = 44;
Console.WriteLine("Throwing Exception");
throw new Exception("something is wrong here");
}, ()=>{
Console.WriteLine("It was a mistake, will not try again");
});
In 2022 we have Func & Action doing the same, please see the demo code below which shows how this can be be used:
void Main()
{
var demo = new CallbackDemo();
demo.DoWork(()=> { Console.WriteLine("I have finished the work"); });
demo.DoWork((r)=> { Console.WriteLine($"I have finished the work here is the result {r}"); });
demo.DoWork(()=> { Console.WriteLine($"This is passed with func"); return 5;});
demo.DoWork((f)=> { Console.WriteLine($"This is passed with func and result is {f}"); return 10;});
}
// Define other methods and classes here
public class CallbackDemo
{
public void DoWork(Action actionNoParameter)
{
int a = 5;
int b = 10;
//i will do th maths and call you back
int result = a + b;
//callback
actionNoParameter(); //execute
Console.WriteLine($"[The Actual Result is {result}]");
}
public void DoWork(Action<int> actionWithParameter)
{
int a = 5;
int b = 10;
//i will do th maths and call you back
int result = a + b;
//callback
actionWithParameter(result); //execute
Console.WriteLine($"[The Actual Result is {result}]");
}
public void DoWork(Func<int> funcWithReturn)
{
int a = 5;
int b = 10;
//i will do th maths and call you back
int result = a + b;
//callback
int c = funcWithReturn(); //execute
result += c;
Console.WriteLine($"[The Actual Result is {result}]");
}
public void DoWork(Func<int,int> funcWithParameter)
{
int a = 5;
int b = 10;
//i will do th maths and call you back
int result = a + b;
//callback
result += funcWithParameter(result); //execute
Console.WriteLine($"[The Actual Result is {result}]");
}
}
I am writing a C# app on Windows CE 6 to monitor a 3G modem. The app will call functions in a C DLL to access the modem.
In startup, C# app will call this function to create a new connection:
[DllImport("swmodem.dll", CallingConvention = CallingConvention.Winapi)]
public static extern int CreateDataConnection(EVENT_CALLBACK callback);
The EVENT_CALLBACK is defined as:
public delegate void EVENT_CALLBACK(int e, IntPtr data);
A data structure is also defined:
[StructLayout(LayoutKind.Sequential)]
public struct ECIO_INFO
{
public UInt32 ecio1; /*!< Primary scramble code */
public UInt32 ecio2; /*!< Received signal code power */
public UInt32 ecio3; /*!< Energy per chip per power density */
}
In C DLL, a function pointer is passed in CreateDataConnection() for modem status update.
int CreateDataConnection(EVENT_CALLBACK ecb)
{
.
.
fEventCallback = ecb;
// Create a connection
.
.
}
After a connection is created, the DLL will invoke the callback function to update the modem status, for example EC/IO (The ratio of received pilot energy).
Basically, when ECIO changes, the callback function is called to pass the ECIO data to C# app:
In C DLL:
void ProcessNotification(EVENT_CALLBACK fEventCallback)
{
ECIO_INFO ecio_info;
ecio_info.ecio1 = ecio_info.ecio2 = ecio_info.ecio3 = 0;
if(data.nNumOfCells>0)
ecio_info.ecio1 = data.arCellInfo[0].nEcIo;
if(data.nNumOfCells>1)
ecio_info.ecio2 = data.arCellInfo[1].nEcIo;
if(data.nNumOfCells>2)
ecio_info.ecio3 = data.arCellInfo[2].nEcIo;
if(data.nNumOfCells>0)
fEventCallback(ME_RSCP_ECIO, &ecio_info);
}
In C# app, the callback function is defined as:
private void ModemEventCallback(int e, IntPtr data)
{
.
.
Modem.ECIO_INFO new_reinfo = new Modem.ECIO_INFO();
new_reinfo = (Modem.ECIO_INFO)Marshal.PtrToStructure(
data, typeof(Modem.ECIO_INFO));
.
.
}
Now, the problem comes. When the program starts, everything is fine, the connection is created ok and EC/IO is being updated. but after running for several hours, the EC/IO update is stopped. After a test, I found it is stopped when the callback is invoke:
fEventCallback(ME_RSCP_ECIO, &ecio_info);
I don't know what has gone wrong here Probably passing a function pointer in a C# DLL invoke just not right way to do it, or some fault is buried in the code?
Since callback function is just pointer for C/C++, callback parameter must be declared as IntPtr. Create EVENT_CALLBACK instance end ensure that it remains alive all time your program runs. Use Marshal.GetFunctionPointerForDelegate Method to convert delecate instance to IntPtr, and pass resulting IntPtr to CreateDataConnection function.
[DllImport("swmodem.dll", CallingConvention = CallingConvention.Winapi)]
public static extern int CreateDataConnection(IntPtr callback);
...
EVENT_CALLBACK c;
c = new EVENT_CALLBACK( ... ); // keep this alive !
...
CreateDataConnection(Marshal.GetFunctionPointerForDelegate(c));
Try this
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void EVENT_CALLBACK(int e, IntPtr data);
It solved my problem.
I think you must use GCHandl.Alloc with GCHandleType.Pinned, by this you will tell gc that this object must remain in memory even though
there might be no "roots" in the application that refer to this object and the memory for this object cannot be compacted