简体   繁体   中英

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). 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. I DID have some success when using the MessageBox API, but I am getting an error when attempting to do the same with 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.

Step 1: Declarations a) CreateFile Declaration:

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; 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."

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. It's leading you up a surpisingly roundabout route.

Create File C# solution:

using System.IO;
// ...

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

MessageBox C# solution:

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

Fin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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