elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


  Mostrar Mensajes
Páginas: 1 2 3 [4] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... 1257
31  Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets) en: 21 Septiembre 2025, 12:28 pm
Clase PortableExecutableUtil (1ª PARTE):

Nota: Para que me cupiera el código en este post, he tenido que eliminar TODA la documentación XML en torno a las excepciones de cada método, además de los códigos de ejemplo que había embedidos en la documentación (de todas formas en el siguiente post muestro ejemplos de uso). Disculpas. 🙏

Código
  1. ''' <summary>
  2. ''' Utility class for working with Portable Executable (PE) files.
  3. ''' </summary>
  4. Partial Public Class PortableExecutableUtil
  5.  
  6.    Private Sub New()
  7.    End Sub
  8.  
  9.    ''' <summary>
  10.    ''' Appends an arbitrary data blob to the Certificate Table data-directory entry
  11.    ''' in the Portable Executable (PE) header of the given file.
  12.    ''' </summary>
  13.    '''
  14.    ''' <param name="inputFilePath">
  15.    ''' Path to the input —digitally signed— Portable Executable (PE) file (e.g., "C:\Windows\explorer.exe").
  16.    ''' </param>
  17.    '''
  18.    ''' <param name="outputFilePath">
  19.    ''' Path to the output file that will be written with the modified Certificate Table.
  20.    ''' <para></para>
  21.    ''' Cannot be the same as <paramref name="inputFilePath"/>.
  22.    ''' </param>
  23.    '''
  24.    ''' <param name="blob">
  25.    ''' A <see cref="Byte()"/> array containing the arbitrary data blob to append into the certificate table.
  26.    ''' </param>
  27.    '''
  28.    ''' <param name="markerBegin">
  29.    ''' Optional. A byte sequence used to mark the beginning of the data blob within the Certificate Table content.
  30.    ''' <para></para>
  31.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_BEGIN#</c>" in UTF-8 encoding bytes.
  32.    ''' <para></para>
  33.    ''' It is strongly recommended to use a unique and long enough byte pattern
  34.    ''' to avoid accidental conflicts when identifying/extracting the appended blob.
  35.    ''' </param>
  36.    '''
  37.    ''' <param name="markerEnd">
  38.    ''' Optional. A byte sequence used to mark the end of the data blob within the Certificate Table content.
  39.    ''' <para></para>
  40.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_END#</c>" in UTF-8 encoding bytes.
  41.    ''' <para></para>
  42.    ''' It is strongly recommended to use a unique and long enough byte pattern
  43.    ''' to avoid accidental conflicts when identifying/extracting the appended blob.
  44.    ''' </param>
  45.    '''
  46.    ''' <param name="throwIfInvalidCertSize">
  47.    ''' Optional. Determines whether to allow appending data that will cause to exceed the maximum allowed certificate table size (~100 MB).
  48.    ''' <para></para>
  49.    ''' If set to <see langword="True"/>, the method will throw an <see cref="InvalidOperationException"/>
  50.    ''' if the appended data would cause the certificate table size to exceed the maximum allowed limit,
  51.    ''' preventing digital signature invalidation.
  52.    ''' <para></para>
  53.    ''' If set to <see langword="False"/>, the certificate table size limit can be exceeded (up to ~2 GB) when appending data,
  54.    ''' but the digital signature will become invalid, as the operating system will
  55.    ''' not recognize a certificate table greater than the maximum allowed size.
  56.    ''' Use it at your own risk.
  57.    ''' <para></para>
  58.    ''' Default value is <see langword="True"/>.
  59.    ''' </param>
  60.    '''
  61.    ''' <param name="overwriteOutputFile">
  62.    ''' If <see langword="False"/> and the output file already exists, the method throws an <see cref="IOException"/>.
  63.    ''' <para></para>
  64.    ''' If <see langword="True"/>, any existing output file will be overwritten.
  65.    ''' <para></para>
  66.    ''' Default value is <see langword="False"/>.
  67.    ''' </param>
  68.    <DebuggerStepThrough>
  69.    Public Shared Sub AppendBlobToPECertificateTable(inputFilePath As String,
  70.                                                     outputFilePath As String,
  71.                                                     blob As Byte(),
  72.                                                     Optional markerBegin As Byte() = Nothing,
  73.                                                     Optional markerEnd As Byte() = Nothing,
  74.                                                     Optional throwIfInvalidCertSize As Boolean = True,
  75.                                                     Optional overwriteOutputFile As Boolean = False)
  76.  
  77.        ValidateCommonParameters((NameOf(blob), blob))
  78.  
  79.        Using ms As New MemoryStream(blob)
  80.            AppendBlobToPECertificateTable(inputFilePath, outputFilePath,
  81.                                           ms, markerBegin, markerEnd,
  82.                                           throwIfInvalidCertSize, overwriteOutputFile)
  83.        End Using
  84.    End Sub
  85.  
  86.    ''' <summary>
  87.    ''' Appends an arbitrary data blob to the Certificate Table data-directory entry
  88.    ''' in the Portable Executable (PE) header of the given file.
  89.    ''' </summary>
  90.    '''
  91.    ''' <param name="inputFilePath">
  92.    ''' Path to the input —digitally signed— Portable Executable (PE) file (e.g., "C:\Windows\explorer.exe").
  93.    ''' </param>
  94.    '''
  95.    ''' <param name="outputFilePath">
  96.    ''' Path to the output file that will be written with the modified Certificate Table.
  97.    ''' <para></para>
  98.    ''' Cannot be the same as <paramref name="inputFilePath"/>.
  99.    ''' </param>
  100.    '''
  101.    ''' <param name="blobStream">
  102.    ''' The <see cref="Stream"/> containing the arbitrary data to append into the certificate table.
  103.    ''' </param>
  104.    '''
  105.    ''' <param name="markerBegin">
  106.    ''' Optional. A byte sequence used to mark the beginning of the data blob within the Certificate Table content.
  107.    ''' <para></para>
  108.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_BEGIN#</c>" in UTF-8 encoding bytes.
  109.    ''' <para></para>
  110.    ''' It is strongly recommended to use a unique and long enough byte pattern
  111.    ''' to avoid accidental conflicts when identifying/extracting the appended blob.
  112.    ''' </param>
  113.    '''
  114.    ''' <param name="markerEnd">
  115.    ''' Optional. A byte sequence used to mark the end of the data blob within the Certificate Table content.
  116.    ''' <para></para>
  117.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_END#</c>" in UTF-8 encoding bytes.
  118.    ''' <para></para>
  119.    ''' It is strongly recommended to use a unique and long enough byte pattern
  120.    ''' to avoid accidental conflicts when identifying/extracting the appended blob.
  121.    ''' </param>
  122.    '''
  123.    ''' <param name="throwIfInvalidCertSize">
  124.    ''' Optional. Determines whether to allow appending data that will cause to exceed the maximum allowed certificate table size (~100 MB).
  125.    ''' <para></para>
  126.    ''' If set to <see langword="True"/>, the method will throw an <see cref="InvalidOperationException"/>
  127.    ''' if the appended data would cause the certificate table size to exceed the maximum allowed limit,
  128.    ''' preventing digital signature invalidation.
  129.    ''' <para></para>
  130.    ''' If set to <see langword="False"/>, the certificate table size limit can be exceeded (up to ~2 GB) when appending data,
  131.    ''' but the digital signature will become invalid, as the operating system will
  132.    ''' not recognize a certificate table greater than the maximum allowed size.
  133.    ''' Use it at your own risk.
  134.    ''' <para></para>
  135.    ''' Default value is <see langword="True"/>.
  136.    ''' </param>
  137.    '''
  138.    ''' <param name="overwriteOutputFile">
  139.    ''' If <see langword="False"/> and the output file already exists, the method throws an <see cref="IOException"/>.
  140.    ''' <para></para>
  141.    ''' If <see langword="True"/>, any existing output file will be overwritten.
  142.    ''' <para></para>
  143.    ''' Default value is <see langword="False"/>.
  144.    ''' </param>
  145.    <DebuggerStepThrough>
  146.    Public Shared Sub AppendBlobToPECertificateTable(inputFilePath As String,
  147.                                                     outputFilePath As String,
  148.                                                     blobStream As Stream,
  149.                                                     Optional markerBegin As Byte() = Nothing,
  150.                                                     Optional markerEnd As Byte() = Nothing,
  151.                                                     Optional throwIfInvalidCertSize As Boolean = True,
  152.                                                     Optional overwriteOutputFile As Boolean = False)
  153.  
  154.        ValidateCommonParameters((NameOf(inputFilePath), inputFilePath),
  155.                                 (NameOf(outputFilePath), outputFilePath),
  156.                                 (NameOf(blobStream), blobStream),
  157.                                 (NameOf(markerBegin), markerBegin),
  158.                                 (NameOf(markerEnd), markerEnd),
  159.                                 (NameOf(overwriteOutputFile), overwriteOutputFile))
  160.  
  161.        ' PE header alignment (it is aligned on 8-byte boundary).
  162.        ' https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#overview
  163.        Const PeHeaderAlignment As Short = 8
  164.  
  165.        ' Maximum Certificate Table size, in bytes, not counting the alignment (PeHeaderAlignment) bytes.
  166.        ' If a Certificate Table exceeds this size (MaxCertTableSize + PeHeaderAlignment),
  167.        ' the operating system rejects to parse the certificate.
  168.        ' Note: This limit is somewhat arbitrary, derived from testing on Windows 10.
  169.        Const MaxCertTableSize As Integer = 102400000
  170.        ' Kibibytes (KiB): 100000
  171.        ' Kilobytes  (KB): 102400
  172.        ' Mebibytes (MiB): 97.65625
  173.        ' Megabytes  (MB): 102.40
  174.  
  175.        Dim metaStructSize As Integer = Marshal.SizeOf(GetType(CertBlobMeta))
  176.  
  177.        Dim dataWithMarkersSize As Long = markerBegin.Length + metaStructSize + blobStream.Length + markerEnd.Length
  178.  
  179.        If throwIfInvalidCertSize AndAlso (dataWithMarkersSize > MaxCertTableSize) Then
  180.            Dim msg As String =
  181.                $"The size of the data to append ({NameOf(markerBegin)} + {NameOf(blobStream)} + {NameOf(markerEnd)} = {dataWithMarkersSize} bytes) " &
  182.                $"exceeds the maximum allowed certificate table size ({MaxCertTableSize} bytes), which would invalidate the digital signature."
  183.  
  184.            Throw New InvalidOperationException(msg)
  185.        End If
  186.  
  187.        Dim inputFileInfo As New FileInfo(inputFilePath)
  188.        Dim inputFileLength As Long = inputFileInfo.Length
  189.        If inputFileLength > Integer.MaxValue Then
  190.            Dim msg As String = $"The input file '{inputFilePath}' is too large ({inputFileLength} bytes). " &
  191.                                $"Maximum supported file size is around {Integer.MaxValue} bytes."
  192.            Throw New IOException(msg)
  193.        End If
  194.  
  195.        Using fsInput As New FileStream(inputFileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192 * 2, FileOptions.None),
  196.              peReader As New PEReader(fsInput, PEStreamOptions.Default)
  197.  
  198.            Dim headers As PEHeaders = Nothing
  199.            Dim certDirRVA As Integer, certDirSize As Integer
  200.            ValidatePEHeaderAndCertDir(peReader, headers, certDirRVA, certDirSize)
  201.  
  202.            ' Calculate aligned new certificate table size.
  203.            Dim newCertDirSizeCandidate As Long = certDirSize + dataWithMarkersSize
  204.            Dim newCertDirSizeAligned As Long = CLng(Math.Ceiling(newCertDirSizeCandidate / PeHeaderAlignment)) * PeHeaderAlignment
  205.  
  206.            If (inputFileLength - certDirSize) + newCertDirSizeAligned > Integer.MaxValue Then
  207.                Dim msg As String = $"The required total size to create the output file ({newCertDirSizeAligned} bytes) " &
  208.                                     "exceeds the practical limit for the Portable Executable."
  209.                Throw New InvalidOperationException(msg)
  210.            End If
  211.  
  212.            If throwIfInvalidCertSize AndAlso (newCertDirSizeAligned > MaxCertTableSize + PeHeaderAlignment) Then
  213.                Dim msg As String =
  214.                    $"The size for the new certificate table ({newCertDirSizeAligned} bytes) " &
  215.                    $"exceeds the maximum allowed certificate table size ({MaxCertTableSize} + {PeHeaderAlignment} bytes), " &
  216.                    "which would invalidate the digital signature."
  217.                Throw New InvalidOperationException(msg)
  218.            End If
  219.  
  220.            Dim totalBytesLengthToAdd As Long = newCertDirSizeAligned - certDirSize
  221.            Dim paddingLength As Integer = CInt(totalBytesLengthToAdd - dataWithMarkersSize)
  222.  
  223.            ' Create the blob meta structure.
  224.            Dim meta As New CertBlobMeta With {
  225.                .BlobSize = CInt(blobStream.Length),
  226.                .PaddingLength = paddingLength
  227.            }
  228.            Dim metaBytes As Byte() = MarshalExtensions.ConvertToBytes(meta)
  229.  
  230.            ' Write changes to output file.
  231.            Using fsOutput As New FileStream(outputFilePath, If(overwriteOutputFile, FileMode.Create, FileMode.CreateNew),
  232.                                             FileAccess.Write, FileShare.Read, bufferSize:=8192 * 2, FileOptions.None)
  233.  
  234.                Dim writeBufferSize As Integer = 8192 * 2
  235.                Dim writeBuffer(writeBufferSize - 1) As Byte
  236.  
  237.                ' Write head (0 to certDirRVA-1)
  238.                fsInput.Position = 0
  239.                StreamExtensions.CopyExactTo(fsInput, fsOutput, certDirRVA)
  240.  
  241.                ' Write original certificate table.
  242.                fsInput.Position = certDirRVA
  243.                StreamExtensions.CopyExactTo(fsInput, fsOutput, certDirSize)
  244.  
  245.                ' Append markerBegin + metaBytes + blobStream + markerEnd + padding (if required to align).
  246.                fsOutput.Write(markerBegin, 0, markerBegin.Length)
  247.                fsOutput.Write(metaBytes, 0, metaStructSize)
  248.                StreamExtensions.CopyExactTo(blobStream, fsOutput, CInt(blobStream.Length))
  249.                fsOutput.Write(markerEnd, 0, markerEnd.Length)
  250.                If paddingLength > 0 Then
  251.                    fsOutput.Write(New Byte(paddingLength - 1) {}, 0, paddingLength)
  252.                End If
  253.  
  254.                ' Copy any original remainder bytes (tail).
  255.                Dim tailStart As Integer = certDirRVA + certDirSize
  256.                If tailStart < fsInput.Length Then
  257.                    fsInput.Position = tailStart
  258.                    Dim remainingTail As Integer = CInt(fsInput.Length - tailStart)
  259.                    StreamExtensions.CopyExactTo(fsInput, fsOutput, remainingTail)
  260.                End If
  261.  
  262.                UpdateCertificateTableLengths(fsInput, fsOutput, headers, certDirRVA, CUInt(certDirSize + totalBytesLengthToAdd))
  263.            End Using ' fsOutput
  264.        End Using ' fsInput, peReader
  265.    End Sub
  266.  
  267.    ''' <summary>
  268.    ''' Retrieves all the data blobs —that are enclosed between the specified <paramref name="markerBegin"/> and <paramref name="markerEnd"/> markers—
  269.    ''' from the Certificate Table data-directory entry in the Portable Executable (PE) header of the given file.
  270.    ''' <para></para>
  271.    ''' These blobs must have been previously added with the <see cref="AppendBlobToPECertificateTable"/> function.
  272.    ''' </summary>
  273.    '''
  274.    ''' <param name="filePath">
  275.    ''' Path to the input —digitally signed— Portable Executable (PE) file
  276.    ''' from which to extract data blobs (e.g., "C:\Windows\explorer.exe").
  277.    ''' </param>
  278.    '''
  279.    ''' <param name="markerBegin">
  280.    ''' Optional. A byte sequence used to delimit the beginning of a data blob within the Certificate Table content.
  281.    ''' <para></para>
  282.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_BEGIN#</c>" in UTF-8 encoding bytes.
  283.    ''' <para></para>
  284.    ''' This value must be the same used when calling <see cref="AppendBlobToPECertificateTable"/> function.
  285.    ''' </param>
  286.    '''
  287.    ''' <param name="markerEnd">
  288.    ''' Optional. A byte sequence used to delimit the end of a data blob within the Certificate Table content.
  289.    ''' <para></para>
  290.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_END#</c>" in UTF-8 encoding bytes.
  291.    ''' <para></para>
  292.    ''' This value must be the same used when calling <see cref="AppendBlobToPECertificateTable"/> function.
  293.    ''' </param>
  294.    '''
  295.    ''' <returns>
  296.    ''' An <see cref="ImmutableArray"/> of <see cref="ArraySegment(Of Byte)"/> representing each blob found.
  297.    ''' </returns>
  298.    <DebuggerStepThrough>
  299.    Public Shared Function GetBlobsFromPECertificateTable(filePath As String,
  300.                                                          Optional markerBegin As Byte() = Nothing,
  301.                                                          Optional markerEnd As Byte() = Nothing) As ImmutableArray(Of ArraySegment(Of Byte))
  302.  
  303.        ValidateCommonParameters((NameOf(filePath), filePath),
  304.                                 (NameOf(markerBegin), markerBegin),
  305.                                 (NameOf(markerEnd), markerEnd))
  306.  
  307.        Dim metaStructSize As Integer = Marshal.SizeOf(GetType(CertBlobMeta))
  308.  
  309.        Dim blobs As New Collection(Of ArraySegment(Of Byte))
  310.  
  311.        Using fs As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read,
  312.                                   bufferSize:=8192 * 2, FileOptions.SequentialScan),
  313.              peReader As New PEReader(fs, PEStreamOptions.LeaveOpen)
  314.  
  315.            Dim headers As PEHeaders = Nothing
  316.            Dim certDirRVA As Integer, certDirSize As Integer
  317.            ValidatePEHeaderAndCertDir(peReader, headers, certDirRVA, certDirSize)
  318.  
  319.            ' Read the entire certificate table into memory.
  320.            ' Note: This assumes the system has enough RAM for large tables up to ~2GB.
  321.            fs.Position = certDirRVA
  322.            Dim certBytes As Byte() = StreamExtensions.ReadExact(fs, certDirSize)
  323.  
  324.            Dim searchIndex As Integer
  325.  
  326.            ' Main loop to locate all blob segments enclosed by the markers.
  327.            While searchIndex < certBytes.Length
  328.                ' Locate the start marker.
  329.                Dim idx As Integer = Array.IndexOf(certBytes, markerBegin(0), searchIndex)
  330.                ' Ensure there's room for full marker and meta.
  331.                If (idx = -1) OrElse (idx + markerBegin.Length + metaStructSize) >= certBytes.Length Then
  332.                    Exit While
  333.                End If
  334.  
  335.                ' Verify full start marker match.
  336.                Dim matchStart As Boolean = True
  337.                For j As Integer = 1 To markerBegin.Length - 1
  338.                    If certBytes(idx + j) <> markerBegin(j) Then
  339.                        matchStart = False
  340.                        Exit For
  341.                    End If
  342.                Next
  343.                If Not matchStart Then
  344.                    searchIndex = idx + 1
  345.                    Continue While
  346.                End If
  347.  
  348.                ' Read CertBlobMeta structure bytes.
  349.                Dim metaStart As Integer = idx + markerBegin.Length
  350.                Dim metaBytes(metaStructSize - 1) As Byte
  351.                Array.Copy(certBytes, metaStart, metaBytes, 0, metaStructSize)
  352.                Dim meta As CertBlobMeta = MarshalExtensions.ConvertToStructure(Of CertBlobMeta)(metaBytes)
  353.                Dim blobStart As Integer = metaStart + metaStructSize
  354.                Dim blobSize As Integer = meta.BlobSize
  355.  
  356.                ' Add the actual blob (skip padding).
  357.                blobs.Add(New ArraySegment(Of Byte)(certBytes, blobStart, blobSize))
  358.  
  359.                ' Move search index past the end marker.
  360.                searchIndex = blobStart + blobSize + markerEnd.Length + meta.PaddingLength
  361.            End While
  362.        End Using
  363.  
  364.        Return blobs.ToImmutableArray()
  365.    End Function
  366.  
  367.    ''' <summary>
  368.    ''' Removes a specific blob —that is enclosed between the specified <paramref name="markerBegin"/> and <paramref name="markerEnd"/> markers—
  369.    ''' from the Certificate Table data-directory entry in the Portable Executable (PE) header of the given file.
  370.    ''' <para></para>
  371.    ''' The blob must have been previously added with the <see cref="AppendBlobToPECertificateTable"/> function.
  372.    ''' </summary>
  373.    '''
  374.    ''' <param name="inputFilePath">
  375.    ''' Path to the input —digitally signed— Portable Executable (PE) file (e.g., "C:\Windows\explorer.exe")
  376.    ''' from which the blob will be removed.
  377.    ''' </param>
  378.    '''
  379.    ''' <param name="outputFilePath">
  380.    ''' Path to the output file that will be written with the modified Certificate Table.
  381.    ''' <para></para>
  382.    ''' Cannot be the same as <paramref name="inputFilePath"/>.
  383.    ''' </param>
  384.    '''
  385.    ''' <param name="blobIndex">
  386.    ''' Zero-based index of the blob to remove from the Certificate Table.
  387.    ''' </param>
  388.    '''
  389.    ''' <param name="markerBegin">
  390.    ''' Optional. A byte sequence used to delimit the beginning of a data blob within the Certificate Table content.
  391.    ''' <para></para>
  392.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_BEGIN#</c>" in UTF-8 encoding bytes.
  393.    ''' <para></para>
  394.    ''' This value must be the same used when calling <see cref="AppendBlobToPECertificateTable"/> function.
  395.    ''' </param>
  396.    '''
  397.    ''' <param name="markerEnd">
  398.    ''' Optional. A byte sequence used to delimit the end of a data blob within the Certificate Table content.
  399.    ''' <para></para>
  400.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_END#</c>" in UTF-8 encoding bytes.
  401.    ''' <para></para>
  402.    ''' This value must be the same used when calling <see cref="AppendBlobToPECertificateTable"/> function.
  403.    ''' </param>
  404.    '''
  405.    ''' <param name="overwriteOutputFile">
  406.    ''' If <see langword="False"/> and the output file already exists, the method throws an <see cref="IOException"/>.
  407.    ''' <para></para>
  408.    ''' If <see langword="True"/>, any existing output file will be overwritten.
  409.    ''' <para></para>
  410.    ''' Default value is <see langword="False"/>.
  411.    ''' </param>
  412.    <DebuggerStepThrough>
  413.    Public Shared Sub RemoveBlobFromPECertificateTable(inputFilePath As String,
  414.                                                       outputFilePath As String,
  415.                                                       blobIndex As Integer,
  416.                                                       Optional markerBegin As Byte() = Nothing,
  417.                                                       Optional markerEnd As Byte() = Nothing,
  418.                                                       Optional overwriteOutputFile As Boolean = False)
  419.  
  420.        ' The rest of parameters are validated in the following call to GetBlobsFromPECertificateTable function.
  421.        ValidateCommonParameters((NameOf(outputFilePath), outputFilePath),
  422.                                 (NameOf(blobIndex), blobIndex),
  423.                                 (NameOf(overwriteOutputFile), overwriteOutputFile))
  424.  
  425.        Dim blobs As ImmutableArray(Of ArraySegment(Of Byte)) =
  426.            GetBlobsFromPECertificateTable(inputFilePath, markerBegin, markerEnd)
  427.  
  428.        If blobIndex >= blobs.Length Then
  429.            Dim msg As String = "Blob index was out of range. Must be less than the length of existing blobs."
  430.            Throw New ArgumentOutOfRangeException(NameOf(blobIndex), msg)
  431.        End If
  432.  
  433.        Using fsInput As New FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read,
  434.                                        bufferSize:=8192 * 2, FileOptions.SequentialScan),
  435.              peReader As New PEReader(fsInput, PEStreamOptions.LeaveOpen)
  436.  
  437.            Dim headers As PEHeaders = Nothing
  438.            Dim certDirRVA As Integer, certDirSize As Integer
  439.            ValidatePEHeaderAndCertDir(peReader, headers, certDirRVA, certDirSize)
  440.  
  441.            ' Read CertBlobMeta structure
  442.            Dim metaStructSize As Integer = Marshal.SizeOf(GetType(CertBlobMeta))
  443.            Dim metaStart As Integer = blobs(blobIndex).Offset - metaStructSize - markerBegin.Length
  444.            Dim metaBytes(metaStructSize - 1) As Byte
  445.            fsInput.Position = certDirRVA + metaStart + markerBegin.Length
  446.            fsInput.Read(metaBytes, 0, metaBytes.Length)
  447.            Dim meta As CertBlobMeta = MarshalExtensions.ConvertToStructure(Of CertBlobMeta)(metaBytes)
  448.  
  449.            ' Compute region to remove: markerBegin + meta + blob + markerEnd + padding (if any)
  450.            Dim removeStart As Integer = metaStart
  451.            Dim removeLen As Integer = markerBegin.Length + metaStructSize + meta.BlobSize + markerEnd.Length + meta.PaddingLength
  452.            ' Safety checks for corrupted meta or inconsistent Certificate Table.
  453.            If removeStart < 0 Then
  454.                Dim msg As String = "Computed removal region start is before the beginning of the Certificate Table."
  455.                Throw New InvalidOperationException(msg)
  456.            End If
  457.            If (removeStart + removeLen) > certDirSize Then
  458.                Dim msg As String = "Computed removal region extends beyond the Certificate Table."
  459.                Throw New InvalidOperationException(msg)
  460.            End If
  461.  
  462.            ' Write changes to output file.
  463.            Using fsOutput As New FileStream(outputFilePath, If(overwriteOutputFile, FileMode.Create, FileMode.CreateNew),
  464.                                             FileAccess.Write, FileShare.Read, bufferSize:=8192 * 2, FileOptions.None)
  465.  
  466.                ' Write head (0 to certDirRVA-1)
  467.                fsInput.Position = 0
  468.                StreamExtensions.CopyExactTo(fsInput, fsOutput, certDirRVA)
  469.  
  470.                ' Write new certificate table.
  471.                fsInput.Position = certDirRVA
  472.                StreamExtensions.CopyExactTo(fsInput, fsOutput, removeStart)
  473.                fsInput.Position = certDirRVA + removeStart + removeLen
  474.                Dim remain As Integer = certDirSize - (removeStart + removeLen)
  475.                If remain > 0 Then
  476.                    StreamExtensions.CopyExactTo(fsInput, fsOutput, remain)
  477.                End If
  478.  
  479.                ' Copy any original remainder bytes (tail).
  480.                Dim tailStart As Long = certDirRVA + certDirSize
  481.                If tailStart < fsInput.Length Then
  482.                    fsInput.Position = tailStart
  483.                    StreamExtensions.CopyExactTo(fsInput, fsOutput, CInt(fsInput.Length - tailStart))
  484.                End If
  485.  
  486.                UpdateCertificateTableLengths(fsInput, fsOutput, headers, certDirRVA, CUInt(certDirSize - removeLen))
  487.            End Using
  488.        End Using
  489.    End Sub
  490.  
  491.    ''' <summary>
  492.    ''' Removes all blobs —that were enclosed between the specified <paramref name="markerBegin"/> and <paramref name="markerEnd"/> markers—
  493.    ''' from the Certificate Table data-directory entry in the Portable Executable (PE) header of the given file.
  494.    ''' <para></para>
  495.    ''' The blob(s) must have been previously added with the <see cref="AppendBlobToPECertificateTable"/> function.
  496.    ''' </summary>
  497.    '''
  498.    ''' <param name="inputFilePath">
  499.    ''' Path to the input —digitally signed— Portable Executable (PE) file (e.g., "C:\Windows\explorer.exe")
  500.    ''' from which the blobs will be removed.
  501.    ''' </param>
  502.    '''
  503.    ''' <param name="outputFilePath">
  504.    ''' Path to the output file that will be written with the modified Certificate Table.
  505.    ''' <para></para>
  506.    ''' Cannot be the same as <paramref name="inputFilePath"/>.
  507.    ''' </param>
  508.    '''
  509.    ''' <param name="markerBegin">
  510.    ''' Optional. A byte sequence used to delimit the beginning of a data blob within the Certificate Table content.
  511.    ''' <para></para>
  512.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_BEGIN#</c>" in UTF-8 encoding bytes.
  513.    ''' <para></para>
  514.    ''' This value must be the same used when calling <see cref="AppendBlobToPECertificateTable"/> function.
  515.    ''' </param>
  516.    '''
  517.    ''' <param name="markerEnd">
  518.    ''' Optional. A byte sequence used to delimit the end of a data blob within the Certificate Table content.
  519.    ''' <para></para>
  520.    ''' Cannot be null or empty. Default value is "<c>#CERT_BLOB_END#</c>" in UTF-8 encoding bytes.
  521.    ''' <para></para>
  522.    ''' This value must be the same used when calling <see cref="AppendBlobToPECertificateTable"/> function.
  523.    ''' </param>
  524.    '''
  525.    ''' <param name="overwriteOutputFile">
  526.    ''' If <see langword="False"/> and the output file already exists, the method throws an <see cref="IOException"/>.
  527.    ''' <para></para>
  528.    ''' If <see langword="True"/>, any existing output file will be overwritten.
  529.    ''' <para></para>
  530.    ''' Default value is <see langword="False"/>.
  531.    ''' </param>
  532.    <DebuggerStepThrough>
  533.    Public Shared Sub RemoveBlobsFromPECertificateTable(inputFilePath As String,
  534.                                                        outputFilePath As String,
  535.                                                        Optional markerBegin As Byte() = Nothing,
  536.                                                        Optional markerEnd As Byte() = Nothing,
  537.                                                        Optional overwriteOutputFile As Boolean = False)
  538.  
  539.        ValidateCommonParameters((NameOf(inputFilePath), inputFilePath),
  540.                                 (NameOf(outputFilePath), outputFilePath),
  541.                                 (NameOf(markerBegin), markerBegin),
  542.                                 (NameOf(markerEnd), markerEnd),
  543.                                 (NameOf(overwriteOutputFile), overwriteOutputFile))
  544.  
  545.        Dim metaStructSize As Integer = Marshal.SizeOf(GetType(CertBlobMeta))
  546.  
  547.        Dim removalRanges As New List(Of Tuple(Of Integer, Integer))()
  548.  
  549.        Using fsInput As New FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read,
  550.                                        bufferSize:=8192 * 2, FileOptions.SequentialScan),
  551.              peReader As New PEReader(fsInput, PEStreamOptions.LeaveOpen)
  552.  
  553.            Dim headers As PEHeaders = Nothing
  554.            Dim certDirRVA As Integer, certDirSize As Integer
  555.            ValidatePEHeaderAndCertDir(peReader, headers, certDirRVA, certDirSize)
  556.  
  557.            fsInput.Position = certDirRVA
  558.            Dim certBytes As Byte() = StreamExtensions.ReadExact(fsInput, certDirSize)
  559.  
  560.            Dim searchIndex As Integer
  561.            While searchIndex < certBytes.Length
  562.                Dim idx As Integer = Array.IndexOf(certBytes, markerBegin(0), searchIndex)
  563.                ' Ensure there's room for full marker and meta.
  564.                If (idx = -1) OrElse (idx + markerBegin.Length + metaStructSize) >= certBytes.Length Then
  565.                    Exit While
  566.                End If
  567.  
  568.                ' Verify full start marker match.
  569.                Dim matchStart As Boolean = True
  570.                For j As Integer = 1 To markerBegin.Length - 1
  571.                    If certBytes(idx + j) <> markerBegin(j) Then
  572.                        matchStart = False
  573.                        Exit For
  574.                    End If
  575.                Next
  576.                If Not matchStart Then
  577.                    searchIndex = idx + 1
  578.                    Continue While
  579.                End If
  580.  
  581.                ' Read CertBlobMeta structure bytes.
  582.                Dim metaStart As Integer = idx + markerBegin.Length
  583.                Dim metaBytes(metaStructSize - 1) As Byte
  584.                Array.Copy(certBytes, metaStart, metaBytes, 0, metaStructSize)
  585.                Dim meta As CertBlobMeta = MarshalExtensions.ConvertToStructure(Of CertBlobMeta)(metaBytes)
  586.  
  587.                ' Compute region to remove: markerBegin + meta + blob + markerEnd + padding (if any)
  588.                Dim removeStart As Integer = idx
  589.                Dim removeLen As Integer = markerBegin.Length + metaStructSize + meta.BlobSize + markerEnd.Length + meta.PaddingLength
  590.                ' Safety checks for corrupted meta or inconsistent Certificate Table.
  591.                If removeStart < 0 Then
  592.                    Dim msg As String = "Computed removal region start is before the beginning of the Certificate Table."
  593.                    Throw New InvalidOperationException(msg)
  594.                End If
  595.                If (removeStart + removeLen) > certDirSize Then
  596.                    Dim msg As String = "Computed removal region extends beyond the Certificate Table."
  597.                    Throw New InvalidOperationException(msg)
  598.                End If
  599.  
  600.                removalRanges.Add(Tuple.Create(removeStart, removeLen))
  601.  
  602.                ' Advance searchIndex past the removed region.
  603.                searchIndex = removeStart + removeLen
  604.            End While
  605.  
  606.            ' If nothing to remove -> copy input to output unchanged (but still produce output file).
  607.            If removalRanges.Count = 0 Then
  608.                Using fsOut As New FileStream(outputFilePath, If(overwriteOutputFile, FileMode.Create, FileMode.CreateNew),
  609.                                              FileAccess.Write, FileShare.Read, bufferSize:=8192 * 2, FileOptions.None)
  610.                    fsInput.Position = 0
  611.                    fsInput.CopyTo(fsOut)
  612.                    ' StreamExtensions.CopyExactTo(fsInput, fsOut, CInt(fsInput.Length))
  613.                End Using
  614.                Exit Sub
  615.            End If
  616.  
  617.            ' Total removed size.
  618.            Dim totalRemoved As Integer = removalRanges.Sum(Function(t) t.Item2)
  619.  
  620.            ' Write changes to output file.
  621.            Using fsOutput As New FileStream(outputFilePath, If(overwriteOutputFile, FileMode.Create, FileMode.CreateNew),
  622.                                             FileAccess.Write, FileShare.Read, bufferSize:=8192 * 2, FileOptions.None)
  623.  
  624.                ' Write head (0 to certDirRVA-1)
  625.                fsInput.Position = 0
  626.                StreamExtensions.CopyExactTo(fsInput, fsOutput, certDirRVA)
  627.  
  628.                ' Write filtered certificate table segments.
  629.                Dim prevEnd As Integer = 0
  630.                For Each r As Tuple(Of Integer, Integer) In removalRanges
  631.                    Dim segStart As Integer = r.Item1
  632.                    Dim segLen As Integer = segStart - prevEnd
  633.                    If segLen > 0 Then
  634.                        ' Copy segment (prevEnd to segStart-1)
  635.                        fsInput.Position = certDirRVA + prevEnd
  636.                        StreamExtensions.CopyExactTo(fsInput, fsOutput, segLen)
  637.                    End If
  638.                    ' Skip the removed region by moving prevEnd.
  639.                    prevEnd = segStart + r.Item2
  640.                Next
  641.                ' Write remaining certificate bytes after last removal.
  642.                If prevEnd < certDirSize Then
  643.                    Dim lastLen As Integer = certDirSize - prevEnd
  644.                    fsInput.Position = certDirRVA + prevEnd
  645.                    StreamExtensions.CopyExactTo(fsInput, fsOutput, lastLen)
  646.                End If
  647.  
  648.                ' Copy any original remainder bytes (tail).
  649.                Dim tailStart As Long = certDirRVA + certDirSize
  650.                If tailStart < fsInput.Length Then
  651.                    fsInput.Position = tailStart
  652.                    StreamExtensions.CopyExactTo(fsInput, fsOutput, CInt(fsInput.Length - tailStart))
  653.                End If
  654.  
  655.                UpdateCertificateTableLengths(fsInput, fsOutput, headers, certDirRVA, CUInt(certDirSize - totalRemoved))
  656.            End Using
  657.        End Using
  658.    End Sub
  659.  
  660. End Class

El código continúa aquí abajo 👇🙂
32  Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets) en: 21 Septiembre 2025, 12:19 pm
Métodos universales para demostrar la vulnerabilidad de validación de firmas en WinVerifyTrust.

— Cómo ocultar y ejecutar malware desde un ejecutable firmado digitalmente —

Recientemente, descubrí el siguiente artículo sobre la vulnerabilidad CVE-2013-3900, conocida como la "Vulnerabilidad de validación de firmas en WinVerifyTrust":

  ◉ DeepInstinct - black hat USA 2016: Certificate Bypass: Hiding and Executing Malware from a Digitally Signed Executable

Esta vulnerabilidad afecta a la función WinVerifyTrust de la API de Windows responsable de verificar la autenticidad de las firmas digitales en archivos (exe, dll, etc), y consiste en la capacidad de un atacante para poder modificar un archivo ejecutable firmado, adjuntando código malicioso en la tabla de certificado ¡sin invalidar la firma digital del archivo!, lo que proporciona una técnica de ocultación muy discreta.

La vulnerabilidad se dio a conocer en el año 2013, pero sigue vigente en 2025 (también en Windows 11. De hecho, con más agravio que en versiones anteriores de Windows), y ha sido la forma de ataque a empresas en varias ocasiones (👉 10-Year-Old Windows Vulnerability Exploited in 3CX Attack)





Prueba de indetectabilidad

Vaya por delante que todo esto lo hago con fines educativos. No soy ningún experto en malware, y no experimento con ello. Pero intentaré aportar lo que pueda:

Para ilustrar brevemente la efectividad de esta vulnerabilidad en 2025, podemos usar como ejemplo el EICAR, un archivo de prueba diseñado para evaluar y verificar el funcionamiento del software antivirus. Se trata de un virus simulado que provoca la reacción del motor antivirus, permitiendo demostrar su capacidad para detectar y neutralizar posibles amenazas.

Se puede descargar aquí: https://www.eicar.org/download-anti-malware-testfile/

Para esta prueba utilizaré el archivo eicar_com.zip (el zip comprimido tal cual).

Bien. 👇 Este es el diagnóstico de VirusTotal del archivo eicar_com.zip:

  ◉ 2546dcffc5ad854d4ddc64fbf056871cd5a00f2471cb7a5bfd4ac23b6e9eedad — 62 detecciones de 69 AVs.


👇 Este es el diagnóstico de VirusTotal de una simple aplicación de consola desarrollada en .NET 4.8, que contiene la representación literal en bytes del archivo eicar_com.zip:
Código
  1. Friend Module Module1
  2.  
  3.    Private ReadOnly rawBytes As Byte() = {
  4.        &H50, ... el resto de bytes ...
  5.    }
  6.  
  7.    Sub Main()
  8.    End Sub
  9. End Module

  ◉ 7a11573dbb67f839390c29a3615d4627d419d571ee29f6170cab22d87550f5b1 — 21 detecciones de 72 AVs.


👇 Este es el diagnóstico de VirusTotal de la misma aplicación de consola, pero cifrada con el packer Enigma:

  ◉ eab90e4659a3414e0b09c9036f07318d0356be6382a5198a16ef73621473c0f2 — 23 detecciones de 72 AVs.


Y, por último, 👇 este es el diagnóstico de VirusTotal de un archivo ejecutable firmado, en este caso el propio y legítimo explorer.exe con certificado digital de Microsoft, al que le he adjuntado la aplicación de consola anterior — cifrada con el packer Enigma — al final de la tabla de certificado:

  ◉ 310025562eb9c497615ffcb6040d9fa5ba6de82b272523656d3a585765d85580 — 3 detecciones de 72 AVs.


Y lo mejor de todo, aparte de la reducción en detecciones, es que la firma no se ha invalidado, por lo que a ojos del sistema operativo sigue siendo un archivo legítimo y totalmente confiable 👍:



Cabe mencionar que si solamente adjuntásemos un archivo PE malicioso y sin cifrar a la tabla de certificado, habría muchas detecciones de AVs, y Windows nos advertiría de que la firma no tiene un formato adecuado:



(Sin embargo, la firma sigue siendo válida, solo que Windows ha detectado que la tabla de certificado no sigue un formato apropiado.)
Nota: El hipervínculo mostrado en la advertencia nos llevará al siguiente artículo: MS13-098: Una vulnerabilidad en Windows podría permitir la ejecución remota de código: 10 de diciembre de 2013

Por lo que yo he experimentado, esta advertencia al examinar la firma digital de un archivo solo se produce al adjuntar archivos PE y sin cifrar a la tabla de certificado. Podemos adjuntar cualquier tipo de documento de texto plano, imágenes y videos, que estén sin cifrar, y Windows no mostrará ningún aviso sobre formato incorrecto.

Por que sí, amigos, aunque esto sería un método descubierto y usado principalmente para ocultar malware, también podríamos darle un uso más didáctico y de utilidad para un mayor número de usuarios, como podría ser la capacidad de ocultar documentos o contraseñas de forma segura donde nadie jamás va a ponerse a mirar: en la tabla de certificado de un archivo PE.

Para un archivo con un certificado corrupto, Windows puede mostrar esto:



Y para un archivo con un certificado digital inválido, Windows muestra este mensaje:



(Esa captura de pantalla la he sacado de Internet y la he editado, sí, pero creanme, he invalidado el certificado varias veces y ponía algo así, "El certificado no es válido.")

Sin más dilación, vamos con el código que he desarrollado...



Características principales del código

Estas son las principales funciones que he desarrollado:

AppendBlobToPECertificateTable: Añade un bloque de datos al final de la tabla de certificado de un archivo PE.
RemoveBlobFromPECertificateTable: Elimina un bloque de datos específico de la tabla de certificado de un archivo PE.
RemoveBlobsFromPECertificateTable: Elimina todos los bloques de datos de la tabla de certificado de un archivo PE.
GetBlobsFromPECertificateTable: Devuelve una colección con todos los bloques de datos presentes en la tabla de certificado de un archivo PE.

Además, también he incluído las siguientes funciones auxiliares de utilidad general:

FileIsPortableExecutable: Determina si un archivo es de facto un archivo PE válido.
FileHasCertificateTable: Determina si un archivo PE contiene una tabla de certificado que no esté vacía. No valida la firma ni el contenido de los certificados; solo verifica la presencia de la tabla.
FileHasCertificate: Determina si un archivo PE contiene un certificado válido que se pueda leer/parsear. No valida la cadena de confianza, expiración ni revocación del certificado.
MarshalExtensions.ConvertToStructure y MarshalExtensions.ConvertToBytes
StreamExtensions.ReadExact y StreamExtensions.CopyExactTo

💡 Al final de este hilo muestro un breve ejemplo de uso para todas las funciones principales 👍



El código fuente

Imports necesarios:
Código
  1. Imports System.Collections.Immutable
  2. Imports System.Collections.ObjectModel
  3. Imports System.ComponentModel
  4. Imports System.IO
  5. Imports System.Reflection.PortableExecutable
  6. Imports System.Runtime.CompilerServices
  7. Imports System.Runtime.InteropServices
  8. Imports System.Security.Cryptography
  9. Imports System.Security.Cryptography.X509Certificates
  10. Imports System.Text

Módulo MarshalExtensions:
Código
  1. ''' <summary>
  2. ''' Provides extension methods related to marshaling operations.
  3. ''' </summary>
  4. Public Module MarshalExtensions
  5.  
  6.    ''' <summary>
  7.    ''' Converts a byte array into a managed structure of type <typeparamref name="T"/>.
  8.    ''' </summary>
  9.    '''
  10.    ''' <typeparam name="T">
  11.    ''' The structure type to convert the byte array into.
  12.    ''' </typeparam>
  13.    '''
  14.    ''' <param name="structBytes">
  15.    ''' The byte array containing the raw data for the structure.
  16.    ''' </param>
  17.    '''
  18.    ''' <returns>
  19.    ''' A managed structure of type <typeparamref name="T"/> populated with data from the <paramref name="structBytes"/> byte array.
  20.    ''' </returns>
  21.    <Extension>
  22.    <EditorBrowsable(EditorBrowsableState.Advanced)>
  23.    Public Function ConvertToStructure(Of T As Structure)(structBytes As Byte()) As T
  24.  
  25.        Dim handle As GCHandle = GCHandle.Alloc(structBytes, GCHandleType.Pinned)
  26.        Try
  27.            Return Marshal.PtrToStructure(Of T)(handle.AddrOfPinnedObject())
  28.        Finally
  29.            handle.Free()
  30.        End Try
  31.    End Function
  32.  
  33.    ''' <summary>
  34.    ''' Converts a managed structure of type <typeparamref name="T"/> into a byte array.
  35.    ''' </summary>
  36.    '''
  37.    ''' <typeparam name="T">
  38.    ''' The structure type to convert to a byte array.
  39.    ''' </typeparam>
  40.    '''
  41.    ''' <param name="struct">
  42.    ''' The structure instance to convert.
  43.    ''' </param>
  44.    '''
  45.    ''' <returns>
  46.    ''' A byte array representing the raw memory of the structure.
  47.    ''' </returns>
  48.    <Extension>
  49.    <EditorBrowsable(EditorBrowsableState.Advanced)>
  50.    Public Function ConvertToBytes(Of T As Structure)(struct As T) As Byte()
  51.  
  52.        Dim size As Integer = Marshal.SizeOf(GetType(T))
  53.        Dim bytes(size - 1) As Byte
  54.        Dim ptr As IntPtr = Marshal.AllocHGlobal(size)
  55.        Try
  56.            Marshal.StructureToPtr(struct, ptr, True)
  57.            Marshal.Copy(ptr, bytes, 0, size)
  58.        Finally
  59.            Marshal.FreeHGlobal(ptr)
  60.        End Try
  61.        Return bytes
  62.    End Function
  63.  
  64. End Module

