0

我正在尝试使用 Win32 API 编写我自己的小型库以连接到串行端口。不幸的是,我找到的所有示例代码都使用 kernel32.dll(我可以这样做),但 createFile 命令使用前缀 win32Com,我假设它是我需要使用的某个命名空间的一部分。不幸的是,我无法弄清楚,作为一个菜鸟,我不知道如何找出这是我需要包含的参考,还是其他什么。

谁能指出一些解释如何执行此操作的示例代码?或者告诉我哪里出错了?我将包括我的“使用”部分和 DllImport 以及我试图在下面创建文件的位置。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

这里还有一些代码,初始化表单等。

[DllImport("kernel32.dll", SetLastError = true)]
        internal static extern IntPtr CreateFile(String lpFileName,
           UInt32 dwDesiredAccess, UInt32 dwShareMode,
           IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition,
           UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);

但是当我使用以下内容时,我得到了错误:The name 'Win32Com' does not exist in the current context.

 hPort = Win32Com.CreateFile(cs.port,
               Win32Com.GENERIC_READ | Win32Com.GENERIC_WRITE, 0, IntPtr.Zero,
               Win32Com.OPEN_EXISTING, Win32Com.FILE_FLAG_OVERLAPPED,
               IntPtr.Zero);

我确定这是我犯的一个简单错误,但不幸的是,我目前还没有技能来找出它在哪里。

提前致谢!

4

1 回答 1

1

您的链接显示:“Win32Com 是一个帮助类,用作我将通过 P/Invoke 使用的 API 函数、结构和常量的静态定义的容器。” 所以你需要创建它并在CreateFile那里放置以下声明。您还应该阅读P/Invoke以了解其工作原理。

于 2013-02-24T01:06:22.793 回答