首页 技术 正文
技术 2022年11月16日
0 收藏 1000 点赞 2,401 浏览 4402 个字

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

百度中的“专业人士”回答中出现了这句话(不知道是不是专业人士啊 百度说的)“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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918