Void Sample
This sample demonstrates how to pass data to an unmanaged function that expects a void pointer as an argument. The sample provides two solutions.
The Void sample uses the following unmanaged function, shown with its original function declaration:
SetData exported from PinvokeLib.dll.
void SetData(DataType typ, void* object)
PInvokeLib.dll is a custom unmanaged library that contains an implementation for the previously listed function.
In this sample, the LibWrap class contains an enumeration of types and two managed prototype methods: SetData and SetData2. These methods represent the following approaches for passing data to an unmanaged function expecting a void*:
SetData declares the DataType enumeration and an object. The MarshalAsAttribute attribute sets the UnmanagedType enumeration to AsAny, which determines the type of an object at run time and marshals the object as that type.
SetData2 overloads the method to declare the DataType enumeration and to identify a double or string type. The ref (ByRef in Visual Basic) keyword passes the double by reference.
The App class calls the methods and initializes the enumeration elements. The first approach specifies each enumeration element; the second approach specifies only the largest value type and the string.
Declaring Prototypes
Public Class LibWrap
Public Enum DataType
DT_I2 = 1
DT_I4
DT_R4
DT_R8
DT_STR
End Enum
' Uses AsAny when void* is expected.
Declare Sub SetData Lib "..\LIB\PInvokeLib.dll" ( _
ByVal t As DataType, _
<MarshalAs(UnmanagedType.AsAny)> ByVal o As Object)
' Uses overloading when void* is expected.
Overloads Declare Sub SetData2 Lib "..\LIB\PInvokeLib.dll" Alias _
"SetData" (ByVal t As DataType, ByRef d As Double)
Overloads Declare Sub SetData2 Lib "..\LIB\PInvokeLib.dll" Alias _
"SetData" (ByVal t As DataType, ByVal s As String)
End Class
public class LibWrap
{
public enum DataType
{
DT_I2 = 1,
DT_I4,
DT_R4,
DT_R8,
DT_STR
}
// Uses AsAny when void* is expected.
[DllImport( "..\\LIB\\PInvokeLib.dll" )]
public static extern void SetData(DataType t,
[MarshalAs(UnmanagedType.AsAny)] object o);
// Uses overloading when void* is expected.
[DllImport("..\\LIB\\PInvokeLib.dll", EntryPoint="SetData")]
public static extern void SetData2(DataType t, ref double i);
[DllImport("..\\LIB\\PInvokeLib.dll", EntryPoint="SetData")]
public static extern void SetData2(DataType t, string s);
}
public ref class LibWrap
{
public:
enum class DataType
{
DT_I2 = 1,
DT_I4,
DT_R4,
DT_R8,
DT_STR
};
// Uses AsAny when void* is expected.
[DllImport("..\\LIB\\PInvokeLib.dll")]
static void SetData(DataType t,
[MarshalAs(UnmanagedType::AsAny)] Object^ o);
// Uses overloading when void* is expected.
[DllImport("..\\LIB\\PInvokeLib.dll", EntryPoint="SetData")]
static void SetData2( DataType t, double% i);
[DllImport("..\\LIB\\PInvokeLib.dll", EntryPoint="SetData")]
static void SetData2( DataType t, String^ s);
};
Calling Functions
Public Class App
Public Shared Sub Main()
Console.WriteLine("Calling SetData using AsAny..." + vbNewLine)
LibWrap.SetData(LibWrap.DataType.DT_I2, CShort(12))
LibWrap.SetData(LibWrap.DataType.DT_I4, CLng(12))
LibWrap.SetData(LibWrap.DataType.DT_R4, CSng(12))
LibWrap.SetData(LibWrap.DataType.DT_R8, CDbl(12))
LibWrap.SetData(LibWrap.DataType.DT_STR, "abcd")
Console.WriteLine(vbNewLine + "Calling SetData using overloading...")
Console.WriteLine(vbNewLine)
Dim d As Double = 12
LibWrap.SetData2(LibWrap.DataType.DT_R8, d)
LibWrap.SetData2(LibWrap.DataType.DT_STR, "abcd")
End Sub
End Class
public class App
{
public static void Main()
{
Console.WriteLine("Calling SetData using AsAny... \n");
LibWrap.SetData(LibWrap.DataType.DT_I2, (short)12);
LibWrap.SetData(LibWrap.DataType.DT_I4, (long)12);
LibWrap.SetData(LibWrap.DataType.DT_R4, (float)12);
LibWrap.SetData(LibWrap.DataType.DT_R8, (double)12);
LibWrap.SetData(LibWrap.DataType.DT_STR, "abcd");
Console.WriteLine("\nCalling SetData using overloading... \n");
double d = 12;
LibWrap.SetData2(LibWrap.DataType.DT_R8, ref d);
LibWrap.SetData2(LibWrap.DataType.DT_STR, "abcd");
}
}
public class App
{
public:
static void Main()
{
Console::WriteLine("Calling SetData using AsAny... \n");
LibWrap::SetData(LibWrap::DataType::DT_I2, (short)12);
LibWrap::SetData(LibWrap::DataType::DT_I4, (long)12);
LibWrap::SetData(LibWrap::DataType::DT_R4, (float)12);
LibWrap::SetData(LibWrap::DataType::DT_R8, (double)12);
LibWrap::SetData(LibWrap::DataType::DT_STR, "abcd");
Console::WriteLine("\nCalling SetData using overloading... \n");
double d = 12;
LibWrap::SetData2(LibWrap::DataType::DT_R8, d);
LibWrap::SetData2(LibWrap::DataType::DT_STR, "abcd");
}
};