WEB开发网
开发学院软件开发C语言 c#编程指南(十) 平台调用P-INVOKE完全掌握, 字符串... 阅读

c#编程指南(十) 平台调用P-INVOKE完全掌握, 字符串和指针

 2010-09-30 22:46:12 来源:WEB开发网   
核心提示: 1[DllImport("TestDll",EntryPoint="GetStringParam")]2publicstaticexternvoidGetStringParamByBytes(byte[]outHello,intlen);34[DllImp

 1     [DllImport("TestDll", EntryPoint = "GetStringParam")]
 2     public static extern void GetStringParamByBytes(byte[] outHello, int len);
 3 
 4     [DllImport("TestDll", EntryPoint = "GetStringParam")]
 5     public static extern void GetStringParamByMarshal([Out, MarshalAs(UnmanagedType.LPStr)]StringBuilder outHello, int len);
 6 
 7     [DllImport("TestDll", EntryPoint = "GetStringParamW")]
 8     public static extern void GetStringParamByBytesW(byte[] outHello, int len);
 9 
10     [DllImport("TestDll", EntryPoint = "GetStringParamW")]
11     public static extern void GetStringParamByMarshalW([Out, MarshalAs(UnmanagedType.LPWStr)]StringBuilder outHello, int len);
12 
13 
14     public byte[] _outHello = new byte[10];
15     public byte[] _outHelloW = new byte[20];
16     public StringBuilder _builder = new StringBuilder(10); //很重要设定string的容量。
17  
18     public void Run()
19     {
20       //
21        GetStringParamByBytes(_outHello, _outHello.Length);
22       GetStringParamByMarshal(_builder, _builder.Capacity);
23       GetStringParamByBytesW(_outHelloW, _outHelloW.Length / 2);
24       GetStringParamByMarshalW(_builder, _builder.Capacity);
25 
26       //
27        Console.WriteLine(Encoding.ASCII.GetString(_outHello));
28       Console.WriteLine(_builder.ToString());
29       Console.WriteLine(Encoding.Unicode.GetString(_outHelloW));
30       Console.WriteLine(_builder.ToString());
31      }
32  

上一页  1 2 3 4 

Tags:编程 指南 平台

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接