-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
40 lines (34 loc) · 1.17 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Runtime.InteropServices;
namespace Marshalling
{
class Program
{
internal static class Import
{
[DllImport(@"MarshallingDLL.dll")]
internal static extern IntPtr getData();
}
// Declares a managed prototype for unmanaged function.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class pItem
{
public int a;
[MarshalAs(UnmanagedType.LPStr)] //we read const char*
public string str;
public IntPtr items; //store a pointer
}
public static pItem getData()
{
pItem testItem = new pItem { a = 0, str = "undefined", items = IntPtr.Zero }; //initialize a pItem
IntPtr ItemPtr = Import.getData(); //get a pointer from dll
testItem = Marshal.PtrToStructure<pItem>(ItemPtr); //read data located at a pointer
return testItem;
}
static void Main(string[] args)
{
pItem myItem = getData();
Console.WriteLine(myItem.a.ToString() + "; " + myItem.str + "; " + myItem.items.ToString());
}
}
}