ylbtech-LanguageSamples-Pinvoke(平台调用)

ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Pinvoke(平台调用)

 

1.A,示例(Sample) 返回顶部

“平台调用”示例

本示例演示了如何从 C# 中调用平台调用(导出的 DLL 函数)。有关更多信息,请参见互操作性(C# 编程指南) 。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“平台调用”示例

  1. 在“解决方案资源管理器”中,右击“PinvokeTest”项目并单击“设为启动项目”。

  2. 在“调试”菜单上,单击“开始执行(不调试)”。

  3. 对于 Marshal 和 Pinvoke,分别重复上述步骤。

从命令行生成并运行“平台调用”示例

  1. 使用“更改目录”命令转到“PinvokeTest”目录。

  2. 键入以下命令:

    csc PinvokeTest.cs
    
    PinvokeTest
  3. 使用“更改目录”命令转到“Marshal”目录。

  4. 键入以下命令:

    csc Marshal.cs
    
    Marshal
  5. 使用“更改目录”命令转到“Pinvoke”目录。

  6. 键入以下命令:

    csc logfont.cs pinvoke.cs
    
    pinvoke
1.B,Marshal 示例代码(Sample Code)返回顶部

1.B.1, Marshal.cs

ylbtech-LanguageSamples-Pinvoke(平台调用)
// 版权所有(C) Microsoft Corporation。保留所有权利。

// 此代码的发布遵从

// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。

//

//版权所有(C) Microsoft Corporation。保留所有权利。



// Marshal.cs

using System;

using System.Runtime.InteropServices;



class PlatformInvokeTest

{

    [DllImport("msvcrt.dll")]

    public static extern int puts(

        [MarshalAs(UnmanagedType.LPStr)]

        string m);

    [DllImport("msvcrt.dll")]

    internal static extern int _flushall();





    public static void Main() 

    {

        puts("Hello World!");

        _flushall();

    }

}
View Code

1.B.2,

1.B,Pinvoke 示例代码2(Sample Code)返回顶部

1.B.1, logfont.cs

ylbtech-LanguageSamples-Pinvoke(平台调用)
// 版权所有(C) Microsoft Corporation。保留所有权利。

// 此代码的发布遵从

// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。

//

//版权所有(C) Microsoft Corporation。保留所有权利。



// logfont.cs

// 编译时使用:/target:module

using System;

using System.Runtime.InteropServices;



[StructLayout(LayoutKind.Sequential)]

public class LOGFONT 

{ 

    public const int LF_FACESIZE = 32;

    public int lfHeight; 

    public int lfWidth; 

    public int lfEscapement; 

    public int lfOrientation; 

    public int lfWeight; 

    public byte lfItalic; 

    public byte lfUnderline; 

    public byte lfStrikeOut; 

    public byte lfCharSet; 

    public byte lfOutPrecision; 

    public byte lfClipPrecision; 

    public byte lfQuality; 

    public byte lfPitchAndFamily;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=LF_FACESIZE)]

    public string lfFaceName; 

}
View Code

1.B.2, pinvoke.cs

ylbtech-LanguageSamples-Pinvoke(平台调用)
// 版权所有(C) Microsoft Corporation。保留所有权利。

// 此代码的发布遵从

// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。

//

//版权所有(C) Microsoft Corporation。保留所有权利。



// pinvoke.cs

// 编译时使用:/addmodule:logfont.netmodule

// csc pinvoke.cs /addmodule:logfont.netmodule

using System;

using System.Runtime.InteropServices;



class PlatformInvokeTest

{   

    [DllImport("gdi32.dll", CharSet=CharSet.Auto)]

    public static extern IntPtr CreateFontIndirect(

        [In, MarshalAs(UnmanagedType.LPStruct)]

        LOGFONT lplf   // 特征

        );



    [DllImport("gdi32.dll")]

    public static extern bool DeleteObject(

        IntPtr handle

        );



    public static void Main() 

    {

        LOGFONT lf = new LOGFONT();

        lf.lfHeight = 9;

        lf.lfFaceName = "Arial";

        IntPtr handle = CreateFontIndirect(lf);



        if (IntPtr.Zero == handle)

        {

            Console.WriteLine("Can't creates a logical font.");

        }

        else

        {

            

            if (IntPtr.Size == 4)

                Console.WriteLine("{0:X}", handle.ToInt32());

            else

                Console.WriteLine("{0:X}", handle.ToInt64());        



            // 删除所创建的逻辑字体。

            if (!DeleteObject(handle))

                Console.WriteLine("Can't delete the logical font");



        }

        

    }

}
View Code

1.B.3,

1.B,PinvokeTest 示例代码3(Sample Code)返回顶部

1.B.1, PinvokeTest.cs

ylbtech-LanguageSamples-Pinvoke(平台调用)
// 版权所有(C) Microsoft Corporation。保留所有权利。

// 此代码的发布遵从

// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。

//

//版权所有(C) Microsoft Corporation。保留所有权利。



// PInvokeTest.cs

using System;

using System.Runtime.InteropServices;



class PlatformInvokeTest

{

    [DllImport("msvcrt.dll")]

    public static extern int puts(string c);

    [DllImport("msvcrt.dll")]

    internal static extern int _flushall();



    public static void Main() 

    {

        puts("Test");

        _flushall();

    }

}
View Code

1.B.2,

1.C,下载地址(Free Download)返回顶部

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

你可能感兴趣的:(language)