C#使用VB6 API

0 c# vb6 api invoke

你在跟我开玩笑!在发布此问题后几秒钟,我决定以不同的方式搜索,并以某种方式遇到以下内容:[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); 欢迎大家编写代码 - 对于由此造成的任何不便,我深感抱歉.

我目前遇到了最奇怪的问题 - 我发现这个问题没有得到很好的记录.我试图通过C#.net使用各种AP​​I,如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方面所做的工作,该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);
Run Code Online (Sandbox Code Playgroud)

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

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

也许有人看到了我不知道的东西.感谢大家!

spe*_*der 5

您可以更容易地学习在.Net 中创建文件惯用方法,而不是执行一些旧代码的逐行翻译.它引领着你一条惊人的迂回路线.