Módulo StreamExtensions:
Código
  1. ''' <summary>
  2. ''' Provides extension methods for <see cref="Stream"/>.
  3. ''' </summary>
  4. Public Module StreamExtensions
  5.  
  6.    ''' <summary>
  7.    ''' Reads exactly the specified amount of bytes from the current stream, and advances the position within the stream.
  8.    ''' </summary>
  9.    '''
  10.    ''' <param name="stream">
  11.    ''' The source <see cref="Stream"/> to read from.
  12.    ''' </param>
  13.    '''
  14.    ''' <param name="count">
  15.    ''' The exact number of bytes to be read from the stream.
  16.    ''' </param>
  17.    '''
  18.    ''' <returns>
  19.    ''' A <see cref="Byte()"/> array containing the bytes read from the stream.
  20.    ''' </returns>
  21.    '''
  22.    ''' <exception cref="ArgumentNullException">
  23.    ''' Thrown if <paramref name="stream"/> is null.
  24.    ''' </exception>
  25.    '''
  26.    ''' <exception cref="ArgumentException">
  27.    ''' Thrown if <paramref name="stream"/> is empty.
  28.    ''' </exception>
  29.    '''
  30.    ''' <exception cref="ArgumentOutOfRangeException">
  31.    ''' Thrown if <paramref name="count"/> is less than or equal to zero.
  32.    ''' <para></para>
  33.    ''' Thrown if <paramref name="count"/> is greater than the bytes available from the current position in the stream.
  34.    ''' </exception>
  35.    '''
  36.    ''' <exception cref="IOException">
  37.    ''' Thrown if <paramref name="stream"/> is not readable.
  38.    ''' </exception>
  39.    '''
  40.    ''' <exception cref="EndOfStreamException">
  41.    ''' Thrown if the stream ends before <paramref name="count"/> bytes are read.
  42.    ''' </exception>
  43.    <Extension>
  44.    <EditorBrowsable(EditorBrowsableState.Always)>
  45.    Public Function ReadExact(stream As Stream, count As Integer) As Byte()
  46.  
  47.        If stream Is Nothing Then
  48.            Throw New ArgumentNullException(paramName:=NameOf(stream))
  49.        End If
  50.  
  51.        If Not stream.CanRead Then
  52.            Dim msg As String = "The source stream does not support reading."
  53.            Throw New IOException(msg)
  54.        End If
  55.  
  56.        If stream.Length <= 0 Then
  57.            Dim msg As String = "The source stream is empty, cannot read any bytes."
  58.            Throw New ArgumentException(msg, paramName:=NameOf(stream))
  59.        End If
  60.  
  61.        If count <= 0 Then
  62.            Dim msg As String = "Count must be greater than 0."
  63.            Throw New ArgumentOutOfRangeException(paramName:=NameOf(count), count, msg)
  64.        End If
  65.  
  66.        If (stream.Position + count) > stream.Length Then
  67.            Dim msg As String = $"Requested {count} bytes, but only {stream.Length - stream.Position} bytes are available from the current position in the source stream."
  68.            Throw New ArgumentOutOfRangeException(paramName:=NameOf(count), count, msg)
  69.        End If
  70.  
  71.        Dim buffer(count - 1) As Byte
  72.        Dim totalRead As Integer
  73.  
  74.        While totalRead < buffer.Length
  75.            Dim read As Integer = stream.Read(buffer, totalRead, buffer.Length - totalRead)
  76.            If read = 0 Then
  77.                Dim msg As String = "Source stream ended before the requested number of bytes were read."
  78.                Throw New EndOfStreamException(msg)
  79.            End If
  80.            totalRead += read
  81.        End While
  82.  
  83.        Return buffer
  84.    End Function
  85.  
  86.    ''' <summary>
  87.    ''' Reads exactly the specified amount of bytes from the current stream and writes them to another stream.
  88.    ''' </summary>
  89.    '''
  90.    ''' <param name="source">
  91.    ''' The <see cref="Stream"/> from which to copy the contents to the <paramref name="destination"/> stream.
  92.    ''' </param>
  93.    '''
  94.    ''' <param name="destination">
  95.    ''' The <see cref="Stream"/> to which the contents of the <paramref name="source"/> stream will be copied.
  96.    ''' </param>
  97.    '''
  98.    ''' <param name="count">
  99.    ''' The exact number of bytes to copy from the source stream.
  100.    ''' </param>
  101.    '''
  102.    ''' <param name="bufferSize">
  103.    ''' The size of the buffer. This value must be greater than zero.
  104.    ''' <para></para>
  105.    ''' The default size is 81920.
  106.    ''' </param>
  107.    '''
  108.    ''' <exception cref="ArgumentNullException">
  109.    ''' Thrown if <paramref name="source"/> or <paramref name="destination"/> are null.
  110.    ''' </exception>
  111.    '''
  112.    ''' <exception cref="ArgumentException">
  113.    ''' Thrown if the <paramref name="source"/> stream is empty.
  114.    ''' </exception>
  115.    '''
  116.    ''' <exception cref="ArgumentOutOfRangeException">
  117.    ''' Thrown if <paramref name="count"/> or <paramref name="bufferSize"/> are less than or equal to zero.
  118.    ''' </exception>
  119.    '''
  120.    ''' <exception cref="IOException">
  121.    ''' Thrown if <paramref name="source"/> stream is not readable or <paramref name="destination"/> stream is not writable.
  122.    ''' </exception>
  123.    '''
  124.    ''' <exception cref="EndOfStreamException">
  125.    ''' Thrown if the <paramref name="source"/> stream ends before <paramref name="count"/> bytes are copied.
  126.    ''' </exception>
  127.    <Extension>
  128.    <EditorBrowsable(EditorBrowsableState.Always)>
  129.    Public Sub CopyExactTo(source As Stream, destination As Stream, count As Integer,
  130.                           Optional bufferSize As Integer = 81920)
  131.  
  132.        If source Is Nothing Then
  133.            Throw New ArgumentNullException(paramName:=NameOf(source))
  134.        End If
  135.  
  136.        If destination Is Nothing Then
  137.            Throw New ArgumentNullException(paramName:=NameOf(destination))
  138.        End If
  139.  
  140.        If Not source.CanRead Then
  141.            Dim msg As String = "The source stream does not support reading."
  142.            Throw New IOException(msg)
  143.        End If
  144.  
  145.        If Not destination.CanWrite Then
  146.            Dim msg As String = "The destination stream does not support writting."
  147.            Throw New IOException(msg)
  148.        End If
  149.  
  150.        If source.Length <= 0 Then
  151.            Dim msg As String = "The source stream is empty, cannot read any bytes."
  152.            Throw New ArgumentException(msg, paramName:=NameOf(source))
  153.        End If
  154.  
  155.        If count <= 0 Then
  156.            Dim msg As String = "Count must be greater than 0."
  157.            Throw New ArgumentOutOfRangeException(paramName:=NameOf(count), count, msg)
  158.        End If
  159.  
  160.        If bufferSize <= 0 Then
  161.            Dim msg As String = "Buffer size must be greater than 0."
  162.            Throw New ArgumentOutOfRangeException(paramName:=NameOf(bufferSize), bufferSize, msg)
  163.        End If
  164.  
  165.        Dim buffer(bufferSize - 1) As Byte
  166.        Dim remaining As Integer = count
  167.  
  168.        While remaining > 0
  169.            Dim toRead As Integer = Math.Min(buffer.Length, remaining)
  170.            Dim read As Integer = source.Read(buffer, 0, toRead)
  171.            If read = 0 Then
  172.                Dim msg As String = "Source stream ended before the requested number of bytes were copied."
  173.                Throw New EndOfStreamException(msg)
  174.            End If
  175.            destination.Write(buffer, 0, read)
  176.            remaining -= read
  177.        End While
  178.    End Sub
  179.  
  180. End Module

El código continúa aquí abajo 👇🙂
33  Programación / Scripting / [APORTE] [PowerShell] [VBS] Mostrar el tiempo transcurrido desde el último arranque del sistema. en: 8 Septiembre 2025, 00:57 am
El siguiente script, desarrollado en PowerShell, crea una ventana gráfica (Form) que muestra, en tiempo real, el tiempo transcurrido desde el último arranque (uptime) del sistema:


( Nota: el efecto de parpadeo o flickering es debido a la captura del GIF animado )

Es un script muy simple y su único cometido es ese. Yo lo utilizo en una máquina virtual, aunque cada persona podría encontrarle usos diferentes.

El código:
Código
  1. Add-Type -AssemblyName System.Drawing
  2. Add-Type -AssemblyName System.Windows.Forms
  3.  
  4. Add-Type @"
  5. using System;
  6. using System.Runtime.InteropServices;
  7.  
  8. public static class WinAPI {
  9.    [DllImport("kernel32.dll")]
  10.    public static extern IntPtr GetConsoleWindow();
  11.  
  12.    [DllImport("user32.dll")]
  13.    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  14.  
  15.    [DllImport("shell32.dll", CharSet=CharSet.Unicode)]
  16.    public static extern int ExtractIconEx(string lpszFile, int nIconIndex, out IntPtr phiconLarge, out IntPtr phiconSmall, int nIcons);
  17.  
  18.    [DllImport("user32.dll", CharSet=CharSet.Auto)]
  19.    public static extern bool DestroyIcon(IntPtr handle);
  20. }
  21. "@
  22.  
  23. # --- SINGLE INSTANCE CHECK THROUGH MUTEX ---
  24. $mutexName = "Global\ComputerUptimeFormMutex"
  25. $createdNew = $false
  26. $mutex = New-Object System.Threading.Mutex($true, $mutexName, [ref]$createdNew)
  27.  
  28. if (-not $createdNew) {
  29.    [System.Windows.Forms.MessageBox]::Show(
  30.        "Only one instance of this program is allowed.",
  31.        "Computer Uptime",
  32.        [System.Windows.Forms.MessageBoxButtons]::OK,
  33.        [System.Windows.Forms.MessageBoxIcon]::Stop
  34.    )
  35.    exit
  36. }
  37.  
  38. # --- HIDE CURRENT POWERSHELL CONSOLE ---
  39. $SW_HIDE = 0
  40. $hWnd = [WinAPI]::GetConsoleWindow()
  41. [WinAPI]::ShowWindow($hWnd, $SW_HIDE) | Out-Null
  42.  
  43. # --- CREATE THE FORM ---
  44. $form                 = New-Object System.Windows.Forms.Form
  45. $form.Text            = "Computer Uptime"
  46. $form.Size            = New-Object System.Drawing.Size(350, 150)
  47. $form.StartPosition   = "CenterScreen"
  48. $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
  49. $form.MaximizeBox     = $false
  50. $form.MinimizeBox     = $true
  51. $form.Padding         = New-Object System.Windows.Forms.Padding(4)
  52. $form.DoubleBuffered  = $true
  53.  
  54. # --- SET FORM ICON ---
  55. $shell32   = "$env:SystemRoot\System32\shell32.dll"
  56. $hLarge    = [IntPtr]::Zero
  57. $hSmall    = [IntPtr]::Zero
  58. $iconIndex = 265 # A clock icon in Windows 10.
  59.  
  60. [WinAPI]::ExtractIconEx($shell32, $iconIndex, [ref]$hLarge, [ref]$hSmall, 1) | Out-Null
  61.  
  62. if ($hSmall -ne [IntPtr]::Zero) {
  63.    $form.Icon = [System.Drawing.Icon]::FromHandle($hSmall)
  64. }
  65.  
  66. # --- LABEL TO DISPLAY UPTIME ---
  67. $label                = New-Object System.Windows.Forms.Label
  68. $label.Font           = New-Object System.Drawing.Font("Segoe UI", 14, [System.Drawing.FontStyle]::Bold)
  69. $label.Dock           = [System.Windows.Forms.DockStyle]::Fill
  70. $label.TextAlign      = [System.Drawing.ContentAlignment]::MiddleCenter
  71. $label.AutoSize       = $false
  72. $label.DoubleBuffered = $true
  73. $form.Controls.Add($label)
  74.  
  75. # --- GET SYSTEM INFORMATION FROM WMI ---
  76. $os           = Get-CimInstance Win32_OperatingSystem
  77. $bootTime     = $os.LastBootUpTime
  78. $computerName = $os.CSName
  79.  
  80. # --- TIMER TO UPDATE UPTIME ---
  81. $timer = New-Object System.Windows.Forms.Timer
  82. $timer.Interval = 100
  83. $timer.Add_Tick({
  84.    $uptime       = (Get-Date) - $bootTime
  85.    $minutes      = $uptime.Minutes.ToString("00")
  86.    $seconds      = $uptime.Seconds.ToString("00")
  87.    $milliseconds = $uptime.Milliseconds.ToString("000")
  88.    $label.Text   = "$computerName`n`n$($uptime.Days) days — $($uptime.Hours)h : $($minutes)m : $($seconds)s : $($milliseconds)ms"
  89. })
  90. $timer.Start()
  91.  
  92. # --- RELEASE ICON HANDLES AND MUTEX WHEN FORM GETS CLOSED ---
  93. $form.Add_FormClosed({
  94.    if ($hSmall -ne [IntPtr]::Zero) { [WinAPI]::DestroyIcon($hSmall) }
  95.    if ($hLarge -ne [IntPtr]::Zero) { [WinAPI]::DestroyIcon($hLarge) }
  96.    $mutex.ReleaseMutex()
  97. })
  98.  
  99. # --- SHOW THE FORM ---
  100. [void]$form.ShowDialog()

PD: 80% del código fue hecho por ChatGPT (considero una pérdida de tiempo diseñar manualmente el form en texto plano, además de buscar y añadir las definiciones de la API de Windows, cosas que puede hacer una IA perfectamente y en menos de un segundo), 20% edición y revisión humana. De todas formas, esto no tendría ningún mérito haberlo hecho a mano en un 100%, pero aun así quiero ser honesto con lo que comparto.



Por último, les muestro una especie de equivalente mucho más básico hecho con VisualBasic Script (VBS). El siguiente código tan solo muestra un cuadro de mensaje, sin actualización en tiempo real de ningún tipo.

Código
  1. Option Explicit
  2.  
  3. Dim oneMinute, oneHour, oneDay: oneMinute = 60: oneHour = 3600: oneDay = 86400
  4. Dim objWMIService, colOperatingSystems, objOperatingSystem
  5. Dim computerName, lastBootUpTime, upTime
  6.  
  7. Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  8. Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
  9.  
  10. For Each objOperatingSystem In colOperatingSystems
  11.    computerName = objOperatingSystem.CSName
  12.    lastBootUpTime = objOperatingSystem.LastBootUpTime
  13.    lastBootUpTime = CDate(Mid(lastBootUpTime, 1, 4) & "/" & Mid(lastBootUpTime, 5, 2) & "/" & Mid(lastBootUpTime, 7, 2) & " " & _
  14.                           Mid(lastBootUpTime, 9, 2) & ":" & Mid(lastBootUpTime, 11, 2) & ":" & Mid(lastBootUpTime, 13, 2))
  15.  
  16.    upTime = DateDiff("s", lastBootUpTime, Now)
  17.    MsgBox computerName & vbCrLf & vbCrLf  & _
  18.           upTime \ oneDay & " days ~ " & _
  19.           (upTime Mod oneDay) \ oneHour & "h : " & _
  20.           (upTime Mod oneHour) \ oneMinute & "m : " & _
  21.           upTime Mod oneMinute & "s", vbInformation, "Computer Uptime"
  22.  
  23. Next
  24.  
  25. WScript.Quit(0)
34  Foros Generales / Sugerencias y dudas sobre el Foro / ¿Han recibido mi e-mail? en: 7 Septiembre 2025, 22:20 pm
Hola. Envié un correo a varias direcciones que supuestamente son del staff de elhacker.net, lo siento por parecer pesado pero me quitarían una preocupación de encima si alguien me confirmase que han recibido el correo. Por que no sé si en alguna (o todas) esas direcciones de correo tal vez me tienen bloqueado por discusiones ocurridas en el pasado.

El e-mail que les he enviado es en relación a mi petición para eliminar un e-mail que aparece en un post del sitio web https://forum.elhacker.net/, y ahí explico todos los detalles...

Este es el segundo hilo que abro al respecto, y sé que solo han pasado dos días, pero me preocupo con facilidad, sobre todo cuando el primer hilo lo han borrado sin ofrecer respuesta, y por el momento nadie se ha puesto en contacto conmigo. (¿ustedes suelen fijarse en los hilos borrados de la papelera?).

Por favor tengan en cuenta que yo desconozco quien tiene acceso para administrar ese sitio web, no sé si solamente el-brujo es capaz, y a lo mejor por eso nadie ha querido ofrecerme una respuesta ni contactar conmigo. En ese caso díganmelo y contactaré con él por WhatsApp, como ya os dije en una ocasión no quiero recurrir a eso sin su consentimiento para terminar molestando... si no fuese realmente necesario.

Ya he enviado la correspondiente solicitud de retirada de contenido a Google para ver si ellos pueden eliminar ese resultado de búsqueda donde aparece el post de https://forum.elhacker.net/, pero desconozco cuanto tiempo puede tardar en resolverse este tipo de denuncia, y al final pueden decidir no hacer nada al respecto, así que por favor necesito que ustedes me ayuden con lo que está en vuestras manos poder hacer.

Quiero pensar que en esta ocasión no me perciben como Elektro "el que le cae mal a todo el staff", sino como una persona que simplemente solicita algo tan razonable como poder eliminar cierta información personal que no debería ser accesible de forma pública como lo es desde ese sitio web, y a su vez desde un motor de búsqueda tipo Google.

Si ustedes revocasen mi baneo en ese sitio web, y suponiendo que yo pudiera editar un post de esa antigüedad, pues podría hacer yo mismo la tarea de eliminar ese e-mail y así no les quitaría más tiempo con este asunto.

Gracias de antemano.
35  Media / Multimedia / Re: comprobar estado de video en: 5 Septiembre 2025, 19:02 pm
yo me refiero cuando metes una pelicula dvd en clonedvd la comprueba

No sé a qué te refieres exactamente. De todas formas, probablemente lo que ese programa haga (y cualquier otro, como por ejemplo MakeMKV) sea una validación inicial para determinar que el formato y estructura del DVD que estás intentando cargar coincida con lo que se espera de un DVD de video, y tras esa validación inicial luego identificará la cantidad de pistas de video, audio y subtítulos, los títulos, etc, pero dudo mucho que haga algo como "comprobar el estado" del video.

El título que le asignaste al hilo es: "comprobar estado de video", refiriéndote a archivos MKV y MP4, así que voy a responder a eso:

Para comprobar si un archivo en formato MKV o MP4 está corrupto, es suficiente con cargar el archivo en el programa MKVToolnix (descarga) y presionar el botón "Iniciar multiplexado". Si la operación se completa sin ningún registro de avisos ni errores, entonces las pistas del contenedor MKV/MP4 están en perfectas condiciones.

Atentamente,
Elektro.
36  Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets) en: 3 Septiembre 2025, 01:34 am
Métodos universales para trabajar (los últimos) aspectos básicos con fuentes de texto (.ttf y .otf)...

Funciones 'UtilFonts.GetFontGlyphOutlineData' y 'FontExtensions.GetGlyphOutlineData'

    Sirven para obtener los datos crudos de contorno (outline) de un glifo para un carácter específico en una fuente.

    Devuelven un array de bytes que representa la forma vectorial del glifo en el formato solicitado (Native o Bezier).

    Estos datos se pueden usar como base para comparaciones de glifos.

Funciones 'UtilFonts.FontGlyphOutlinesAreEqual' y 'FontExtensions.GlyphOutlinesAreEqual'

    Sirven para comparar si dos fuentes producen los mismos datos de contorno (outline) de un glifo para un carácter específico.

Funciones 'UtilFonts.GetFontGlyphOutlineSimilarity' y 'FontExtensions.GetGlyphOutlineSimilarity'

    Sirven para calcular un índice de similitud entre los contornos de un glifo para un carácter específico en dos fuentes distintas.

    Se puede usar cuando se quiere medir cuán parecidos son los glifos entre dos fuentes, en lugar de solo saber si son exactamente iguales.



El código fuente

⚠️ Importante: Para poder utilizar este código se requieren algunas definiciones de la API de Windows que he compartido en el post anterior a este. No lo comparto aquí de nuevo para evitar repetir código y evitar que este post quede demasiado grande y tedioso de leer. 🙏

Código
  1. Public Class UtilFonts
  2.  
  3.    ''' <summary>
  4.    ''' Prevents a default instance of the <see cref="UtilFonts"/> class from being created.
  5.    ''' </summary>
  6.    Private Sub New()
  7.    End Sub
  8.  
  9.    ''' <summary>
  10.    ''' Retrieves the raw outline data for a given glyph from the specified font file.
  11.    ''' <para></para>
  12.    ''' This function calls <see cref="DevCase.Win32.NativeMethods.GetGlyphOutline"/> in background
  13.    ''' to retrieve outline data with the requested <paramref name="format"/>.
  14.    ''' </summary>
  15.    '''
  16.    ''' <param name="fontFile">
  17.    ''' Path to the font file from which the glyph will be obtained.
  18.    ''' </param>
  19.    '''
  20.    ''' <param name="ch">
  21.    ''' The character whose glyph outline will be requested.
  22.    ''' </param>
  23.    '''
  24.    ''' <param name="format">
  25.    ''' The format in which the glyph outline will be retrieved.
  26.    ''' <para></para>
  27.    ''' This value only can be <see cref="GetGlyphOutlineFormat.Native"/> or <see cref="GetGlyphOutlineFormat.Bezier"/>.
  28.    ''' <para></para>
  29.    ''' Note: callers must interpret the returned byte array based on the selected format.
  30.    ''' </param>
  31.    '''
  32.    ''' <param name="matrix">
  33.    ''' An optional <see cref="GlyphOutlineMatrix2"/> used to transform the glyph outline.
  34.    ''' <para></para>
  35.    ''' If no value is provided or default structure is passed, an identity matrix
  36.    ''' will be used (see: <see cref="GlyphOutlineMatrix2.GetIdentityMatrix()"/>),
  37.    ''' where the transfromed graphical object is identical to the source object.
  38.    ''' </param>
  39.    '''
  40.    ''' <returns>
  41.    ''' A <see cref="Byte"/> array containing the raw glyph outline data with the requested <paramref name="format"/>.
  42.    ''' <para></para>
  43.    ''' Returns <see langword="Nothing"/> if the glyph is empty in the specified font.
  44.    ''' </returns>
  45.    '''
  46.    ''' <exception cref="FileNotFoundException">
  47.    ''' Thrown when the font file is not found.
  48.    ''' </exception>
  49.    <DebuggerStepThrough>
  50.    Public Shared Function GetFontGlyphOutlineData(fontFile As String, ch As Char, format As GetGlyphOutlineFormat,
  51.                                                   Optional matrix As GlyphOutlineMatrix2 = Nothing) As Byte()
  52.  
  53.        If Not File.Exists(fontFile) Then
  54.            Throw New FileNotFoundException("Font file not found.", fileName:=fontFile)
  55.        End If
  56.  
  57.        Using pfc As New PrivateFontCollection()
  58.            pfc.AddFontFile(fontFile)
  59.  
  60.            Using f As New Font(pfc.Families(0), emSize:=1)
  61.                Return FontExtensions.GetGlyphOutlineData(f, ch, format, matrix)
  62.            End Using
  63.        End Using
  64.    End Function
  65.  
  66.    ''' <summary>
  67.    ''' Determines whether the glyph outline for the specified character is identical in two font files.
  68.    ''' </summary>
  69.    '''
  70.    ''' <param name="firstFontFile">
  71.    ''' Path to the first font file to compare.
  72.    ''' </param>
  73.    '''
  74.    ''' <param name="secondFontFile">
  75.    ''' Path to the second font file to compare.
  76.    ''' </param>
  77.    '''
  78.    ''' <param name="ch">
  79.    ''' The character whose glyph outline will be compared between the two fonts.
  80.    ''' </param>
  81.    '''
  82.    ''' <returns>
  83.    ''' <see langword="True"/> if both fonts produce identical outlines for the specified glyph.
  84.    ''' <para></para>
  85.    ''' <see langword="False"/> if the outlines differ or if one of the fonts has an empty glyph.
  86.    ''' If the glyph outlines are empty in both fonts, returns <see langword="True"/>.
  87.    ''' </returns>
  88.    '''
  89.    ''' <exception cref="FileNotFoundException">
  90.    ''' Thrown when one of the font files is not found.
  91.    ''' </exception>
  92.    <DebuggerStepThrough>
  93.    Public Shared Function FontGlyphOutlinesAreEqual(firstFontFile As String, secondFontFile As String, ch As Char) As Boolean
  94.  
  95.        If Not File.Exists(firstFontFile) Then
  96.            Throw New FileNotFoundException("First font file not found.", fileName:=firstFontFile)
  97.        End If
  98.  
  99.        If Not File.Exists(secondFontFile) Then
  100.            Throw New FileNotFoundException("Second ont file not found.", fileName:=secondFontFile)
  101.        End If
  102.  
  103.        Using firstPfc As New PrivateFontCollection(),
  104.              secondPfc As New PrivateFontCollection()
  105.  
  106.            firstPfc.AddFontFile(firstFontFile)
  107.            secondPfc.AddFontFile(secondFontFile)
  108.  
  109.            Using firstFont As New Font(firstPfc.Families(0), emSize:=1),
  110.                  secondFont As New Font(secondPfc.Families(0), emSize:=1)
  111.  
  112.                Return FontExtensions.GlyphOutlineIsEqualTo(firstFont, secondFont, ch)
  113.            End Using
  114.        End Using
  115.    End Function
  116.  
  117.    ''' <summary>
  118.    ''' Computes a similarity score between the glyph outline for the specified character in two font files.
  119.    ''' </summary>
  120.    '''
  121.    ''' <param name="firstFontFile">
  122.    ''' Path to the first font file to compare.
  123.    ''' </param>
  124.    '''
  125.    ''' <param name="secondFontFile">
  126.    ''' Path to the second font file to compare.
  127.    ''' </param>
  128.    '''
  129.    ''' <param name="ch">
  130.    ''' The character whose glyph outline will be compared between the two fonts.
  131.    ''' </param>
  132.    '''
  133.    ''' <returns>
  134.    ''' A <see cref="Single"/> value between 0.0 and 1.0 representing the similarity
  135.    ''' (the number of matching bytes in the outline data) of the glyph outlines.
  136.    ''' <para></para>
  137.    ''' If one of the fonts has an empty glyph, returns 0. If the glyph outlines are empty in both fonts, returns 1.
  138.    ''' </returns>
  139.    '''
  140.    ''' <exception cref="FileNotFoundException">
  141.    ''' Thrown when one of the font files is not found.
  142.    ''' </exception>
  143.    <DebuggerStepThrough>
  144.    Public Shared Function GetFontGlyphOutlineSimilarity(firstFontFile As String, secondFontFile As String, ch As Char) As Single
  145.  
  146.        If Not File.Exists(firstFontFile) Then
  147.            Throw New FileNotFoundException("First font file not found.", fileName:=firstFontFile)
  148.        End If
  149.  
  150.        If Not File.Exists(secondFontFile) Then
  151.            Throw New FileNotFoundException("Second ont file not found.", fileName:=secondFontFile)
  152.        End If
  153.  
  154.        Using firstPfc As New PrivateFontCollection(),
  155.              secondPfc As New PrivateFontCollection()
  156.  
  157.            firstPfc.AddFontFile(firstFontFile)
  158.            secondPfc.AddFontFile(secondFontFile)
  159.  
  160.            Using firstFont As New Font(firstPfc.Families(0), emSize:=1),
  161.                  secondFont As New Font(secondPfc.Families(0), emSize:=1)
  162.  
  163.                Return FontExtensions.GetGlyphOutlineSimilarity(firstFont, secondFont, ch)
  164.            End Using
  165.        End Using
  166.    End Function
  167.  
  168. End Class

