C#中的托管和非托管
注意!先看左上角声明!我不想误人子弟!但我不怕思考。没有思考就没有错误,互相学习,共同进步!
百度中的“专业人士”回答中出现了这句话(不知道是不是专业人士啊 百度说的)“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#中的托管和非托管的更多相关文章
- C# using 三种使用方式 C#中托管与非托管 C#托管资源和非托管资源区别
1.using指令.using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到. ...
- C#中托管与非托管
在.net 编程环境中,系统的资源分为托管资源和非托管资源. 对于托管的资源的回收工作,是不需要人工干预回收的,而且你也无法干预他们的回收,所能够做的 只是了解.net CLR如何做这些操作.也就是说 ...
- 浅谈 .NET 中的对象引用、非托管指针和托管指针 理解C#中的闭包
浅谈 .NET 中的对象引用.非托管指针和托管指针 目录 前言 一.对象引用 二.值传递和引用传递 三.初识托管指针和非托管指针 四.非托管指针 1.非托管指针不能指向对象引用 2.类成员指针 五 ...
- C#中的托管与非托管
在.net 编程环境中,系统的资源分为托管资源和非托管资源. 字面理解托管,就是托付个别人管理,要的是结果,具体怎么完成的我并不关心,就像某些'牛逼'的老板“我只要结果”那样. 在.NET FRAME ...
- 浅谈 .NET 中的对象引用、非托管指针和托管指针
目录 前言 一.对象引用 二.值传递和引用传递 三.初识托管指针和非托管指针 四.非托管指针 1.非托管指针不能指向对象引用 2.类成员指针 五.托管指针 前言 本文主要是以 C# 为例介绍 .NET ...
- C# 托管和非托管混合编程
在非托管模块中实现你比较重要的算法,然后通过 CLR 的平台互操作,来使托管代码调用它,这样程序仍然能够正常工作,但对非托管的本地代码进行反编译,就很困难. 最直接的实现托管与非托管编程的方法就是 ...
- [.net 面向对象程序设计进阶] (8) 托管与非托管
本节导读:虽然在.NET编程过程中,绝大多数内存垃圾回收由CLR(公共语言运行时)自动回收,但也有很多需要我们编码回收.掌握托管与非托管的基本知识,可以有效避免某些情况下导致的程序异常. 1.什么是托 ...
- 利用C#Marshal类实现托管和非托管的相互转换
Marshal 类 命名空间:System.Runtime.InteropServices 提供了一个方法集,这些方法用于分配非托管内存.复制非托管内存块.将托管类型转换为非托管类型,此外还提供了在与 ...
- [转]C# 之DLL调用(托管与非托管)
每种编程语言调用DLL的方法都不尽相同,在此只对用C#调用DLL的方法进行介绍.首先,您需要了解什么是托管,什么是非托管.一般可以认为:非托管代码主要是基于win 32平台开发的DLL,activeX ...
- C#的托管与非托管大难点
托管代码与非托管代码 众所周知,我们正常编程所用的高级语言,是无法被计算机识别的.需要先将高级语言翻译为机器语言,才能被机器理解和运行.在标准C/C++中,编译过程是这样的:源代码首先经过预处理器,对 ...
随机推荐
- gcc工具链简述
工具链软件包括BINUTILS.GCC.GLIBC.GDB等. BINUTILS是二进制程序处理工具,包括链接器.汇编器等目标程序处理的工具. GCC(GNU Compiler Collection) ...
- yaffs文件系统
1. 概述yaffs文件系统专为Nandflash设计的日志文件系统,占用page中oob区域.目前有两个版本的yaffs文件系统.nandflash不可靠,存在坏块,存在数据错误,需要软件弥补纠正此 ...
- iOS 可延展视图(点击前显示部分文字,点击后显示全部)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicat ...
- Python之配置文件模块 ConfigParser
写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...
- Swift实战-豆瓣电台(七)显示动画
youku观看地址http://v.youku.com/v_show/id_XNzMxODQzNDIw.html 这是一个很酷的动画效果.特别是数据多的时候 知识点 在单元格(Cell)显示方法中设置 ...
- js兼容
//document.getElementsByClassName(); if (!document.getElementsByClassName) { document.getElementsByC ...
- Lintcode: Wood Cut
Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you ...
- for 穷举、迭代 while循环
1.穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 2.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100 ...
- C#: 数据绑定
数据绑定是分离UI和后端主逻辑程序的一种好的办法.这里总结下TextBox, Label, ComboBox, ListBox, DataGridView的数据绑定 数据绑定都是通过DB来和UI控件的 ...
- C++之路进阶——codevs1036(商务旅行)
1036 商务旅行 题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇 ...