惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Vulnerabilities – Threatpost
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
G
GRAHAM CLULEY
Cyberwarzone
Cyberwarzone
S
Schneier on Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 聂微东
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
The Hacker News
The Hacker News
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
Scott Helme
Scott Helme
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
L
LangChain Blog
F
Full Disclosure
I
Intezer
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy
美团技术团队
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
罗磊的独立博客
T
Tenable Blog
D
DataBreaches.Net
M
MIT News - Artificial intelligence
S
Securelist
C
CERT Recently Published Vulnerability Notes
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
NISL@THU
NISL@THU
The Register - Security
The Register - Security
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog

博客园 - Nillson

传说中的Singleton.... 设计模式--简单工厂模式 策略模式 抽象类与接口 C# 实现的一个二叉树类 回顾一个面试题 再谈代理 常见的排序方法 预定义,宏定义 连接符,数值运算与函数 复杂查询 数据库中的Index和View的理解 重载和重写 采用递归的方法获得一棵树的所有叶节点 .NET中的新概念整理 4月要看的书 挂个牛人 一篇关于如何写注释的文章,值得收藏 Vistual Studio 2005到Vistual Studio 2008的版本转换问题 Visual Studio 2008 的一个Bug
System.Runtime.InteropServices浅见
Nillson · 2008-03-31 · via 博客园 - Nillson

System.Runtime.InteropServices提供了相应的类或者方法来支持托管/非托管模块间的互相调用。
System.Runtime.InteropServices中几个比较重要的类:
DllImportAttribute : 该类提供对非托管动态链接库进行引用的方法,并告诉我们的编译器该程序的静态入口点是非托管的动态连接库,它的静态属性提供了对非托管动态链接库进行调用所必需的信息,作为最基本的要求,该类应该定义提供调用的非托管动态链接库的名称。成员详细信息
StructLayoutAttribute: 该类使得用户可以控制类或结构的数据字段的物理布局。

[StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
public class MySystemTime 
{
   [FieldOffset(
0)]public ushort wYear; 
   [FieldOffset(
2)]public ushort wMonth;
   [FieldOffset(
4)]public ushort wDayOfWeek; 
   [FieldOffset(
6)]public ushort wDay; 
   [FieldOffset(
8)]public ushort wHour; 
   [FieldOffset(
10)]public ushort wMinute; 
   [FieldOffset(
12)]public ushort wSecond; 
   [FieldOffset(
14)]public ushor wMilliseconds; 
}

MarshalAsAttribute : 指示如何在托管代码和非托管代码之间封送数据。下面是MSDN给出的示例代码:

[C#] 
//Applied to a parameter.
  public void M1 ([MarshalAs(UnmanagedType.LPWStr)]String msg);
//Applied to a field within a class.
  class MsgText {
    [MarshalAs(UnmanagedType.LPWStr)] Public String msg;
  }

//Applied to a return value.
[return: MarshalAs(UnmanagedType.LPWStr)]
public String GetMessage();

一个将三个类综合运用的实例:调用kernel32.dll中的非托管方法"GetSystemTime"将系统时间返回给定制的类MySystemTime并执行输出.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace DllImportTest
{
    
/// <summary>
    
/// 定义一个用于接收非托管API方法返回值的类
    
/// StructLayout定义了该类的各个成员在物理上的排列方式
    
/// </summary>

    [StructLayout(LayoutKind.Explicit, Size = 16, CharSet = CharSet.Ansi)]
    
public class MySystemTime
    
{
        [FieldOffset(
0)]
        
public ushort wYear;
        [FieldOffset(
2)]
        
public ushort wMonth;
        [FieldOffset(
4)]
        
public ushort wDayOfWeek;
        [FieldOffset(
6)]
        
public ushort wDay;
        [FieldOffset(
8)]
        
public ushort wHour;
        [FieldOffset(
10)]
        
public ushort wMinute;
        [FieldOffset(
12)]
        
public ushort wSecond;
        [FieldOffset(
14)]
        
public ushort wMilliseconds;
    }

    
/// <summary>
    
/// 用LibWrapper的静态方法来调用非托管API方法"GetSystemTime"
    
/// </summary>

    class LibWrapper
    
{
        [DllImport(
"kernel32.dll", EntryPoint = "GetSystemTime")]
        
//如果定义的方法名称与要进行封装的非托管API方法不同则需要在DLLImport中指定入口点.
        public static extern void gettime([MarshalAs(UnmanagedType.LPStruct)]MySystemTime st);
    }


    
class TestApplication
    
{
        
public static void Main()
        
{
            
try
            
{
                MySystemTime sysTime 
= new MySystemTime();
                
//LibWrapper.GetSystemTime(sysTime);
                LibWrapper.gettime(sysTime);
                Console.WriteLine(
"The System time is {0}/{1}/{2} {3}:{4}:{5}", sysTime.wDay,
                   sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
            }

            
catch (TypeLoadException e)
            
{
                Console.WriteLine(
"TypeLoadException : " + e.Message);
            }

            
catch (Exception e)
            
{
                Console.WriteLine(
"Exception : " + e.Message);
            }

        }

    }


}