zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    JQuery有几种选择器?
    Java有哪些基本数据类型
    您认为做好测试用例设计工作的关键是什么?
    final关键字
    目前主要的测试用例设计方法是什么?
    举例说明同步和异步。
    maven配置多个镜像
    参数Parameters、变量Variables
    jsp文件导包
    动态横切
  • 原文地址:https://www.cnblogs.com/mamiyiya777/p/5759578.html
Copyright © 2011-2022 走看看