C#编程之C#中的托管和非托管
小标 2019-01-15 来源 : 阅读 1489 评论 0

摘要:本文主要向大家介绍了C#编程之C#中的托管和非托管,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了C#编程之C#中的托管和非托管,通过具体的内容向大家展示,希望对大家学习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 = 0;
            string strPort;
            if (0 == 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 (0 != m_dwIPAddr)
                {
                    m_dwIDCode = GetIP(IDCode.Text);
                    if (0 != m_dwIDCode)
                    {
                        CP5200.CP5200_Net_Init(m_dwIPAddr, m_nIPPort, m_dwIDCode, m_nTimeout);
                        nRet = 1;
                    }
                }

            }

            return nRet;
        }

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

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程