|
1 | 1 | ---
|
2 | 2 | title: "Marshaling a Delegate as a Callback Method"
|
3 | 3 | ms.date: "03/30/2017"
|
4 |
| -dev_langs: |
| 4 | +dev_langs: |
5 | 5 | - "csharp"
|
6 | 6 | - "vb"
|
7 | 7 | - "cpp"
|
8 |
| -helpviewer_keywords: |
| 8 | +helpviewer_keywords: |
9 | 9 | - "data marshaling, Callback sample"
|
10 | 10 | - "marshaling, Callback sample"
|
11 | 11 | ms.assetid: 6ddd7866-9804-4571-84de-83f5cc017a5a
|
12 | 12 | author: "rpetrusha"
|
13 | 13 | ms.author: "ronpet"
|
14 | 14 | ---
|
15 | 15 | # Marshaling a Delegate as a Callback Method
|
16 |
| -This sample demonstrates how to pass delegates to an unmanaged function expecting function pointers. A delegate is a class that can hold a reference to a method and is equivalent to a type-safe function pointer or a callback function. |
17 |
| - |
| 16 | +This sample demonstrates how to pass delegates to an unmanaged function expecting function pointers. A delegate is a class that can hold a reference to a method and is equivalent to a type-safe function pointer or a callback function. |
| 17 | + |
18 | 18 | > [!NOTE]
|
19 |
| -> When you use a delegate inside a call, the common language runtime protects the delegate from being garbage collected for the duration of that call. However, if the unmanaged function stores the delegate to use after the call completes, you must manually prevent garbage collection until the unmanaged function finishes with the delegate. For more information, see the [HandleRef Sample](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/hc662t8k(v=vs.100)) and [GCHandle Sample](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/44ey4b32(v=vs.100)). |
20 |
| - |
21 |
| - The Callback sample uses the following unmanaged functions, shown with their original function declaration: |
22 |
| - |
23 |
| -- **TestCallBack** exported from PinvokeLib.dll. |
24 |
| - |
25 |
| - ``` |
26 |
| - void TestCallBack(FPTR pf, int value); |
27 |
| - ``` |
28 |
| - |
29 |
| -- **TestCallBack2** exported from PinvokeLib.dll. |
30 |
| - |
31 |
| - ``` |
32 |
| - void TestCallBack2(FPTR2 pf2, char* value); |
33 |
| - ``` |
34 |
| - |
35 |
| - [PinvokeLib.dll](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/as6wyhwt(v=vs.100)) is a custom unmanaged library that contains an implementation for the previously listed functions. |
36 |
| - |
37 |
| - In this sample, the `LibWrap` class contains managed prototypes for the `TestCallBack` and `TestCallBack2` methods. Both methods pass a delegate to a callback function as a parameter. The signature of the delegate must match the signature of the method it references. For example, the `FPtr` and `FPtr2` delegates have signatures that are identical to the `DoSomething` and `DoSomething2` methods. |
38 |
| - |
39 |
| -## Declaring Prototypes |
40 |
| - [!code-cpp[Conceptual.Interop.Marshaling#37](../../../samples/snippets/cpp/VS_Snippets_CLR/conceptual.interop.marshaling/cpp/callback.cpp#37)] |
41 |
| - [!code-csharp[Conceptual.Interop.Marshaling#37](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.interop.marshaling/cs/callback.cs#37)] |
42 |
| - [!code-vb[Conceptual.Interop.Marshaling#37](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.interop.marshaling/vb/callback.vb#37)] |
43 |
| - |
44 |
| -## Calling Functions |
45 |
| - [!code-cpp[Conceptual.Interop.Marshaling#38](../../../samples/snippets/cpp/VS_Snippets_CLR/conceptual.interop.marshaling/cpp/callback.cpp#38)] |
46 |
| - [!code-csharp[Conceptual.Interop.Marshaling#38](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.interop.marshaling/cs/callback.cs#38)] |
47 |
| - [!code-vb[Conceptual.Interop.Marshaling#38](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.interop.marshaling/vb/callback.vb#38)] |
48 |
| - |
| 19 | +> When you use a delegate inside a call, the common language runtime protects the delegate from being garbage collected for the duration of that call. However, if the unmanaged function stores the delegate to use after the call completes, you must manually prevent garbage collection until the unmanaged function finishes with the delegate. For more information, see the [HandleRef Sample](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/hc662t8k(v=vs.100)) and [GCHandle Sample](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/44ey4b32(v=vs.100)). |
| 20 | +
|
| 21 | +The Callback sample uses the following unmanaged functions, shown with their original function declaration: |
| 22 | + |
| 23 | +- `TestCallBack` exported from PinvokeLib.dll. |
| 24 | + |
| 25 | + ```cpp |
| 26 | + void TestCallBack(FPTR pf, int value); |
| 27 | + ``` |
| 28 | + |
| 29 | +- `TestCallBack2` exported from PinvokeLib.dll. |
| 30 | + |
| 31 | + ```cpp |
| 32 | + void TestCallBack2(FPTR2 pf2, char* value); |
| 33 | + ``` |
| 34 | + |
| 35 | +[PinvokeLib.dll](marshaling-data-with-platform-invoke.md#pinvokelibdll) is a custom unmanaged library that contains an implementation for the previously listed functions. |
| 36 | + |
| 37 | +In this sample, the `LibWrap` class contains managed prototypes for the `TestCallBack` and `TestCallBack2` methods. Both methods pass a delegate to a callback function as a parameter. The signature of the delegate must match the signature of the method it references. For example, the `FPtr` and `FPtr2` delegates have signatures that are identical to the `DoSomething` and `DoSomething2` methods. |
| 38 | + |
| 39 | +## Declaring Prototypes |
| 40 | +[!code-cpp[Conceptual.Interop.Marshaling#37](../../../samples/snippets/cpp/VS_Snippets_CLR/conceptual.interop.marshaling/cpp/callback.cpp#37)] |
| 41 | +[!code-csharp[Conceptual.Interop.Marshaling#37](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.interop.marshaling/cs/callback.cs#37)] |
| 42 | +[!code-vb[Conceptual.Interop.Marshaling#37](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.interop.marshaling/vb/callback.vb#37)] |
| 43 | + |
| 44 | +## Calling Functions |
| 45 | +[!code-cpp[Conceptual.Interop.Marshaling#38](../../../samples/snippets/cpp/VS_Snippets_CLR/conceptual.interop.marshaling/cpp/callback.cpp#38)] |
| 46 | +[!code-csharp[Conceptual.Interop.Marshaling#38](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.interop.marshaling/cs/callback.cs#38)] |
| 47 | +[!code-vb[Conceptual.Interop.Marshaling#38](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.interop.marshaling/vb/callback.vb#38)] |
| 48 | + |
49 | 49 | ## See also
|
50 | 50 | - [Miscellaneous Marshaling Samples](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/ss9sb93t(v=vs.100))
|
51 |
| -- [Platform Invoke Data Types](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/ac7ay120(v=vs.100)) |
| 51 | +- [Platform Invoke Data Types](marshaling-data-with-platform-invoke.md#platform-invoke-data-types) |
52 | 52 | - [Creating Prototypes in Managed Code](creating-prototypes-in-managed-code.md)
|
0 commit comments