| using System; |
| using System.Runtime.InteropServices; |
| |
| namespace TestApp |
| { |
| class Program |
| { |
| [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| public struct Name |
| { |
| [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] |
| public string FirstName; |
| [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] |
| public string LastName; |
| [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] |
| public string[] Array; |
| }; |
| |
| [DllImport("TestDll.dll")] |
| public static extern void GetName(ref Name name); |
| |
| [DllImport("TestDll.dll")] |
| public static extern void Hello(); |
| |
| static void Main(string[] args) |
| { |
| Hello(); |
| var name = new Name(); |
| GetName(ref name); |
| Console.WriteLine(name.FirstName); |
| foreach (var s in name.Array) |
| Console.WriteLine(s); |
| } |
| } |
| } |
沒有留言:
張貼留言