तो अगर मैं WINAPI के माध्यम से अपने कॉलबैक फ़ंक्शन पर एक सूची पास करना चाहते हैं मैं GCHandle
// object to IntPtr (before calling WinApi):
List<string> list1 = new List<string>();
GCHandle handle1 = GCHandle.Alloc(list1);
IntPtr parameter = (IntPtr) handle1;
// call WinAPi and pass the parameter here
// then free the handle when not needed:
handle1.Free();
// back to object (in callback function):
GCHandle handle2 = (GCHandle) parameter;
List<string> list2 = (handle2.Target as List<string>);
list2.Add("hello world");
Thx David Heffernan संपादित करने के लिए
का उपयोग करें: के रूप में टिप्पणी में बताया गया है, आप मुक्त करने की जरूरत है उपयोग के बाद हैंडल। इसके अलावा मैंने कास्टिंग का इस्तेमाल किया। here जैसे स्थिर मेथगोड्स GCHandle.ToIntPtr(handle1)
और GCHandle.FromIntPtr(parameter)
का उपयोग करना बुद्धिमान हो सकता है। मैंने यह जांच नहीं की है।
इसे GCHandle –