C# 中安全代码与不安全代码
C# 中安全代码与不安全代码 P/Invoke
非托管代码需要在unsafe块中书写。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; namespace IntPtrSample
{
class Program
{
static void Main(string[] args)
{
string stringA = "I seem to be turned around!";
int copylen = stringA.Length; // Allocate HGlobal memory for source and destination strings
IntPtr sptr = Marshal.StringToHGlobalAnsi(stringA);
IntPtr dptr = Marshal.AllocHGlobal(copylen + ); // The unsafe section where byte pointers are used.
unsafe
{
byte* src = (byte*)sptr.ToPointer();
byte* dst = (byte*)dptr.ToPointer(); src[] = (byte)'a';
src[] = (byte)'a';
src[] = (byte)'a';
src[] = (byte)'a'; if (copylen > )
{
// set the source pointer to the end of the string
// to do a reverse copy.
src += copylen - ; while (copylen-- > )
{
*dst++ = *src--;
}
*dst = ;
}
} string stringB = Marshal.PtrToStringAnsi(dptr); Console.WriteLine("Original:\n{0}\n", stringA);
Console.WriteLine("Reversed:\n{0}", stringB); // Free HGlobal memory
Marshal.FreeHGlobal(dptr);
Marshal.FreeHGlobal(sptr); }
}
}
// 参考文章
http://msdn.microsoft.com/en-us/library/system.intptr.topointer.aspx
http://msdn.microsoft.com/en-us/library/etb4xxec.aspx
C# 中安全代码与不安全代码的更多相关文章
- Java中普通代码块,构造代码块,静态代码块区别及代码示例
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 1 //普通代码块 ...
- 网页中嵌入可以点击“运行代码”执行html/css/js代码
html代码 <textarea name="textarea" cols="60" rows="10" id="rn01& ...
- Java中普通代码块,构造代码块,静态代码块执行顺序
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 1 //普通代码块 ...
- 当C#中带有return的TryCatch代码遇到Finally时代码执行顺序
编写的代码最怕出现的情况是运行中有错误出现,但是无法定位错误代码位置.综合<C#4.0图解教程>,总结如下: TryCatchFinally用到的最多的是TryCatch,Catch可以把 ...
- C#开发中使用Npoi操作excel实例代码
C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...
- 2.C#中通过委托Func消除重复代码
阅读目录 一:重复的代码 二:C#中通过委托Func消除重复代码 一:重复代码 public class Persion { public string Name { get; set; } ...
- .NET(C#)中不同级别的安全透明代码对类型的影响
测试代码将测试一个方法和类默认在全部信任权限下和部分信任权限下的代码类型. 上面说的默认就是指未加入其他安全透明类型的特性. 代码类型可以是: 透明代码(Transparent Code) 关键代码( ...
- Java中替换HTML标签的方法代码
这篇文章主要介绍了Java中替换HTML标签的方法代码,需要的朋友可以参考下 replaceAll("\\&[a-zA-Z]{0,9};", "").r ...
- 关于java中普通代码块、构造代码块与静态代码块
1.普通代码块 public static void main(String[] args) { /*普通代码块: *直接定义在在方法或语句中出现”{普通代码的执行语句}“的就称为普通代码块. *普通 ...
- 怎么知道RTL Schematic中的instance与哪段代码对应呢
2013-06-23 20:15:47 ISE综合后可以看到RTL Schematic,但我们知道在RTL编码时,要经常问自己一个问题“我写的这段代码会综合成什么样的电路呢”.对于一个简单的设计,比如 ...
随机推荐
- 项目中的.Net
一.@符号的妙用 1.字符串转义符 源:'\'在C#中是特殊符号,表示转义字符,所有要表示普通字符串'\',则需要用'\\',通过@符号,可以实现'\'当做普通字符使用,如下: str ...
- Cocos2dx 粒子销毁问题
Cocos2dx 粒子销毁问题 DionysosLai(906391500@qq.com) 2014-7-3 之前在调试粒子特效时,在粒子编辑器有个选项是用来调整粒子的生命时间,当粒子存在的时间 ...
- PV、UV
1.什么是PV值 PV(page view)即页面浏览量或点击量,是衡量一个网站或网页用户访问量.具体的说,PV值就是所有访问者在24小时(0点到24点)内看了某个网站多少个页面或某个网页多少次.PV ...
- Gentoo:startx出现Failed to load module问题
安装完xorg-server后,startx启动桌面环境,出现缺少模块错误. 查看log: cat /var/log/Xorg.0.log | grep EE [75.403] (EE) Failed ...
- 基于WPF系统框架设计(2)-Fluent Ribbon之HelloWorld
Fluent/Ribbon是微软在其最新桌面操作系统Windows 7中使用的图形用户界面. Windows平台的进化,伴随着系统图形界面的重新设计.从Windows XP到Windows Vista ...
- Win7删除虚拟机新建的用户怎么恢复
11月3日 VMware Workstation 7 Installed in Windows 7 AutoLogon Problem 安装VMware Workstation 7 在Windows ...
- include file与jsp:include 的区别
<%include file="a.jsp"%>静态包含,先加入再编译,就是在编译的时候将a.jsp的代码加入进来在编译,只会生成一个servlet文件,而且不同a ...
- Linux——Virtualenv和pip小探
转载自:http://mengzhuo.org/blog/virtualenv%E5%92%8Cpip%E5%B0%8F%E6%8E%A2.html 本文献给那些看着参差不齐的中文文档/教程,但还在坚 ...
- PDF如何自动滚动阅读
PDF如何自动滚动阅读 视图---页面显示---自动滚动 快捷键 Ctrl +shift+H
- LeetCode题目:Permutations
题目:Given a collection of distinct numbers, return all possible permutations. 大意:全排列给定数组,其中给定数组中没有相同的 ...