简体   繁体   English

C# 使用 VB6 API

[英]C# Using VB6 API

You have to be kidding me, Literally seconds after posting this question I decided to search in a different way: and somehow came across the following.你一定是在开玩笑,在发布这个问题后几秒钟,我决定以不同的方式搜索:不知何故遇到了以下问题。 [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. System,UInt32 dwCreationDisposition. System,UInt32 dwFlagsAndAttributes; IntPtr hTemplateFile). [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)。 Happy coding everyone - I'm sorry for any inconvenience this may have caused.祝大家编码愉快 - 对于可能造成的任何不便,我深表歉意。

I am currently having the oddest issue - one that I have found not to be very well documented.我目前遇到了最奇怪的问题 - 我发现没有很好的文档记录。 I am trying to use various APIs such as CreateFile, ReadFile etc. through C#.net.我正在尝试通过 C#.net 使用各种 API,例如 CreateFile、ReadFile 等。 I DID have some success when using the MessageBox API, but I am getting an error when attempting to do the same with CreateFile.在使用 MessageBox API 时,我确实取得了一些成功,但在尝试对 CreateFile 执行相同操作时出现错误。 I will explain in heavy detail below.我将在下面详细解释。

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);

Now, I will show you what I have done regarding the CreateFile API which is not working as of now.现在,我将向您展示我对 CreateFile API 所做的工作,该文件目前无法正常工作。

Step 1: Declarations a) CreateFile Declaration:第 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);

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

The error that I am getting: "PInvokeStackImbalance was detected Message: A call to PInvoke function 'ScanTime Crypter:Public:.CreateFile' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."我得到的错误:“检测到 PInvokeStackImbalance 消息:对 PInvoke function 'ScanTime Crypter:Public:.CreateFile' 的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查PInvoke 签名的调用约定和参数与目标非托管签名匹配。”

Perhaps someone sees what I do not.也许有人看到了我没有看到的东西。 Thanks everyone!感谢大家!

It would be much much easier for you to learn the idiomatic way of creating a file in.Net rather than performing literal line by line translations of some old code.学习在.Net中创建文件的惯用方式比逐行翻译一些旧代码要容易得多。 It's leading you up a surpisingly roundabout route.它带你走上一条令人惊讶的迂回路线。

Create File C# solution:创建文件 C# 解决方案:

using System.IO;
// ...

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

MessageBox C# solution: MessageBox C# 解决方案:

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

Fin.鳍。

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

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