注意!先看左上角声明!我不想误人子弟!但我不怕思考。没有思考就没有错误,互相学习,共同进步!

百度中的“专业人士”回答中出现了这句话(不知道是不是专业人士啊 百度说的)“1、是指托管代码,托管代码(Managed Code)实际上就是中间语言(IL)代码。”

如果这句话是对的,应该是对的,那么托管就好理解了,(自己搜中间语言去),我们编写的语言要通过中间语言来翻译即所谓的托管给IL

那么非托管就是不用IL即不用中间语言翻译,别人(微软把)已经把这个语句翻译好了,说白了就是封装好的,你直接去用就行,废话少说直接上列子

using System;
using System.Runtime.InteropServices; class Example
{
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type); static void Main()
{
// Call the MessageBox function using platform invoke.
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}

这个是MSDN上的使用 DllImportAttribute 特性导入 Win32 MessageBox 函数,MessageBox这个窗体函数是已经封装好的。我们在前面先声明一下,引用的几个参数,在主函数中就直接引用了。再来个实战的列子

namespace CPower_CSharp
{
public class CP5200
{
private const string m_strPath = "CP5200.dll"; [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern string CP5200_RS232_GetFileName(); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_SplitScreen(int nCardID, int nScrWidth, int nScrHeight, int nWndCnt, int[] pWndRects); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_SplitScreen(int nCardID, int nScrWidth, int nScrHeight, int nWndCnt, int[] pWndRects); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_InitEx(IntPtr fName, int nBaudrate, int dwTimeout); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_Init(uint dwIP, int nIPPort, uint dwIDCode, int nTimeOut); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_Open(); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_SendText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_SendText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_SendTagText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_SendTagText(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nSpeed, int nEffect, int nStayTime, int nAlignment); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_SendPicture(int nCardID, int nWndNo, int nPosX, int nPosY, int nCx, int nCy, IntPtr pPictureFile, int nSpeed, int nEffect, int nStayTime, int nPictRef); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_SendPicture(int nCardID, int nWndNo, int nPosX, int nPosY, int nCx, int nCy, IntPtr pPictureFile, int nSpeed, int nEffect, int nStayTime, int nPictRef); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_SendStatic(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nAlignment, int x, int y, int cx, int cy); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_SendStatic(int nCardID, int nWndNo, IntPtr pText, int crColor, int nFontSize, int nAlignment, int x, int y, int cx, int cy); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_SendClock(int nCardID, int nWinNo, int nStayTime, int nCalendar, int nFormat, int nContent, int nFont, int nRed, int nGreen, int nBlue, IntPtr pTxt); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_SendClock(int nCardID, int nWinNo, int nStayTime, int nCalendar, int nFormat, int nContent, int nFont, int nRed, int nGreen, int nBlue, IntPtr pTxt); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_SetTime(byte nCardID, byte[] pInfo); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_SetTime(byte nCardID, byte[] pInfo); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_RS232_PlaySelectedPrg(int nCardID, int[] pSelected, int nSelCnt, int nOption); [DllImport(m_strPath, CharSet = CharSet.Auto)]
public static extern int CP5200_Net_PlaySelectedPrg(int nCardID, int[] pSelected, int nSelCnt, int nOption);
}
}
 private int InitComm()
{
int nRet = ;
string strPort;
if ( == m_nCommType)
{
strPort = "COM" + m_nPort.ToString();
nRet = CP5200.CP5200_RS232_InitEx(Marshal.StringToHGlobalAnsi(strPort), m_nBaudrate, m_nTimeout);
}
else
{
m_dwIPAddr = GetIP(IPAddr.Text);
if ( != m_dwIPAddr)
{
m_dwIDCode = GetIP(IDCode.Text);
if ( != m_dwIDCode)
{
CP5200.CP5200_Net_Init(m_dwIPAddr, m_nIPPort, m_dwIDCode, m_nTimeout);
nRet = ;
}
} } return nRet;
}

红色的代码就直接引用了额,由于是静态的方法不用实例了OK

C#中的托管和非托管的更多相关文章

  1. C# using 三种使用方式 C#中托管与非托管 C#托管资源和非托管资源区别

    1.using指令.using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到. ...

  2. C#中托管与非托管

    在.net 编程环境中,系统的资源分为托管资源和非托管资源. 对于托管的资源的回收工作,是不需要人工干预回收的,而且你也无法干预他们的回收,所能够做的 只是了解.net CLR如何做这些操作.也就是说 ...

  3. 浅谈 .NET 中的对象引用、非托管指针和托管指针 理解C#中的闭包

    浅谈 .NET 中的对象引用.非托管指针和托管指针   目录 前言 一.对象引用 二.值传递和引用传递 三.初识托管指针和非托管指针 四.非托管指针 1.非托管指针不能指向对象引用 2.类成员指针 五 ...

  4. C#中的托管与非托管

    在.net 编程环境中,系统的资源分为托管资源和非托管资源. 字面理解托管,就是托付个别人管理,要的是结果,具体怎么完成的我并不关心,就像某些'牛逼'的老板“我只要结果”那样. 在.NET FRAME ...

  5. 浅谈 .NET 中的对象引用、非托管指针和托管指针

    目录 前言 一.对象引用 二.值传递和引用传递 三.初识托管指针和非托管指针 四.非托管指针 1.非托管指针不能指向对象引用 2.类成员指针 五.托管指针 前言 本文主要是以 C# 为例介绍 .NET ...

  6. C# 托管和非托管混合编程

    在非托管模块中实现你比较重要的算法,然后通过 CLR 的平台互操作,来使托管代码调用它,这样程序仍然能够正常工作,但对非托管的本地代码进行反编译,就很困难.   最直接的实现托管与非托管编程的方法就是 ...

  7. [.net 面向对象程序设计进阶] (8) 托管与非托管

    本节导读:虽然在.NET编程过程中,绝大多数内存垃圾回收由CLR(公共语言运行时)自动回收,但也有很多需要我们编码回收.掌握托管与非托管的基本知识,可以有效避免某些情况下导致的程序异常. 1.什么是托 ...

  8. 利用C#Marshal类实现托管和非托管的相互转换

    Marshal 类 命名空间:System.Runtime.InteropServices 提供了一个方法集,这些方法用于分配非托管内存.复制非托管内存块.将托管类型转换为非托管类型,此外还提供了在与 ...

  9. [转]C# 之DLL调用(托管与非托管)

    每种编程语言调用DLL的方法都不尽相同,在此只对用C#调用DLL的方法进行介绍.首先,您需要了解什么是托管,什么是非托管.一般可以认为:非托管代码主要是基于win 32平台开发的DLL,activeX ...

  10. C#的托管与非托管大难点

    托管代码与非托管代码 众所周知,我们正常编程所用的高级语言,是无法被计算机识别的.需要先将高级语言翻译为机器语言,才能被机器理解和运行.在标准C/C++中,编译过程是这样的:源代码首先经过预处理器,对 ...

随机推荐

  1. win7 加域开机自动登录域用户

    解决办法:1.本地管理员帐户登录到本机.点击左下角的“开始”,在运行中输入“regedit”,点击确定 2.弹出“注册表编辑器”,找到下面的路径:[HKEY_LOCAL_MACHINE\SOFTWAR ...

  2. javaScript函数与闭包

    js中函数也是对象,具有一切对象的特征,可以作为表达式给变量赋值,可以作为函数的形参,或者函数的返回值,函数内可以嵌套函数等等,函数内以声明方式定义的函数是局部函数,用表达式声明的函数则由赋值变量的性 ...

  3. SQL Server中的uniqueidentifier类型

    uniqueidentifier类型可以配合T-SQL中的newid和newsequentialid来生成唯一标识符,具体区别如下(摘抄自微软官方文档). Nonsequential GUIDs: Y ...

  4. iOS 自定义UIButton(图片和文字混合)

    // UIApplicationDelegate  .h文件 #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder &l ...

  5. MVC 登录认证与授权及读取登录错误码

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    最近在自学MVC,遇到的问题很多,索性一点点总结下 ...

  6. Java基础之处理事件——使用适配器类(Sketcher 3 using an Adapter class)

    控制台程序. 适配器类是指实现了监听器接口的类,但监听器接口中的方法没有内容,所以它们什么也不做.背后的思想是:允许从提供的适配器类派生自己的监听器类,之后再实现那些自己感兴趣的类.其他的空方法会从适 ...

  7. Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象

    本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...

  8. IntelliJ IDEA 显示行号方法

    设置方法如下:   File->Settings->Editor->General->Appearence->Show Line Number  

  9. php获取文件后缀名格式

    function get_extension($file) { substr(strrchr($file, '.'), 1); } 第2种方法: function get_extension($fil ...

  10. CentOs5.2中PHP的升级

    最近一个项目中需要使用到PHP5.2的版本,而服务器上使用了官方的yum源进行安装,默认的版本是5.1.6,需要升级.但是因为不是一个非常 正式的服务器环境,所以想通过简单的yum update一下了 ...