_lwrite





VB.Net


Imports System.Runtime.InteropServices

Module _LWrite

    Const GENERIC_READ = &H80000000
    Const GENERIC_WRITE = &H40000000
    Const GENERIC_EXECUTE = &H20000000
    Const GENERIC_ALL = &H10000000

    Const CREATE_NEW = 1
    Const CREATE_ALWAYS = 2
    Const OPEN_EXISTING = 3
    Const OPEN_ALWAYS = 4
    Const TRUNCATE_EXISTING = 5

    <DllImport("kernel32.dll", EntryPoint:="_lwrite")>
    Public Function _LWrite(ByVal hFile As IntPtr, ByVal lpBuffer As String, ByVal nNumberOfBytesToWrite As Integer) As Integer
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Ansi)>
    Private Function CreateFileA(lpFileName As String, dwDesiredAccess As Integer, dwShareMode As Integer, lpSecurityAttributes As IntPtr, dwCreationDisposition As Integer, dwFlagsAndAttributes As Integer, hTemplateFile As IntPtr) As IntPtr
    End Function

    <DllImport("kernel32", SetLastError:=True)>
    Private Function CloseHandle(hObject As IntPtr) As Boolean
    End Function

    Sub WriteToFile()
        Dim filePath As String = "C:\Kernel32\example.txt"
        Dim buffer As String = "Hello, World!"

        ' Open the file in binary mode
        Dim hFile As IntPtr = CreateFileA(filePath, GENERIC_WRITE, 0, IntPtr.Zero, CREATE_NEW, 0, IntPtr.Zero)

        If hFile <> IntPtr.Zero Then
            ' Write the data to the file
            Dim numBytesWritten As Integer = _LWrite(hFile, buffer, buffer.Length)

            ' Close the file
            CloseHandle(hFile)
        Else
            Stop
            ' Handle the error
        End If
    End Sub
End Module