y:

Código
  1. Module FontExtensions
  2.  
  3.    ''' <summary>
  4.    ''' Retrieves the raw outline data for a given glyph from the specified <see cref="System.Drawing.Font"/>.
  5.    ''' <para></para>
  6.    ''' This function calls <see cref="DevCase.Win32.NativeMethods.GetGlyphOutline"/> in background
  7.    ''' to retrieve outline data with the requested <paramref name="format"/>.
  8.    ''' </summary>
  9.    '''
  10.    ''' <param name="font">
  11.    ''' The <see cref="System.Drawing.Font"/> object from which the glyph will be obtained.
  12.    ''' </param>
  13.    '''
  14.    ''' <param name="ch">
  15.    ''' The character whose glyph outline will be requested.
  16.    ''' </param>
  17.    '''
  18.    ''' <param name="format">
  19.    ''' The format in which the glyph outline will be retrieved.
  20.    ''' <para></para>
  21.    ''' This value only can be <see cref="GetGlyphOutlineFormat.Native"/> or <see cref="GetGlyphOutlineFormat.Bezier"/>.
  22.    ''' <para></para>
  23.    ''' Note: callers must interpret the returned byte array based on the selected format.
  24.    ''' </param>
  25.    '''
  26.    ''' <param name="matrix">
  27.    ''' An optional <see cref="GlyphOutlineMatrix2"/> used to transform the glyph outline.
  28.    ''' <para></para>
  29.    ''' If no value is provided or default structure is passed, an identity matrix
  30.    ''' will be used (see: <see cref="GlyphOutlineMatrix2.GetIdentityMatrix()"/>),
  31.    ''' where the transfromed graphical object is identical to the source object.
  32.    ''' </param>
  33.    '''
  34.    ''' <returns>
  35.    ''' A <see cref="Byte"/> array containing the raw glyph outline data with the requested <paramref name="format"/>.
  36.    ''' <para></para>
  37.    ''' Returns <see langword="Nothing"/> if the glyph is empty in the specified <paramref name="font"/>.
  38.    ''' </returns>
  39.    '''
  40.    ''' <exception cref="ArgumentNullException">
  41.    ''' Thrown when <paramref name="font"/> is <see langword="Nothing"/>.
  42.    ''' </exception>
  43.    '''
  44.    ''' <exception cref="ArgumentException">
  45.    ''' Thrown when the specified <paramref name="format"/> is invalid to request glyph outline data.
  46.    ''' </exception>
  47.    '''
  48.    ''' <exception cref="System.ComponentModel.Win32Exception">
  49.    ''' Thrown when a Win32 error occurs during font or device context operations.
  50.    ''' </exception>
  51.    <Extension>
  52.    <EditorBrowsable(EditorBrowsableState.Always)>
  53.    <DebuggerStepThrough>
  54.    Public Function GetGlyphOutlineData(font As Font, ch As Char, format As GetGlyphOutlineFormat,
  55.                                        Optional matrix As GlyphOutlineMatrix2 = Nothing) As Byte()
  56.  
  57.        If font Is Nothing Then
  58.            Throw New ArgumentNullException(paramName:=NameOf(font))
  59.        End If
  60.  
  61.        If format <> GetGlyphOutlineFormat.Native AndAlso
  62.           format <> GetGlyphOutlineFormat.Bezier Then
  63.  
  64.            Dim msg As String = $"The specified format '{format}' does not produce glyph outline data. " & Environment.NewLine &
  65.                                $"Use '{NameOf(GetGlyphOutlineFormat.Native)}' or '{NameOf(GetGlyphOutlineFormat.Bezier)}' " &
  66.                                "formats to request glyph outline data."
  67.  
  68.            Throw New ArgumentException(msg, paramName:=NameOf(format))
  69.        End If
  70.  
  71.        Dim hdc As IntPtr
  72.        Dim hFont As IntPtr
  73.        Dim oldObj As IntPtr
  74.  
  75.        Dim win32Err As Integer
  76.  
  77.        Try
  78.            hFont = font.ToHfont()
  79.            hdc = NativeMethods.CreateCompatibleDC(IntPtr.Zero)
  80.            oldObj = NativeMethods.SelectObject(hdc, hFont)
  81.            win32Err = Marshal.GetLastWin32Error()
  82.            If oldObj = IntPtr.Zero OrElse oldObj = DevCase.Win32.Common.Constants.HGDI_ERROR Then
  83.                Throw New Win32Exception(win32Err)
  84.            End If
  85.  
  86.            Dim chCode As UInteger = CUInt(Convert.ToInt32(ch))
  87.            If matrix.Equals(New GlyphOutlineMatrix2()) Then
  88.                matrix = GlyphOutlineMatrix2.GetIdentityMatrix()
  89.            End If
  90.  
  91.            Dim needed As UInteger = NativeMethods.GetGlyphOutline(hdc, chCode, format, Nothing, Nothing, Nothing, matrix)
  92.  
  93.            win32Err = Marshal.GetLastWin32Error()
  94.  
  95.            Select Case needed
  96.                Case 0UI
  97.                    ' Zero curve data points were returned, meaning the glyph is empty.
  98.                    Return Nothing
  99.  
  100.                Case DevCase.Win32.Common.Constants.GDI_ERROR
  101.                    If win32Err = Win32ErrorCode.ERROR_SUCCESS Then
  102.                        ' The function returned GDI_ERROR, but no error recorded by GetLastError, meaning the function succeeded.
  103.                        ' Tests carried out have shown that when this happens the glyph simply does not exists.
  104.                        Return Nothing
  105.                    Else
  106.                        Throw New Win32Exception(win32Err)
  107.                    End If
  108.  
  109.                Case Else
  110.                    Dim bufferPtr As IntPtr = Marshal.AllocHGlobal(New IntPtr(needed))
  111.                    Try
  112.                        Dim got As UInteger = NativeMethods.GetGlyphOutline(hdc, chCode, format, Nothing, needed, bufferPtr, matrix)
  113.                        win32Err = Marshal.GetLastWin32Error()
  114.                        If got = DevCase.Win32.Common.Constants.GDI_ERROR AndAlso
  115.                           win32Err <> Win32ErrorCode.ERROR_SUCCESS Then
  116.                            Throw New Win32Exception(win32Err)
  117.                        End If
  118.  
  119.                        Dim result(CInt(got) - 1) As Byte
  120.                        Marshal.Copy(bufferPtr, result, 0, CInt(got))
  121.                        Return result
  122.                    Finally
  123.                        Marshal.FreeHGlobal(bufferPtr)
  124.                    End Try
  125.  
  126.            End Select
  127.  
  128.        Finally
  129.            If hFont <> IntPtr.Zero Then
  130.                NativeMethods.DeleteObject(hFont)
  131.            End If
  132.            If oldObj <> IntPtr.Zero Then
  133.                NativeMethods.DeleteObject(oldObj)
  134.            End If
  135.            If hdc <> IntPtr.Zero Then
  136.                NativeMethods.DeleteDC(hdc)
  137.            End If
  138.  
  139.        End Try
  140.  
  141.    End Function
  142.  
  143.    ''' <summary>
  144.    ''' Determines whether the glyph outline for the specified character in the source <see cref="System.Drawing.Font"/>
  145.    ''' is identical to the glyph outline of the same character in another <see cref="System.Drawing.Font"/>.
  146.    ''' </summary>
  147.    '''
  148.    ''' <param name="firstFont">
  149.    ''' The first <see cref="System.Drawing.Font"/> to compare.
  150.    ''' </param>
  151.    '''
  152.    ''' <param name="secondFont">
  153.    ''' The second <see cref="System.Drawing.Font"/> to compare.
  154.    ''' </param>
  155.    '''
  156.    ''' <param name="ch">
  157.    ''' The character whose glyph outline will be compared between the two fonts.
  158.    ''' </param>
  159.    '''
  160.    ''' <returns>
  161.    ''' <see langword="True"/> if both fonts produce identical outlines for the specified glyph.
  162.    ''' <para></para>
  163.    ''' <see langword="False"/> if the outlines differ or if one of the fonts has an empty glyph.
  164.    ''' If the glyph outlines are empty in both fonts, returns <see langword="True"/>.
  165.    ''' </returns>
  166.    <Extension>
  167.    <EditorBrowsable(EditorBrowsableState.Always)>
  168.    <DebuggerStepThrough>
  169.    Public Function GlyphOutlinesAreEqual(firstFont As Font, secondFont As Font, ch As Char) As Boolean
  170.  
  171.        Dim firstBytes As Byte() = FontExtensions.GetGlyphOutlineData(firstFont, ch, GetGlyphOutlineFormat.Native)
  172.        Dim secondBytes As Byte() = FontExtensions.GetGlyphOutlineData(secondFont, ch, GetGlyphOutlineFormat.Native)
  173.  
  174.        Return (firstBytes Is Nothing AndAlso secondBytes Is Nothing) OrElse
  175.               (
  176.                 (firstBytes Is Nothing = (secondBytes Is Nothing)) AndAlso
  177.                  firstBytes.SequenceEqual(secondBytes)
  178.               )
  179.    End Function
  180.  
  181.    ''' <summary>
  182.    ''' Computes a similarity score between the glyph outline for the
  183.    ''' specified character in the source <see cref="System.Drawing.Font"/>,
  184.    ''' and the the glyph outline of the same character in another <see cref="System.Drawing.Font"/>.
  185.    ''' </summary>
  186.    '''
  187.    ''' <param name="firstFont">
  188.    ''' The first <see cref="System.Drawing.Font"/> to compare.
  189.    ''' </param>
  190.    '''
  191.    ''' <param name="secondFont">
  192.    ''' The second <see cref="System.Drawing.Font"/> to compare.
  193.    ''' </param>
  194.    '''
  195.    ''' <param name="ch">
  196.    ''' The character whose glyph outlines will be compared between the two fonts.
  197.    ''' </param>
  198.    '''
  199.    ''' <returns>
  200.    ''' A <see cref="Single"/> value between 0.0 and 1.0 representing the similarity
  201.    ''' (the number of matching bytes in the outline data) of the glyph outlines.
  202.    ''' <para></para>
  203.    ''' If one of the fonts has an empty glyph, returns 0. If the glyph outlines are empty in both fonts, returns 1.
  204.    ''' </returns>
  205.    <Extension>
  206.    <EditorBrowsable(EditorBrowsableState.Always)>
  207.    <DebuggerStepThrough>
  208.    Public Function GetGlyphOutlineSimilarity(firstFont As Font, secondFont As Font, ch As Char) As Single
  209.  
  210.        Dim firstBytes As Byte() = FontExtensions.GetGlyphOutlineData(firstFont, ch, GetGlyphOutlineFormat.Native)
  211.        Dim secondBytes As Byte() = FontExtensions.GetGlyphOutlineData(secondFont, ch, GetGlyphOutlineFormat.Native)
  212.  
  213.        If firstBytes Is Nothing AndAlso secondBytes Is Nothing Then
  214.            Return 1.0F
  215.        End If
  216.  
  217.        If (firstBytes Is Nothing) <> (secondBytes Is Nothing) Then
  218.            Return 0.0F
  219.        End If
  220.  
  221.        Dim maxLength As Integer = System.Math.Max(firstBytes.Length, secondBytes.Length)
  222.        Dim minLength As Integer = System.Math.Min(firstBytes.Length, secondBytes.Length)
  223.        Dim equalCount As Integer = 0
  224.  
  225.        For i As Integer = 0 To minLength - 1
  226.            If firstBytes(i) = secondBytes(i) Then
  227.                equalCount += 1
  228.            End If
  229.        Next
  230.  
  231.        Return CSng(equalCount) / maxLength
  232.    End Function
  233.  
  234. End Module
37  Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets) en: 2 Septiembre 2025, 10:12 am
Métodos universales para trabajar (otros) aspectos básicos con fuentes de texto (.ttf y .otf)...

(AL FINAL DE ESTE POST HE COMPARTIDO UN EJEMPLO DE USO 😏)

Funciones 'UtilFonts.FontHasGlyph', 'UtilFonts.FontHasGlyphs', 'FontExtensions.HasGlyph' y 'FontExtensions.HasGlyphs'

    Sirven para determinar si existen glifos en una fuente de texto para un caracter o una serie de caracteres específicos.

    Se utilizaría, por ejemplo, con este tipo de fuente que no tiene glifos propios para las vocales con tilde:

   

Funciones 'UtilFonts.FontGlyphHasOutline' y 'FontExtensions.GlyphHasOutline'

    Sirven para determinar si un glifo está vacío (no hay contornos dibujados).

    Se utilizaría, por ejemplo, con este tipo de fuentes que no dibujan las vocales con tilde:

   

    Tener en cuenta que esta función solo sirve para determinar si el glifo contiene algo,
    no puede determinar si el glifo es una figura incompleta como por ejemplo la de esta vocal que solo tiene la tilde:

   



El código fuente

Imports necesarios

Código
  1. Imports System.ComponentModel
  2. Imports System.Drawing
  3. Imports System.Drawing.Text
  4. Imports System.IO
  5. Imports System.Runtime.CompilerServices
  6. Imports System.Runtime.InteropServices
  7.  
  8. Imports DevCase.Win32
  9. Imports DevCase.Win32.Enums
  10. Imports DevCase.Win32.Structures

Clases secundarias requeridas

(Lo siento pero he tenido que borrar mucha documentación XML -no esencial- para que me quepa todo el código en este post.)

Código
  1. #Region " Constants "
  2.  
  3. Namespace DevCase.Win32.Common.Constants
  4.  
  5.    <HideModuleName>
  6.    Friend Module Constants
  7.  
  8. #Region " GDI32 "
  9.  
  10.    ''' <summary>
  11.    ''' Error return value for some GDI32 functions.
  12.    ''' </summary>
  13.    Public Const GDI_ERROR As UInteger = &HFFFFFFFFUI
  14.  
  15.    ''' <summary>
  16.    ''' Error return value for some GDI32 functions.
  17.    ''' </summary>
  18.    Public ReadOnly HGDI_ERROR As New IntPtr(-1)
  19.  
  20. #End Region
  21.  
  22.    End Module
  23.  
  24. End Namespace
  25.  
  26. #End Region

Código
  1. #Region " Enums "
  2.  
  3. Namespace DevCase.Win32.Enums
  4.  
  5.    ''' <remarks>
  6.    ''' List of System Error Codes: <see href="https://docs.microsoft.com/en-us/windows/desktop/Debug/system-error-codes"/>.
  7.    ''' </remarks>
  8.    Public Enum Win32ErrorCode As Integer
  9.  
  10.        ''' <summary>
  11.        ''' The operation completed successfully.
  12.        ''' </summary>
  13.        ERROR_SUCCESS = &H0
  14.    End Enum
  15.  
  16.    ''' <remarks>
  17.    ''' <see href="https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-wcrange"/>
  18.    ''' </remarks>
  19.    <Flags>
  20.    Public Enum GetGlyphIndicesFlags ' GGI
  21.  
  22.        ''' <summary>
  23.        ''' Marks unsupported glyphs with the hexadecimal value 0xFFFF.
  24.        ''' </summary>
  25.        MarkNonExistingGlyphs = 1 ' GGI_MARK_NONEXISTING_GLYPHS
  26.    End Enum
  27.  
  28.    ''' <remarks>
  29.    ''' <see href="https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getglyphoutlinew"/>
  30.    ''' </remarks>
  31.    Public Enum GetGlyphOutlineFormat ' GGO
  32.        Metrics = 0
  33.        Bitmap = 1
  34.  
  35.        ''' <summary>
  36.        ''' The function retrieves the curve data points in the rasterizer's native format and uses the font's design units.
  37.        ''' </summary>
  38.        Native = 2
  39.  
  40.        Bezier = 3
  41.        BitmapGray2 = 4
  42.        BitmapGray4 = 5
  43.        BitmapGray8 = 6
  44.        GlyphIndex = &H80
  45.        Unhinted = &H100
  46.    End Enum
  47.  
  48. End Namespace
  49.  
  50. #End Region

Código
  1. #Region " Structures "
  2.  
  3.    Namespace DevCase.Win32.Structures
  4.  
  5.    #Region " GlyphMetrics "
  6.  
  7.        ''' <remarks>
  8.        ''' <see href="https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-glyphmetrics"/>
  9.        ''' </remarks>
  10.        <StructLayout(LayoutKind.Sequential)>
  11.        Public Structure GlyphMetrics
  12.            Public BlackBoxX As UInteger
  13.            Public BlackBoxY As UInteger
  14.            Public GlyphOrigin As NativePoint
  15.            Public CellIncX As Short
  16.            Public CellIncY As Short
  17.        End Structure
  18.  
  19.    #End Region
  20.  
  21.    #Region " NativePoint (POINT) "
  22.  
  23.    ''' <summary>
  24.    ''' Defines the x- and y- coordinates of a point.
  25.    ''' </summary>
  26.    '''
  27.    ''' <remarks>
  28.    ''' <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd162805%28v=vs.85%29.aspx"/>
  29.    ''' </remarks>
  30.    <DebuggerStepThrough>
  31.    <StructLayout(LayoutKind.Sequential)>
  32.    Public Structure NativePoint
  33.  
  34. #Region " Fields "
  35.  
  36.        Public X As Integer
  37.        Public Y As Integer
  38.  
  39. #End Region
  40.  
  41. #Region " Constructors "
  42.  
  43.        Public Sub New(x As Integer, y As Integer)
  44.            Me.X = x
  45.            Me.Y = y
  46.        End Sub
  47.  
  48.        Public Sub New(pt As Point)
  49.            Me.New(pt.X, pt.Y)
  50.        End Sub
  51.  
  52. #End Region
  53.  
  54. #Region " Operator Conversions "
  55.  
  56.        Public Shared Widening Operator CType(pt As NativePoint) As Point
  57.            Return New Point(pt.X, pt.Y)
  58.        End Operator
  59.  
  60.        Public Shared Widening Operator CType(pt As Point) As NativePoint
  61.            Return New NativePoint(pt.X, pt.Y)
  62.        End Operator
  63.  
  64. #End Region
  65.  
  66.    End Structure
  67.  
  68.    #End Region
  69.  
  70.    #Region " GlyphOutlineMatrix2 "
  71.  
  72.    ''' <remarks>
  73.    ''' <see href="https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-mat2"/>
  74.    ''' </remarks>
  75.    <StructLayout(LayoutKind.Sequential)>
  76.    Public Structure GlyphOutlineMatrix2 ' MAT2
  77.  
  78.        Public M11 As Fixed
  79.        Public M12 As Fixed
  80.        Public M21 As Fixed
  81.        Public M22 As Fixed
  82.  
  83.        ''' <summary>
  84.        ''' Gets an <see cref="GlyphOutlineMatrix2"/> transformation in which the transformed graphical object is identical to the source object.
  85.        ''' This is called an identity matrix.
  86.        ''' <para></para>
  87.        ''' In this identity matrix,
  88.        ''' the value of <see cref="GlyphOutlineMatrix2.M11"/> is 1,
  89.        ''' the value of <see cref="GlyphOutlineMatrix2.M12"/> is zero,
  90.        ''' the value of <see cref="GlyphOutlineMatrix2.M21"/> is zero,
  91.        ''' and the value of <see cref="GlyphOutlineMatrix2.M22"/> is 1.
  92.        ''' </summary>
  93.        '''
  94.        ''' <returns>
  95.        ''' The resulting <see cref="GlyphOutlineMatrix2"/>.
  96.        ''' </returns>
  97.        Public Shared Function GetIdentityMatrix() As GlyphOutlineMatrix2
  98.            Return New GlyphOutlineMatrix2() With {
  99.            .M11 = New Fixed With {.Value = 1},
  100.            .M22 = New Fixed With {.Value = 1}
  101.        }
  102.        End Function
  103.  
  104.    End Structure
  105.  
  106.    #End Region
  107.  
  108.    #Region " Fixed "
  109.  
  110.    ''' <summary>
  111.    ''' Contains the integral and fractional parts of a fixed-point real number.
  112.    ''' <para></para>
  113.    ''' Note: The <see cref="Fixed"/> structure is used to describe the elements of the <see cref="GlyphOutlineMatrix2"/> structure.
  114.    ''' </summary>
  115.    '''
  116.    ''' <remarks>
  117.    ''' <see href="https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-fixed"/>
  118.    ''' </remarks>
  119.    <StructLayout(LayoutKind.Sequential)>
  120.    Public Structure Fixed
  121.  
  122. #Region " Public Fields "
  123.  
  124.        ''' <summary>
  125.        ''' The fractional value.
  126.        ''' </summary>
  127.        Public Fraction As UShort
  128.  
  129.        ''' <summary>
  130.        ''' The integral value.
  131.        ''' </summary>
  132.        Public Value As Short
  133.  
  134. #End Region
  135.  
  136. #Region " Operator Conversions "
  137.  
  138.        Public Shared Widening Operator CType(f As Fixed) As Decimal
  139.  
  140.            Return Decimal.Parse($"{f.Value.ToString(NumberFormatInfo.InvariantInfo)}{NumberFormatInfo.InvariantInfo.NumberDecimalSeparator}{f.Fraction.ToString(NumberFormatInfo.InvariantInfo)}", NumberFormatInfo.InvariantInfo)
  141.        End Operator
  142.  
  143.        Public Shared Widening Operator CType(dec As Decimal) As Fixed
  144.  
  145.            Return New Fixed With {
  146.                .Value = CShort(System.Math.Truncate(System.Math.Truncate(dec))),
  147.                .Fraction = UShort.Parse(dec.ToString(NumberFormatInfo.InvariantInfo).Split({NumberFormatInfo.InvariantInfo.NumberDecimalSeparator}, StringSplitOptions.None)(1), NumberFormatInfo.InvariantInfo)
  148.            }
  149.        End Operator
  150.  
  151. #End Region
  152.  
  153. #Region " Public Methods "
  154.  
  155.        Public Overrides Function ToString() As String
  156.  
  157.            Return CDec(Me).ToString()
  158.        End Function
  159.  
  160. #End Region
  161.  
  162.    End Structure
  163.  
  164.    #End Region
  165.  
  166.    End Namespace
  167.  
  168. #End Region
  169.  

Código
  1. #Region " NativeMethods "
  2.  
  3. Namespace DevCase.Win32.NativeMethods
  4.  
  5.    <SuppressUnmanagedCodeSecurity>
  6.    Friend Module Gdi32
  7.  
  8.        ''' <summary>
  9.        ''' Creates a memory device context (DC) compatible with the specified device.
  10.        ''' </summary>
  11.        '''
  12.        ''' <remarks>
  13.        ''' <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd183489%28v=vs.85%29.aspx"/>
  14.        ''' </remarks>
  15.        <DllImport("gdi32.dll", SetLastError:=True)>
  16.        Public Function CreateCompatibleDC(hdc As IntPtr
  17.        ) As IntPtr
  18.        End Function
  19.  
  20.        ''' <summary>
  21.        ''' Deletes the specified device context (DC).
  22.        ''' <para></para>
  23.        ''' An application must not delete a DC whose handle was obtained by calling the <see cref="GetDC"/> function.
  24.        ''' instead, it must call the <see cref="ReleaseDC"/> function to free the DC.
  25.        ''' </summary>
  26.        '''
  27.        ''' <remarks>
  28.        ''' <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd183533%28v=vs.85%29.aspx"/>
  29.        ''' </remarks>
  30.        <DllImport("gdi32.dll")>
  31.        Public Function DeleteDC(hdc As IntPtr
  32.        ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  33.        End Function
  34.  
  35.        ''' <summary>
  36.        ''' Selects an object into a specified device context.
  37.        ''' <para></para>
  38.        ''' The new object replaces the previous object of the same type.
  39.        ''' </summary>
  40.        '''
  41.        ''' <remarks>
  42.        ''' <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd162957%28v=vs.85%29.aspx"/>
  43.        ''' </remarks>
  44.        <DllImport("gdi32.dll", ExactSpelling:=False)>
  45.        Public Function SelectObject(hdc As IntPtr,
  46.                                     hObject As IntPtr
  47.        ) As IntPtr
  48.        End Function
  49.  
  50.        ''' <summary>
  51.        ''' Deletes a logical pen, brush, font, bitmap, region, or palette,
  52.        ''' freeing all system resources associated with the object.
  53.        ''' <para></para>
  54.        ''' After the object is deleted, the specified handle is no longer valid.
  55.        ''' <para></para>
  56.        ''' Do not delete a drawing object (pen or brush) while it is still selected into a DC.
  57.        ''' <para></para>
  58.        ''' When a pattern brush is deleted, the bitmap associated with the brush is not deleted.
  59.        ''' The bitmap must be deleted independently.
  60.        ''' </summary>
  61.        '''
  62.        ''' <remarks>
  63.        ''' <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms633540%28v=vs.85%29.aspx"/>
  64.        ''' </remarks>
  65.        <DllImport("gdi32.dll", ExactSpelling:=False, SetLastError:=True)>
  66.        Public Function DeleteObject(hObject As IntPtr
  67.        ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  68.        End Function
  69.  
  70.        ''' <summary>
  71.        ''' Translates a string into an array of glyph indices.
  72.        ''' <para></para>
  73.        ''' The function can be used to determine whether a glyph exists in a font.
  74.        ''' </summary>
  75.        '''
  76.        ''' <remarks>
  77.        ''' <see href="https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getglyphindicesw"/>
  78.        ''' </remarks>
  79.        <DllImport("gdi32.dll", SetLastError:=False, CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
  80.        Public Function GetGlyphIndices(hdc As IntPtr,
  81.                                        str As String,
  82.                                        strLen As Integer,
  83.                                        <[Out], MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=2)>
  84.                                        glyphIndices As UShort(),
  85.                               Optional flags As GetGlyphIndicesFlags = GetGlyphIndicesFlags.MarkNonExistingGlyphs
  86.        ) As UInteger
  87.        End Function
  88.  
  89.        ''' <summary>
  90.        ''' Retrieves the outline or bitmap for a character in the TrueType font that is selected into the specified device context.
  91.        ''' </summary>
  92.        '''
  93.        ''' <remarks>
  94.        ''' <see href="https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getglyphoutlinew"/>
  95.        ''' </remarks>
  96.        <DllImport("gdi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
  97.        Public Function GetGlyphOutline(hdc As IntPtr,
  98.                                        ch As UInteger,
  99.                                        format As GetGlyphOutlineFormat,
  100.                            <Out> ByRef refMetrics As GlyphMetrics,
  101.                                        bufferSize As UInteger,
  102.                                        buffer As IntPtr,
  103.                                  ByRef refMatrix2 As GlyphOutlineMatrix2
  104.        ) As UInteger
  105.        End Function
  106.  
  107.    End Module
  108.  
  109. End Namespace
  110.  
  111. #End Region

Clase principal 'UtilFonts' y modulo 'FontExtensions', que contienen los métodos universales en torno a fuentes de texto

Código
  1. Public Class UtilFonts
  2.  
  3.    ''' <summary>
  4.    ''' Prevents a default instance of the <see cref="UtilFonts"/> class from being created.
  5.    ''' </summary>
  6.    Private Sub New()
  7.    End Sub
  8.  
  9.    ''' <summary>
  10.    ''' Determines whether a glyph exists in the given font file
  11.    ''' for the specified character.
  12.    ''' </summary>
  13.    '''
  14.    ''' <param name="fontFile">
  15.    ''' Path to the font file used to check for glyph availability.
  16.    ''' </param>
  17.    '''
  18.    ''' <param name="ch">
  19.    ''' The character that represents the glyph to check.
  20.    ''' </param>
  21.    '''
  22.    ''' <returns>
  23.    ''' <see langword="True"/> if a glyph exists in the font for the specified character;
  24.    ''' otherwise, <see langword="False"/>.
  25.    ''' </returns>
  26.    <DebuggerStepThrough>
  27.    Public Shared Function FontHasGlyph(fontFile As String, ch As Char) As Boolean
  28.  
  29.        Return UtilFonts.FontHasGlyphs(fontFile, ch) = 1
  30.    End Function
  31.  
  32.    ''' <summary>
  33.    ''' Determines whether a glyph exists in the given font file
  34.    ''' for all the characters in the speciied string.
  35.    ''' </summary>
  36.    '''
  37.    ''' <param name="fontFile">
  38.    ''' Path to the font file used to check for glyphs availability.
  39.    ''' </param>
  40.    '''
  41.    ''' <param name="str">
  42.    ''' A <see cref="String"/> with the character(s) that represents the glyphs to check.
  43.    ''' <para></para>
  44.    ''' Each character (or surrogate pair) is checked for a existing glyph in the font.
  45.    ''' </param>
  46.    '''
  47.    ''' <returns>
  48.    ''' The count of characters from <paramref name="str"/> parameter that have a existing glyph in the font.
  49.    ''' <para></para>
  50.    ''' A count less than the length of <paramref name="str"/> indicates that the font does not have a existing glyph for one or more characters.
  51.    ''' </returns>
  52.    '''
  53.    ''' <exception cref="FileNotFoundException">
  54.    ''' Thrown when the font file is not found.
  55.    ''' </exception>
  56.    <DebuggerStepThrough>
  57.    Public Shared Function FontHasGlyphs(fontFile As String, str As String) As UInteger
  58.  
  59.        If Not System.IO.File.Exists(fontFile) Then
  60.            Throw New FileNotFoundException("Font file not found.", fileName:=fontFile)
  61.        End If
  62.  
  63.        Using pfc As New PrivateFontCollection()
  64.            pfc.AddFontFile(fontFile)
  65.  
  66.            Using f As New Font(pfc.Families(0), emSize:=1)
  67.                Return FontExtensions.HasGlyphs(f, str)
  68.            End Using
  69.        End Using
  70.    End Function
  71.  
  72.    ''' <summary>
  73.    ''' Determines whether a glyph for the specified character in the given font file has an outline.
  74.    ''' <para></para>
  75.    ''' This is useful to determine whether the glyph is empty (no character is drawn),
  76.    ''' but note that a glyph with outlines does not necessarily mean that the character is fully represented.
  77.    ''' Some fonts, for instance, only renders diacritical marks for accented vowels
  78.    ''' instead the full letter (e.g., "<b>´</b>" instead of "<b>í</b>").
  79.    ''' This function solely determines whether the glyph draws an outline, nothing more.
  80.    ''' <para></para>
  81.    ''' To determine whether a glyph exists in the given font file for the specified character, use
  82.    ''' <see cref="UtilFonts.FontHasGlyph"/> or <see cref="UtilFonts.FontHasGlyphs"/> instead.
  83.    ''' </summary>
  84.    '''
  85.    ''' <param name="fontFile">
  86.    ''' Path to the font file used to check for glyph availability.
  87.    ''' </param>
  88.    '''
  89.    ''' <param name="ch">
  90.    ''' The character that represents the glyph to check in the font.
  91.    ''' </param>
  92.    '''
  93.    ''' <returns>
  94.    ''' Returns <see langword="True"/> if the glyph has an outline (visible shape data exists).
  95.    ''' <para></para>
  96.    ''' Returns <see langword="False"/> if the glyph does not have an outline,
  97.    ''' meaning the glyph is empty/unsupported by the font.
  98.    ''' </returns>
  99.    '''
  100.    ''' <exception cref="FileNotFoundException">
  101.    ''' Thrown when the font file is not found.
  102.    ''' </exception>
  103.    <DebuggerStepThrough>
  104.    Public Shared Function FontGlyphHasOutline(fontFile As String, ch As Char) As Boolean
  105.  
  106.        If Not System.IO.File.Exists(fontFile) Then
  107.            Throw New FileNotFoundException("Font file not found.", fileName:=fontFile)
  108.        End If
  109.  
  110.        Using pfc As New PrivateFontCollection()
  111.            pfc.AddFontFile(fontFile)
  112.  
  113.            Using f As New Font(pfc.Families(0), emSize:=1)
  114.                Return FontExtensions.GlyphHasOutline(f, ch)
  115.            End Using
  116.        End Using
  117.    End Function
  118.  
  119. End Class

Código
  1. Module FontExtensions
  2.  
  3.    ''' <summary>
  4.    ''' Determines whether a glyph exists in the given <see cref="System.Drawing.Font"/>
  5.    ''' for the specified character.
  6.    ''' </summary>
  7.    '''
  8.    ''' <param name="font">
  9.    ''' The <see cref="System.Drawing.Font"/> used to check for glyph availability.
  10.    ''' </param>
  11.    '''
  12.    ''' <param name="ch">
  13.    ''' The character that represents the glyph to check.
  14.    ''' </param>
  15.    '''
  16.    ''' <returns>
  17.    ''' <see langword="True"/> if a glyph exists in the font for the specified character;
  18.    ''' otherwise, <see langword="False"/>.
  19.    ''' </returns>
  20.    <Extension>
  21.    <EditorBrowsable(EditorBrowsableState.Always)>
  22.    <DebuggerStepThrough>
  23.    Public Function HasGlyph(font As Font, ch As Char) As Boolean
  24.  
  25.        Return FontExtensions.HasGlyphs(font, ch) = 1
  26.    End Function
  27.  
  28.    ''' <summary>
  29.    ''' Determines whether a glyph exists in the given <see cref="System.Drawing.Font"/>
  30.    ''' for all the characters in the speciied string.
  31.    ''' </summary>
  32.    '''
  33.    ''' <param name="font">
  34.    ''' The <see cref="System.Drawing.Font"/> used to check for glyphs availability.
  35.    ''' </param>
  36.    '''
  37.    ''' <param name="str">
  38.    ''' A <see cref="String"/> with the character(s) that represents the glyphs to check.
  39.    ''' <para></para>
  40.    ''' Each character (or surrogate pair) is checked for a existing glyph in the font.
  41.    ''' </param>
  42.    '''
  43.    ''' <returns>
  44.    ''' The count of characters from <paramref name="str"/> parameter that have a existing glyph in the font.
  45.    ''' <para></para>
  46.    ''' A count less than the length of <paramref name="str"/> indicates that the font does not have a existing glyph for one or more characters.
  47.    ''' </returns>
  48.    '''
  49.    ''' <exception cref="ArgumentNullException">
  50.    ''' Thrown when <paramref name="font"/> or <paramref name="str"/> are null.
  51.    ''' </exception>
  52.    '''
  53.    ''' <exception cref="Win32Exception">
  54.    ''' Thrown when a call to Windows API GDI32 functions (creating device context, selecting font, or retrieving glyph indices) fails.
  55.    ''' </exception>
  56.    <Extension>
  57.    <EditorBrowsable(EditorBrowsableState.Always)>
  58.    <DebuggerStepThrough>
  59.    Public Function HasGlyphs(font As Font, str As String) As UInteger
  60.  
  61.        If font Is Nothing Then
  62.            Throw New ArgumentNullException(paramName:=NameOf(font))
  63.        End If
  64.  
  65.        If String.IsNullOrEmpty(str) Then
  66.            Throw New ArgumentNullException(paramName:=NameOf(str))
  67.        End If
  68.  
  69.        Dim hdc As IntPtr
  70.        Dim hFont As IntPtr
  71.        Dim oldObj As IntPtr
  72.  
  73.        Dim win32Err As Integer
  74.  
  75.        Try
  76.            hFont = font.ToHfont()
  77.            hdc = NativeMethods.CreateCompatibleDC(IntPtr.Zero)
  78.            win32Err = Marshal.GetLastWin32Error()
  79.            If hdc = IntPtr.Zero Then
  80.                Throw New Win32Exception(win32Err)
  81.            End If
  82.  
  83.            oldObj = NativeMethods.SelectObject(hdc, hFont)
  84.            win32Err = Marshal.GetLastWin32Error()
  85.            If oldObj = IntPtr.Zero OrElse oldObj = DevCase.Win32.Common.Constants.HGDI_ERROR Then
  86.                Throw New Win32Exception(win32Err)
  87.            End If
  88.  
  89.            ' Reserve output for each text unit (can be 1 or 2 chars if it's a surrogate pair).
  90.            Dim strLen As Integer = str.Length
  91.            Dim indices As UShort() = New UShort(strLen - 1) {}
  92.            ' Get the glyph indices for the string in the given device context.
  93.            Dim converted As UInteger = NativeMethods.GetGlyphIndices(hdc, str, strLen, indices, GetGlyphIndicesFlags.MarkNonExistingGlyphs)
  94.            win32Err = Marshal.GetLastWin32Error()
  95.            If converted = DevCase.Win32.Common.Constants.GDI_ERROR Then
  96.                Throw New Win32Exception(win32Err)
  97.            End If
  98.  
  99.            ' Count glyphs that exist (index <> 0xFFFF).
  100.            ' If any glyph index is 0xFFFF, the glyph does not exist in that font.
  101.            Dim count As UInteger
  102.            For Each index As UShort In indices
  103.                If index <> &HFFFFUS Then
  104.                    count += 1UI
  105.                End If
  106.            Next
  107.            Return count
  108.  
  109.        Finally
  110.            If oldObj <> IntPtr.Zero Then
  111.                NativeMethods.DeleteObject(oldObj)
  112.            End If
  113.            If hFont <> IntPtr.Zero Then
  114.                NativeMethods.DeleteObject(hFont)
  115.            End If
  116.            If hdc <> IntPtr.Zero Then
  117.                NativeMethods.DeleteDC(hdc)
  118.            End If
  119.  
  120.        End Try
  121.    End Function
  122.  
  123.  
  124.    ''' <summary>
  125.    ''' Determines whether a glyph for the specified character in the given <see cref="System.Drawing.Font"/> has an outline.
  126.    ''' <para></para>
  127.    ''' This is useful to determine whether the glyph is empty (no character is drawn),
  128.    ''' but note that a glyph with outlines does not necessarily mean that the character is fully represented.
  129.    ''' Some fonts, for instance, only renders diacritical marks for accented vowels
  130.    ''' instead the full letter (e.g., "<b>´</b>" instead of "<b>í</b>").
  131.    ''' This function solely determines whether the glyph draws an outline, nothing more.
  132.    ''' <para></para>
  133.    ''' To determine whether a glyph exists in the given font file for the specified character, use
  134.    ''' <see cref="FontExtensions.HasGlyph"/> or <see cref="FontExtensions.HasGlyphs"/> instead.
  135.    ''' </summary>
  136.    '''
  137.    ''' <param name="font">
  138.    ''' The <see cref="System.Drawing.Font"/> used to check for glyph availability.
  139.    ''' </param>
  140.    '''
  141.    ''' <param name="ch">
  142.    ''' The character that represents the glyph to check in the font.
  143.    ''' </param>
  144.    '''
  145.    ''' <returns>
  146.    ''' Returns <see langword="True"/> if the glyph has an outline (visible shape data exists).
  147.    ''' <para></para>
  148.    ''' Returns <see langword="False"/> if the glyph does not have an outline,
  149.    ''' meaning the glyph is empty/unsupported by the font.
  150.    ''' </returns>
  151.    <Extension>
  152.    <EditorBrowsable(EditorBrowsableState.Always)>
  153.    <DebuggerStepThrough>
  154.    Public Function GlyphHasOutline(font As Font, ch As Char) As Boolean
  155.  
  156.        If font Is Nothing Then
  157.            Throw New ArgumentNullException(paramName:=NameOf(font))
  158.        End If
  159.  
  160.        Dim hdc As IntPtr
  161.        Dim hFont As IntPtr
  162.        Dim oldObj As IntPtr
  163.  
  164.        Dim win32Err As Integer
  165.  
  166.        Try
  167.            hFont = font.ToHfont()
  168.            hdc = NativeMethods.CreateCompatibleDC(IntPtr.Zero)
  169.            oldObj = NativeMethods.SelectObject(hdc, hFont)
  170.            win32Err = Marshal.GetLastWin32Error()
  171.            If oldObj = IntPtr.Zero OrElse oldObj = DevCase.Win32.Common.Constants.HGDI_ERROR Then
  172.                Throw New Win32Exception(win32Err)
  173.            End If
  174.  
  175.            Dim chCode As UInteger = CUInt(Convert.ToInt32(ch))
  176.            Dim format As GetGlyphOutlineFormat = GetGlyphOutlineFormat.Native
  177.            Dim matrix As GlyphOutlineMatrix2 = GlyphOutlineMatrix2.GetIdentityMatrix()
  178.  
  179.            Dim ptCount As UInteger = NativeMethods.GetGlyphOutline(hdc, chCode, format, Nothing, Nothing, Nothing, matrix)
  180.            win32Err = Marshal.GetLastWin32Error()
  181.            Select Case ptCount
  182.  
  183.                Case 0UI
  184.                    ' Zero curve data points were returned, meaning the glyph is empty/invisible.
  185.                    Return False
  186.  
  187.                Case DevCase.Win32.Common.Constants.GDI_ERROR
  188.                    If win32Err = Win32ErrorCode.ERROR_SUCCESS Then
  189.                        ' The function returned GDI_ERROR, but no error recorded by GetLastError, meaning the function succeeded.
  190.                        ' Tests carried out have shown that when this happens the glyph simply does not exists.
  191.                        Return False
  192.                    Else
  193.                        Throw New Win32Exception(win32Err)
  194.                    End If
  195.  
  196.                Case Else
  197.                    Return True
  198.  
  199.            End Select
  200.  
  201.        Finally
  202.            If oldObj <> IntPtr.Zero Then
  203.                NativeMethods.DeleteObject(oldObj)
  204.            End If
  205.            If hFont <> IntPtr.Zero Then
  206.                NativeMethods.DeleteObject(hFont)
  207.            End If
  208.            If hdc <> IntPtr.Zero Then
  209.                NativeMethods.DeleteDC(hdc)
  210.            End If
  211.  
  212.        End Try
  213.  
  214.        ' ===================================================
  215.        '   ALTERNATIVE METHODOLOGY USING PURE MANAGED GDI+
  216.        '
  217.        ' (results are the same than using Windows API calls)
  218.        ' ===================================================
  219.        '
  220.        '
  221.        'If font Is Nothing Then
  222.        '    Throw New ArgumentNullException(paramName:=NameOf(font))
  223.        'End If
  224.        '
  225.        'If font.Unit = GraphicsUnit.Pixel AndAlso font.Size < 8 Then
  226.        '    Dim msg As String =
  227.        '        "Font size must be equals or greater than 8 pixels when using GraphicsUnit.Pixel to avoid unreliable pixel detection. " &
  228.        '        "Suggested font size is 16 pixel size; A value of 32, 64 or bigger pixel size would produce the same results."
  229.        '    Throw New ArgumentException(msg)
  230.        '
  231.        'ElseIf font.Size < 4 Then
  232.        '    Dim msg As String =
  233.        '        "Font size must be equals or greater than 4 to avoid unreliable pixel detection. " &
  234.        '        "Suggested usage is GraphicsUnit.Pixel with a font size of 16 pixels; " &
  235.        '        "A value of 32, 64 or bigger pixel size would produce the same results."
  236.        '    Throw New ArgumentException(msg)
  237.        '
  238.        'End If
  239.        '
  240.        '' Measure the required size for the glyph.
  241.        'Dim requiredSize As Size
  242.        'Using tempBmp As New Bitmap(1, 1)
  243.        '    Using g As Graphics = Graphics.FromImage(tempBmp)
  244.        '        Dim sizeF As SizeF = g.MeasureString(ch, font)
  245.        '        ' Add a small margin to avoid clipping due to rounding.
  246.        '        requiredSize = New Size(CInt(System.Math.Ceiling(sizeF.Width)) + 4,
  247.        '                                CInt(System.Math.Ceiling(sizeF.Height)) + 4)
  248.        '    End Using
  249.        'End Using
  250.        '
  251.        '' Create a bitmap big enough to render the glyph,
  252.        '' filling the bitmap background with white color and
  253.        '' drawing the character in black.
  254.        'Using bmp As New Bitmap(requiredSize.Width, requiredSize.Height),
  255.        '      g As Graphics = Graphics.FromImage(bmp)
  256.        '    ' Using AntiAlias may help ensure that very thin glyph strokes
  257.        '    ' still produce detectable pixels, with gray edges.
  258.        '    ' Without anti-aliasing, such strokes might render too faint or disappear entirely,
  259.        '    ' causing the glyph to be misidentified as empty.
  260.        '    g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
  261.        '    g.Clear(Color.White)
  262.        '    g.DrawString(ch, font, Brushes.Black, 0, 0)
  263.        '
  264.        '    Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
  265.        '    Dim bmpData As BitmapData = bmp.LockBits(rect, Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb)
  266.        '
  267.        '    Try
  268.        '        Dim ptr As IntPtr = bmpData.Scan0
  269.        '        Dim bytes As Integer = System.Math.Abs(bmpData.Stride) * bmp.Height
  270.        '        Dim pixelValues(bytes - 1) As Byte
  271.        '        Marshal.Copy(ptr, pixelValues, 0, bytes)
  272.        '
  273.        '        ' Iterate through each pixel.
  274.        '        ' PixelFormat.Format32bppArgb stores pixels as [Blue][Green][Red][Alpha]
  275.        '        ' i=Blue, i+1=Green, i+2=Red, i+3=Alpha
  276.        '        For i As Integer = 0 To pixelValues.Length - 1 Step 4
  277.        '            Dim red As Byte = pixelValues(i + 2)
  278.        '
  279.        '            ' Check if the pixel is darker than nearly-white (threshold 250)
  280.        '            ' If so, we found a visible pixel, meaning the glyph is drawn.
  281.        '            If red < 250 Then
  282.        '                Return True
  283.        '            End If
  284.        '        Next
  285.        '    Finally
  286.        '        bmp.UnlockBits(bmpData)
  287.        '
  288.        '    End Try
  289.        'End Using
  290.        '
  291.        '' No visible pixels found, meaning the glyph is empty/unsupported by the font.
  292.        'Return False
  293.  
  294.    End Function
  295.  
  296. End Module

Modo de empleo

El siguiente ejemplo verifica en los archivos de fuente .ttf de un directorio específico si la tipografía incluye los glifos correspondientes a los caracteres á, é, í, ó y ú. En caso de que falte algún glifo, se imprime un mensaje en consola indicando los glifos ausentes, y finalmente envía el archivo de fuente a la papelera de reciclaje (hay que descomentar las lineas marcadas).

Código
  1. Dim fontFiles As IEnumerable(Of String) = Directory.EnumerateFiles("C:\Fonts", "*.ttf", SearchOption.TopDirectoryOnly)
  2. Dim fontsToDelete As New HashSet(Of String)()
  3. Dim chars As Char() = "áéíóú".ToCharArray()
  4.  
  5. For Each fontFile As String In fontFiles
  6.    Dim missingChars As New HashSet(Of Char)()
  7.  
  8.    For Each ch As Char In chars
  9.        If Not UtilFonts.FontHasGlyph(fontFile, ch) OrElse
  10.           Not UtilFonts.FontGlyphHasOutline(fontFile, ch) Then
  11.            missingChars.Add(ch)
  12.        End If
  13.    Next
  14.  
  15.    If missingChars.Count > 0 Then
  16.        Console.WriteLine($"[{Path.GetFileName(fontFile)}] Missing glyphs: {String.Join(", ", missingChars)}")
  17.        fontsToDelete.Add(fontFile)
  18.    End If
  19. Next
  20.  
  21. For Each fontFile As String In fontsToDelete
  22.    ' Console.WriteLine($"Deleting font file: {fontFile}")
  23.    ' Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(fontFile, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
  24. Next

Por último, quiero comentar que he experimentado estas funciones de forma muy minuciosa, primero con muestras pequeñas de 2 o 3 fuentes... varias veces por cada cambio significativo realizado en el código, y después he probado la versión final con aprox. 14.000 archivos de fuentes de texto, y los resultados han sido muy satisfactorios detectando varios miles de fuentes a los que le faltan los glifos especificados, y, aunque no he podido revisar todos esos miles de fuentes una a una, no he encontrado ningún falso positivo entre varios cientos de fuentes que sí he revisado manualmente.

Eso es todo. 👋
38  Informática / Software / Re: Es posible descargar videos de Patreon? en: 1 Septiembre 2025, 12:41 pm
desde línea de comandos puedes descargarlos con yt-dlp

Hay interfaces gráficas de terceros:

https://github.com/kannagi0303/yt-dlp-gui
https://github.com/ErrorFlynn/ytdlp-interface

Atentamente,
Elektro.
39  Foros Generales / Foro Libre / Re: Viaje a la nueva Corea del Norte: ¿un paraíso turístico del comunismo? en: 31 Agosto 2025, 17:46 pm
Bueno para no desviar el tema seguimos hablando del gordi de corea del norte. :xD

Viendo la tecnología que tienen tan anticuada, que literalmente todo es una bonita fachada por fuera pero podredumbre por dentro (sería muy injusto decir que es lo mismo y tan evidente como en Myanmar, pero cierto parecido si que tiene: las apariencias engañan), yo me apostaría que sus misiles están tan vacíos como el cerebro de Kim Jong-un intentando comprender el funcionamiento de un iPhone. En serio, me viene a la cabeza un hombre de las cavernas acercando un palo al fuego por primera vez y quedándose pensativo durante varios minutos mientras se consume: "Uhmm… ¿que poder hacer yo ahora con palo de fuego?".

Y es cierto que han lanzado algunos misiles a modo de demostración, pero a ver... es que es un régimen de comunistas, no de tontos, el señor gordito –y que según las creencias del país no necesita hacer popó porque es prácticamente un dios– comprará algunos misiles en buenas condiciones para lanzarlos al mar y aparentar ser una amenaza real para el mundo, que debe ser respetada. Si no lanzasen misiles de vez en cuando para intentar intimidar, quizás ya no habría ninguna dictadura comunista en ese país ¿Me entiendes?, el país ya habría sido invadido rescatado de ese dictador, pero no por bondad de la humanidad, no por justicia ni ética, sino por intereses de unos u otros actores para poder colocar de líder/primer ministro/presidente/dictador a su lacayo más leal (como en Ucrania). En fin, el caso es que el resto de misiles... el resto deben tener más aire por dentro que una bolsa de papas Lay's. Mi opinión.

¡Un saludo!
40  Programación / .NET (C#, VB.NET, ASP) / Re: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets) en: 31 Agosto 2025, 15:20 pm
Esta función pertenece a la clase 'UtilFonts' del anterior post, lo comparto aquí por que no me cabe en el otro post y por que esta función no depende de ninguna otra...

Código
  1.   ''' <summary>
  2.   ''' Retrieves the resource name of a TrueType (.ttf) or OpenType font file (.otf)
  3.   ''' by creating a temporary scalable font resource file and reading its contents.
  4.   ''' <para></para>
  5.   ''' This name may differ from the value of the following properties:
  6.   ''' <list type="bullet">
  7.   '''   <item><description><see cref="System.Drawing.Font.Name"/>.</description></item>
  8.   '''   <item><description><see cref="System.Drawing.Font.OriginalFontName"/>.</description></item>
  9.   '''   <item><description><see cref="System.Drawing.Font.SystemFontName"/>.</description></item>
  10.   '''   <item><description><see cref="System.Windows.Media.GlyphTypeface.FamilyNames"/>.</description></item>
  11.   '''   <item><description><see cref="System.Windows.Media.GlyphTypeface.Win32FamilyNames"/>.</description></item>
  12.   ''' </list>
  13.   ''' </summary>
  14.   '''
  15.   ''' <param name="fontFile">
  16.   ''' The path to the font file (e.g., <b>"C:\font.ttf"</b>).
  17.   ''' </param>
  18.   '''
  19.   ''' <returns>
  20.   ''' The resource name of the given font file.
  21.   ''' </returns>
  22.   <DebuggerStepThrough>
  23.   Public Shared Function GetFontResourceName(fontFile As String) As String
  24.  
  25.       If Not File.Exists(fontFile) Then
  26.           Dim msg As String = $"The font file does not exist: '{fontFile}'"
  27.           Throw New FileNotFoundException(msg, fontFile)
  28.       End If
  29.  
  30.       Dim fontName As String = Nothing
  31.       Dim tempFile As String = Path.Combine(Path.GetTempPath(), "~FONT.RES")
  32.  
  33.       ' Ensure any previous existing temp file is deleted.
  34.       If File.Exists(tempFile) Then
  35.           Try
  36.               File.Delete(tempFile)
  37.           Catch ex As Exception
  38.               Dim msg As String = $"Cannot delete existing temp resource file: '{tempFile}'"
  39.               Throw New IOException(msg, ex)
  40.           End Try
  41.       End If
  42.  
  43.       ' Create a temporary scalable font resource.
  44.       Dim created As Boolean = NativeMethods.CreateScalableFontResource(1UI, tempFile, fontFile, Nothing)
  45.       If Not created Then
  46.           Dim msg As String = "Failed to create scalable font resource."
  47.           Throw New IOException(msg)
  48.       End If
  49.  
  50.       Try
  51.           ' Read the temp font file resource into a string.
  52.           Dim buffer As Byte() = File.ReadAllBytes(tempFile)
  53.           Dim bufferStr As String = Encoding.Default.GetString(buffer)
  54.  
  55.           ' Look for the "FONTRES:" marker.
  56.           Const fontResMarker As String = "FONTRES:"
  57.           Dim pos As Integer = bufferStr.IndexOf(fontResMarker)
  58.           If pos < 0 Then
  59.               Dim msg As String = "FONTRES marker not found in temporary font resource file."
  60.               Throw New InvalidOperationException(msg)
  61.           End If
  62.  
  63.           pos += fontResMarker.Length
  64.           Dim endPos As Integer = bufferStr.IndexOf(ControlChars.NullChar, pos)
  65.           If endPos < 0 Then
  66.               Dim msg As String = "Cannot determine the end position of the font name string in the font resource file content."
  67.               Throw New InvalidOperationException(msg)
  68.           End If
  69.  
  70.           fontName = bufferStr.Substring(pos, endPos - pos)
  71.       Catch ex As Exception
  72.           Throw
  73.  
  74.       Finally
  75.           ' Always attempt to delete the created temporary resource file.
  76.           Try
  77.               File.Delete(tempFile)
  78.           Catch
  79.               ' Ignore deletion exceptions; cleanup best effort.
  80.           End Try
  81.  
  82.       End Try
  83.  
  84.       Return fontName
  85.   End Function
  86.  

Código
  1. #Region " NativeMethods "
  2.  
  3. Namespace DevCase.Win32.NativeMethods
  4.  
  5.    <SuppressUnmanagedCodeSecurity>
  6.    Friend Module User32
  7.  
  8. #Region " GDI32.dll "
  9.  
  10.        <DllImport("GDI32.dll", CharSet:=CharSet.Auto, SetLastError:=True, BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
  11.        Friend Function CreateScalableFontResource(hidden As UInteger,
  12.                                                   resourceFile As String,
  13.                                                   fontFile As String,
  14.                                                   currentPath As String
  15.        ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  16.        End Function
  17.  
  18. #End Region
  19.  
  20.    End Module
  21.  
  22. End Namespace
  23.  
  24. #End Region

OFF-TOPIC

Si alguien se pregunta: "¿Y por qué esa obsesión con las diferentes formas que puede haber para obtener el nombre de una fuente?" "¿Qué más te da un nombre u otro?" pues bueno, por que yo necesitaba hallar la forma de obtener el nombre completo amistoso exactamente tal y como se muestra en el visor de fuentes de texto de Windows (fontview.exe), por que esa es la representación más completa y la más sofisticada que he visto hasta ahora, "¿Pero por qué motivo lo necesitas exactamente?" Pues por que se me metió en la cabeza conseguirlo, y yo soy muy cabezón, sin más, así que básicamente en eso ha consistido mi investigación, con varios días de ensayo y error, junto a treinta consultas a ChatGPT con sus cien respuestas inservibles que me sacan de quicio...

En el post anterior simplemente he recopilado las diferencias que he ido encontrando al probar diversas maneras de obtener el nombre de una fuente (a lo mejor me he olvidado de alguna otra forma, no sé). A penas hay información sobre esto en Internet (sobre como obtener el nombre amistoso COMPLETO) por no decir que prácticamente no hay nada de nada; aunque bueno, una forma sé que sería leyendo las tablas en la cabecera de un archivo de fuente, pero eso es un auténtico coñazo y propenso a errores humanos, sobre todo si no eres un friki erudito... diseñador de fuentes que conoce todos los entresijos y las "variables" a tener en cuenta al analizar la cabecera de estos formatos de archivo, cosa que evidentemente yo no conozco, pero por suerte al final descubrí que la propiedad "Title" de la shell de Windows es suficiente para lograr mi propósito a la perfección, y sin tener que recurrir a experimentos tediosos que me causarían pesadillas por la noche.

Lo de instalar y desinstalar fuentes vino a continuación de lo del nombre, primero necesitaba el nombre amistoso completo, y luego ya teniendo ese nombre -fiel a la representación de Microsoft Windows- podía empezar a desarrollar ideas para hacer cosas más útiles o interesantes. Todos los códigos que he visto por Internet en diferentes lenguajes de programación para instalar un archivo de fuente se quedan muuuy cortos para mis expectativas, carecíendo de las funcionalidades más esenciales, la optimización y los controles de errores más básicos... a diferencia de lo que yo he desarrollado y compartido en el anterior post, que aunque puede que no sea perfecto (por que la perfección absoluta no existe), es mejor que todo lo que he encontrado hasta ahora, y no es por echarme flores ni parecer engreído, pero es la verdad; Me siento sorprendido al no haber descubierto ningún otro programador que haya hecho/compartido un código universal para instalar fuentes de texto de forma más o menos eficiente, confiable y versátil. Quizás lo haya, pero yo no lo encontré. Códigos cortitos y que cumplen la funcionalidad mínima de "instalar una fuente" sin importar ningún factor, de esos hay muchos en Internet, pero como digo un BUEN CÓDIGO no encontré.

Lo próximo que comparta en este hilo puede que sea un método universal que sirva para determinar si un archivo de fuente contiene glifos para representar caracteres específicos (ej. "áéíóú"). Ya tengo algo hecho que funciona... pero no siempre funciona de la forma esperada (da falsos positivos con algunos archivos de fuente). Me falta mucho por aprender del formato TrueType y OpenType. Por suerte existen herramientas especializadas como por ejemplo "otfinfo.exe" (descarga) que sirven para obtener información general de una fuente, imprimir en consola los caracteres de un rango Unicode específico, volcar tablas completas y demás, y tener algo así me ayuda a hacer (y corregir) asunciones al leer este formato de archivo.

👋
Páginas: 1 2 3 [4] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... 1257
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines