簡體   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