繁体   English   中英

C# 使用 VB6 API

[英]C# Using VB6 API

你一定是在开玩笑,在发布这个问题后几秒钟,我决定以不同的方式搜索:不知何故遇到了以下问题。 [DllImport("kernel32,dll", SetLastError = true. CharSet = CharSet.Unicode)] private static extern Microsoft.Win32.SafeHandles,SafeFileHandle CreateFile(string lpFileName.System,UInt32 dwDesiredAccess.System,UInt32 dwShareMode,IntPtr pSecurityAttributes。 UInt32 dwCreationDisposition.System,UInt32 dwFlagsAndAttributes;IntPtr hTemplateFile)。 祝大家编码愉快 - 对于可能造成的任何不便,我深表歉意。

我目前遇到了最奇怪的问题 - 我发现没有很好的文档记录。 我正在尝试通过 C#.net 使用各种 API,例如 CreateFile、ReadFile 等。 在使用 MessageBox API 时,我确实取得了一些成功,但在尝试对 CreateFile 执行相同操作时出现错误。 我将在下面详细解释。

Step 1: Declarations
  a) MessageBox Declaration

    1) VB6:`// Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long`
    2) C#: `public static extern int MessageBox(int hwnd, string lptxt, string lcap, int wType);`

Step 2: C# Usage: `MessageBox(0, "Text", "Caption", 0);

现在,我将向您展示我对 CreateFile API 所做的工作,该文件目前无法正常工作。

第 1 步:声明 a) CreateFile 声明:

1) VB6:`// Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long

2) C#: [DllImport("kernel32")]
    public static extern long CreateFile(string lpFileName, long dwDesiredAccess, long dwShareMode, long lpSecurityAttributes, long dwCreationDisposition, long dwFlagsAndAttributes, long hTemplateFile);

第二步:C# 用法:long lFile; lFile = CreateFile(strIcoPath, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);

我得到的错误:“检测到 PInvokeStackImbalance 消息:对 PInvoke function 'ScanTime Crypter:Public:.CreateFile' 的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查PInvoke 签名的调用约定和参数与目标非托管签名匹配。”

也许有人看到了我没有看到的东西。 感谢大家!

学习在.Net中创建文件的惯用方式比逐行翻译一些旧代码要容易得多。 它带你走上一条令人惊讶的迂回路线。

创建文件 C# 解决方案:

using System.IO;
// ...

File.Create(@"myfilename.ext");

MessageBox C# 解决方案:

using System.Windows.Forms;
// ...
MessageBox.Show("Message box message.", "My caption.");
// and other various overloads

鳍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM