Embedding source code in a source program II
- #If TARGET = "winexe" Then
- #If CONFIG = "Resource" AndAlso PLATFORM = "x64" Then
- Imports System.IO
- Imports System.Windows.Forms
- Imports System.Runtime.InteropServices
- Imports System.Text
- Imports Shell32
- Partial Public Class Find_Files_Class
- Private Sub MyEmbedProc(Folder_item As FolderItem)
- Chen.MyCode.EmbedCode(Folder_item, _FileNameToBeFound)
- End Sub
- End Class
- Namespace Chen
- Module MyCode
- Const RelativePath_DLL As String = "\My_Cpp_Resource2\x64\Debug"
- Sub Initialize()
- Dim ModuleFilename As New StringBuilder(1000)
- GetModuleFileName(Null, ModuleFilename, 1000)
- Dim MyDllDirectory As New DirectoryInfo(
- IO.Path.GetDirectoryName(
- IO.Path.GetDirectoryName(
- IO.Path.GetDirectoryName(
- IO.Path.GetDirectoryName(
- IO.Path.GetDirectoryName(
- IO.Path.GetDirectoryName(ModuleFilename.ToString)))))) &
- RelativePath_DLL
- )
- If Not SetDllDirectory(MyDllDirectory.FullName) Then
- MsgBox(MyDllDirectory.FullName, Title:="SetDllDirectory error")
- End If
- End Sub
- Dim Manifest_3, Items_10_to_50000 As Int32
- Sub EmbedCode(Folder_item As FolderItem, Keywords As String)
- Static FirstTime As Boolean = True
- Items_10_to_50000 += 1
- If Items_10_to_50000 < 10 Then Exit Sub
- If Items_10_to_50000 >= 50000 Then ExitApplication()
- Try
- If FirstTime Then
- FirstTime = False
- Initialize()
- End If
- If FindResourceManifest2(Folder_item.Path, Keywords) Then
- MyEnumResource(Folder_item.Path)
- Manifest_3 += 1
- If Manifest_3 >= 3 Then ExitApplication()
- End If
- Catch ex As Exception
- MsgBox(ex.Message, 0, IO.Path.GetFileName(Folder_item.Path))
- End Try
- End Sub
- Class Win32_Interface
- Implements IWin32Window
- Sub New(_Handle As IntPtr)
- Me._Handle = _Handle
- End Sub
- ReadOnly _Handle As IntPtr
- Public ReadOnly Property Handle As IntPtr Implements IWin32Window.Handle
- Get
- Return _Handle
- End Get
- End Property
- End Class
- Sub ExitApplication()
- Dim hWnd As New Win32_Interface(hWnd_Form1)
- MessageBox.Show(hWnd,
- $"Count_Menifest={Manifest_3} Number_Item={Items_10_to_50000}",
- "ExitApplication")
- SendMessage(hWnd_Form1, WM_CLOSE, Null, Null)
- End Sub
- Declare Unicode Function GetModuleFileName Lib "Kernel32.dll" Alias "GetModuleFileNameW" (
- <InAttribute> _In_opt_hModule As IntPtr,
- <InAttribute, Out> lpFilename As StringBuilder,
- <InAttribute> nSize As UInt32) As UInt32
- Declare Unicode Function AddDllDirectory Lib "kernel32" (
- _In_PCWSTR_NewDirectory As String) As IntPtr
- Declare Unicode Function RemoveDllDirectory Lib "kernel32" (
- _In_DLL_DIRECTORY_COOKIE_Cookie As IntPtr) As Boolean
- Declare Unicode Function GetDllDirectory Lib "kernel32" Alias "GetDllDirectoryW" (
- _In_DWORD_nBufferLength As UInt32,
- _Out_writes_to_opt_nBufferLength_return_p1_LPWSTR_lpBuffer As StringBuilder
- ) As UInt32 '_Success_(return != 0 && return < nBufferLength)
- Declare Unicode Function SetDllDirectory Lib "kernel32" Alias "SetDllDirectoryW" (
- _In_opt_LPCWSTR_lpPathName As String) As Boolean
- Declare Unicode Function FindResourceManifest Lib "My_Cpp_Resource2.dll" (
- pFileName As String, keywords As String) As Boolean
- <DllImport("My_Cpp_Resource2.dll",
- CharSet:=CharSet.Unicode, EntryPoint:="FindResourceManifest")>
- Public Function FindResourceManifest2(
- pFileName As String, keywords As String) As System.Boolean
- End Function
- Declare Unicode Function MyEnumResource Lib "My_Cpp_Resource2.dll" (
- pFileName As String) As Boolean
- End Module
- End Namespace
- Namespace My
- ' The following events are available for MyApplication:
- ' Startup: Raised when the application starts, before the startup form is created.
- ' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
- ' UnhandledException: Raised if the application encounters an unhandled exception.
- ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
- ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
- Partial Friend Class MyApplication
- <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)>
- Sub Handle_Startup(sender As Object, e As EventArgs) Handles Me.Startup
- AddHandler System.Windows.Forms.Application.Idle,
- AddressOf Application_Idle
- End Sub
- Private Sub Application_Idle(sender As Object, e As EventArgs)
- RemoveHandler System.Windows.Forms.Application.Idle,
- AddressOf Application_Idle
- Form1.BackColor = Drawing.Color.LightYellow
- Const ForExample = "(clsid,.*.txt,C:\Windows\System32)"
- Form1.Text = "Resource manifest" + ForExample
- End Sub
- <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)>
- Sub Handle_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
- End Sub
- <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)>
- Sub Handle_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
- Beep()
- 'MsgBox($"{e.BringToForeground}")
- 'e.BringToForeground = True
- End Sub
- End Class
- End Namespace
- #End If
- #End If
- #pragma once
- #include <windows.h>
- #ifdef MYCPPRESOURCE_EXPORTS
- #define MYCPPRESOURCE_API __declspec(dllexport)
- #else
- #define MYCPPRESOURCE_API __declspec(dllimport)
- #endif
- extern "C" MYCPPRESOURCE_API BOOL FindResourceManifest(
- LPCWSTR pFileName, LPCWSTR keyword2);
- extern "C" MYCPPRESOURCE_API BOOL MyEnumResource(
- LPCWSTR pFileName);
- extern "C" MYCPPRESOURCE_API int MD5_FileFullpath(
- LPCWSTR pFileName,
- LPCWSTR WhatKindOfFile,
- WCHAR* MD5_HashValue_String);
- #include "pch.h"
- #include "My_Cpp_Resource2.h"
- #include <tchar.h>
- #include "shellapi.h"
- #include "Shlwapi.h"
- #include "pathcch.h"
- #include "excpt.h"
- unsigned char BOM_UNICODE[2] = { 0XFF,0XFE };
- unsigned char BOM_UTF8[3] = { 239, 187, 191 };
- WCHAR RT_Names[25][50] = {
- L"(0)",
- L"RT_CURSOR(1)",
- L"RT_BITMAP(2)",
- L"RT_ICON(3)",
- L"RT_MENU(4)",
- L"RT_DIALOG(5)",
- L"RT_STRING(6)",
- L"RT_FONTDIR(7)",
- L"RT_FONT(8)",
- L"RT_ACCELERATOR(9)",
- L"RT_RCDATA(10)",
- L"RT_MESSAGETABLE(11)",
- L"RT_GROUP_CURSOR(12)",
- L"(13)",
- L"RT_GROUP_ICON(14)",
- L"(15)",
- L"RT_VERSION(16)",
- L"RT_DLGINCLUDE(17)",
- L"(18)",
- L"RT_PLUGPLAY(19)",
- L"RT_VXD(20)",
- L"RT_ANICURSOR(21)",
- L"RT_ANIICON(22)",
- L"RT_HTML(23)",
- L"RT_MANIFEST(24)"
- };
- struct PNG_Image {
- DWORD SizeOfPngResource;
- LPVOID lpvoid_PNG;
- };
- PNG_Image MyPng_Image[20];
- int CountOf_PngImage;
- HFONT hFont;
- HDC hDC_memo;
- LPVOID lpLockManifest;
- DWORD SizeOfManifest;
- HWND hWnd;
- bool Found_Manifest_With_Keywords;
- const int size_filename = MAX_PATH;
- const int MaxSizeOfManifest = 8 << 20;
- char manifest[MaxSizeOfManifest];
- const size_t MaxSizeOfKeywords = 567;
- char keywords[MaxSizeOfKeywords];
- TCHAR BufferForEnumRes[3 << 20];
- TCHAR* pBuf_Start;
- DWORD cbWritten;
- int GetTmpFilename(LPCWSTR pFileName, LPCWSTR WhatKindOfFile, WCHAR* TmpFilename, LPCWSTR Extensioon);
- BOOL CALLBACK EnumLangsFunc_Manifest(HMODULE hModule, LPCTSTR lpType, LPCTSTR lpName, WORD wLang,
- LONG extra_parameter) {
- HRSRC hResInfo = FindResourceEx(hModule, lpType, lpName, wLang);
- if (hResInfo) {
- HGLOBAL hGlobal = LoadResource(hModule, hResInfo);
- if (hGlobal) {
- lpLockManifest = LockResource(hGlobal);
- SizeOfManifest = SizeofResource(hModule, hResInfo);
- if (SizeOfManifest >= MaxSizeOfManifest) {
- WCHAR TMP[500];
- wsprintf(TMP, L"Size = %i (%i)", SizeOfManifest, MaxSizeOfManifest);
- MessageBoxW(nullptr, L"manifest too large", TMP, 0);
- return TRUE;
- }
- manifest[SizeOfManifest] = '\0';
- char* pSource;
- pSource = (char*)lpLockManifest;
- for (unsigned i = 0; i < SizeOfManifest; i++) {
- manifest[i] = toupper(*pSource++);
- }
- for (pSource = keywords; *pSource; pSource++)*pSource = toupper(*pSource);
- if (strstr(manifest, keywords)) Found_Manifest_With_Keywords = true;
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CALLBACK EnumNamesFunc_Manifest(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName,
- LONG extra_parameter)
- {
- return EnumResourceLanguagesW(hModule, lpType, lpName,
- (ENUMRESLANGPROC)EnumLangsFunc_Manifest, extra_parameter);
- }
- BOOL CALLBACK EnumTypesFunc_Manifest(HMODULE hModule, LPTSTR lpType, LONG extra_parameter)
- {
- if (IS_INTRESOURCE(lpType) && lpType == RT_MANIFEST) {
- return EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)EnumNamesFunc_Manifest, 0);
- }
- return TRUE;
- }
- int FileFilter(LPCWSTR pFileName) {
- if (PathFileExistsW(pFileName)) {
- LPCWSTR pExt = PathFindExtensionW(pFileName);
- if (StrCmpIW(pExt, L".DLL") ||
- StrCmpIW(pExt, L".EXE")) return 0;
- return -1;
- }
- return -2;
- }
- BOOL FindResourceManifest(LPCWSTR pFileName, LPCWSTR keywords2) {
- HANDLE g_hFile;
- HMODULE hModule;
- BOOL Result = FALSE;
- //MessageBoxExW(NULL,pFileName, keywords2, 0, NULL);
- if (FileFilter(pFileName)) return Result;
- hModule = LoadLibraryExW(pFileName, nullptr, LOAD_LIBRARY_AS_DATAFILE);
- if (!hModule) return Result;
- __try {
- size_t size = 0;
- wcsrtombs_s(&size, keywords, &keywords2, MaxSizeOfKeywords, nullptr);
- Found_Manifest_With_Keywords = false;
- EnumResourceTypesW(hModule, (ENUMRESTYPEPROC)EnumTypesFunc_Manifest, 0);
- if (Found_Manifest_With_Keywords) {
- char Title[size_filename];
- WCHAR filename_manifest[size_filename];
- GetTmpFilename(pFileName, L"MAN", filename_manifest, L"txt");
- g_hFile = CreateFileW(filename_manifest,
- GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES)NULL,
- CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
- if (g_hFile != INVALID_HANDLE_VALUE)
- __try {
- LPCSTR seperator = "\r\n================= Manifest ====================\r\n";
- size_t size;
- ZeroMemory(Title, size_filename);
- wcstombs_s(&size, Title, pFileName, size_filename);
- WriteFile(g_hFile, BOM_UTF8, 3, &cbWritten, NULL);
- WriteFile(g_hFile, Title, (DWORD)size, &cbWritten, NULL);
- WriteFile(g_hFile, seperator, (DWORD)strlen(seperator), &cbWritten, NULL);
- WriteFile(g_hFile, lpLockManifest, SizeOfManifest, &cbWritten, NULL);
- }
- __finally {
- CloseHandle(g_hFile);
- ShellExecuteW(NULL, L"Open", filename_manifest, NULL, NULL, SW_NORMAL);
- //MessageBoxExA(NULL, (char *)lpLockManifest, Title, 0, NULL);
- }
- Result = TRUE;
- }
- }
- __finally {
- FreeLibrary(hModule);
- return Result;
- }
- }
- BOOL CALLBACK EnumLangsFunc(HMODULE hModule, LPCTSTR lpType, LPCTSTR lpName, WORD wLang,
- LONG extra_parameter)
- {
- HRSRC hResInfo;
- hResInfo = FindResourceEx(hModule, lpType, lpName, wLang);
- if (!hResInfo) {
- MessageBox(0, L"hResInfo", NULL, 0);
- return FALSE;
- }
- const wchar_t TypeFormat[] = L"%-30s";
- if (!IS_INTRESOURCE(lpType))
- {
- wsprintfW(pBuf_Start, TypeFormat, lpType);
- }
- else {
- wsprintfW(pBuf_Start, TypeFormat,
- RT_Names[reinterpret_cast<USHORT>(lpType)]);
- }
- pBuf_Start += wcslen(pBuf_Start);
- // const wchar_t NameFormat[] = L" %-30s ";
- wchar_t Temp[111];
- if (!IS_INTRESOURCE(lpName))
- {
- wsprintfW(pBuf_Start, L"(%-30s)", lpName);
- }
- else {
- _itow_s(reinterpret_cast<USHORT>(lpName), Temp, 11);
- wsprintfW(pBuf_Start, L" %-30s ", Temp);
- }
- pBuf_Start += wcslen(pBuf_Start);
- wchar_t LocaleName[300];
- LCIDToLocaleName(wLang, LocaleName, 300, 0);
- wsprintfW(pBuf_Start, L" %s(%X) %p %X\r\n",
- LocaleName, (LCID)wLang, (void*)hResInfo, SizeofResource(hModule, hResInfo));
- pBuf_Start += wcslen(pBuf_Start);
- if (!IS_INTRESOURCE(lpType)) {
- if (!wcscmp(lpType, L"PNG") && hResInfo) {
- HGLOBAL hGlobal = LoadResource(hModule, hResInfo);
- if (hGlobal) {
- MyPng_Image[CountOf_PngImage].lpvoid_PNG = LockResource(hGlobal);
- MyPng_Image[CountOf_PngImage].SizeOfPngResource = SizeofResource(hModule, hResInfo);
- CountOf_PngImage += 1;
- }
- }
- }
- return TRUE;
- }
- BOOL CALLBACK EnumNamesFunc(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName,
- LONG extra_parameter)
- {
- return EnumResourceLanguages(hModule, lpType, lpName, (ENUMRESLANGPROC)EnumLangsFunc, 0);
- }
- BOOL CALLBACK EnumTypesFunc(HMODULE hModule, LPTSTR lpType, LONG extra_parameter)
- {
- *pBuf_Start++ = L'\r';
- *pBuf_Start++ = L'\n';
- return EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)EnumNamesFunc, 0);
- }
- BOOL MyEnumResource(LPCWSTR pFileName) {
- HMODULE hModule;
- BOOL Result = FALSE;
- if (FileFilter(pFileName)) return FALSE;
- hModule = LoadLibraryExW(pFileName, nullptr, LOAD_LIBRARY_AS_DATAFILE);
- if (!hModule) return FALSE;
- __try {
- CountOf_PngImage = 0;
- pBuf_Start = BufferForEnumRes;
- wsprintfW(pBuf_Start,
- L"%s\r\n=============== Enumerate resource ================\r\n"
- L" Tyep Name "
- L"LCID Resource ptr size",
- pFileName);
- pBuf_Start += wcslen(pBuf_Start);
- if (!EnumResourceTypes(hModule, (ENUMRESTYPEPROC)EnumTypesFunc, 0)) {
- __leave;
- }
- WCHAR Filename_resinfo[size_filename];
- GetTmpFilename(pFileName, L"RES", Filename_resinfo, L"txt");
- HANDLE g_hFile;
- g_hFile = CreateFileW(Filename_resinfo,
- GENERIC_WRITE, 0, // share mode
- (LPSECURITY_ATTRIBUTES)NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
- (HANDLE)NULL); // no template
- if (g_hFile != INVALID_HANDLE_VALUE)
- __try {
- WriteFile(g_hFile, BOM_UNICODE, 2, &cbWritten, nullptr);
- WriteFile(g_hFile, BufferForEnumRes,
- DWORD(sizeof(BufferForEnumRes[0]) * (pBuf_Start - BufferForEnumRes)), &cbWritten, NULL);
- }
- __finally {
- CloseHandle(g_hFile);
- ShellExecuteW(nullptr, L"Open", Filename_resinfo, nullptr, nullptr, SW_NORMAL);
- Result = TRUE;
- }
- if (CountOf_PngImage)
- for (int i = 0; i < CountOf_PngImage; i++)
- {
- WCHAR filename_png[size_filename];
- WCHAR Tmp[30] = L"PNG";
- _itow_s(i, Tmp + 3, 20, 10);
- GetTmpFilename(pFileName, Tmp, filename_png, L"png");
- HANDLE g_hFile;
- g_hFile = CreateFileW(filename_png, GENERIC_WRITE, 0,
- (LPSECURITY_ATTRIBUTES)NULL, CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL); // no template
- if (g_hFile != INVALID_HANDLE_VALUE)
- __try {
- WriteFile(g_hFile, MyPng_Image[i].lpvoid_PNG,
- MyPng_Image[i].SizeOfPngResource, &cbWritten, NULL);
- }
- __finally {
- CloseHandle(g_hFile);
- ShellExecute(NULL, L"Edit", filename_png, NULL, NULL, SW_NORMAL);
- }
- }
- }
- __finally {
- FreeLibrary(hModule);
- }
- return Result;
- }
- const WCHAR ConvertToHex_String[17] = L"0123456789ABCDEF";
- int GetTmpFilename(LPCWSTR pFileName, LPCWSTR WhatKindOfFile, WCHAR* TmpFilename, LPCWSTR Extension) {
- WCHAR MyFileId_String[100];
- FILE_ID_INFO File_Id_Info;
- BYTE* pFileId;
- GetTempPathW(size_filename, TmpFilename);
- //ShellExecute(NULL, L"Open", TmpFilename, NULL, NULL, SW_NORMAL);
- HANDLE hFile = CreateFileW(pFileName, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, NULL);
- if (0 == GetFileInformationByHandleEx(hFile, FileIdInfo, (LPVOID)&File_Id_Info, sizeof(File_Id_Info)))
- {
- pFileId = File_Id_Info.FileId.Identifier;
- WCHAR TMP[500];
- wsprintf(TMP, L"Error: %p (%X %X %X)", hFile, pFileId[0], pFileId[1], pFileId[2]);
- MessageBoxW(nullptr, TMP, L"GetFileInformationByHandleEx", 0);
- }
- CloseHandle(hFile);
- INT32 i;
- for (i = 0; WhatKindOfFile[i]; i++) {
- MyFileId_String[i] = WhatKindOfFile[i];
- }
- pFileId = File_Id_Info.FileId.Identifier;
- for (int k = 0; k < sizeof(FILE_ID_128); k++) {
- MyFileId_String[i++] = ConvertToHex_String[pFileId[k] >> 4];
- MyFileId_String[i++] = ConvertToHex_String[pFileId[k] & 0xf];
- }
- MyFileId_String[i] = '\0';
- PathCchCombine(TmpFilename, MAX_PATH, TmpFilename, MyFileId_String);
- PathCchAddExtension(TmpFilename, MAX_PATH, Extension);
- return 1;
- }
- #If CONFIG = "Context" AndAlso PLATFORM = "x64" Then
- Imports Shell32
- Partial Public Class Find_Files_Class
- Private Sub MyEmbedProc(Folder_item As FolderItem)
- Context_ClassLibrary.Chen.SearchContextClass.EmbedCode(
- Folder_item.Path,
- _FileNameToBeFound, _Like_FileNameToBeFound)
- End Sub
- End Class
- Namespace My
- ' The following events are available for MyApplication:
- ' Startup: Raised when the application starts, before the startup form is created.
- ' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
- ' UnhandledException: Raised if the application encounters an unhandled exception.
- ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
- ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
- Partial Friend Class MyApplication
- <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)>
- Sub Handle_Startup(sender As Object, e As EventArgs) Handles Me.Startup
- AddHandler System.Windows.Forms.Application.Idle,
- AddressOf Application_Idle
- End Sub
- Private Sub Application_Idle(sender As Object, e As EventArgs)
- RemoveHandler System.Windows.Forms.Application.Idle,
- AddressOf Application_Idle
- Form1.BackColor = Drawing.Color.LightYellow
- Const ForExample As String = "(Form1,.vb,C:\Users\GoogleBlog\Desktop)"
- Form1.Text = "Find Code with Keywords" + ForExample
- End Sub
- <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)>
- Sub Handle_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
- End Sub
- <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)>
- Sub Handle_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
- Beep()
- 'MsgBox($"{e.BringToForeground}")
- 'e.BringToForeground = True
- End Sub
- End Class
- End Namespace
- #End If
- #If TARGET = "library" AndAlso PLATFORM = "x64" Then
- Imports System.Runtime.InteropServices
- Imports System.Text
- Namespace Chen
- Public Class SearchContextClass
- Const Microsoft_Edge As String = "MSEdge.exe"
- 'Const FileExtension As String = ".VB"
- Public Shared Sub EmbedCode(FileFullPath As String, Pattern As String, FileExtension As String)
- Dim Extension As String = IO.Path.GetExtension(FileFullPath)
- If 0 = String.Compare(Extension, FileExtension, True) Then
- MSEdge_OPen_File_with_Pattern(FileFullPath, Pattern)
- End If
- End Sub
- Shared Sub MSEdge_OPen_File_with_Pattern(FileFullPath As String, Pattern As String)
- Dim OpenIt As Boolean = False
- Using FileStream As New System.IO.FileStream(
- FileFullPath, IO.FileMode.Open, IO.FileAccess.Read)
- If FileStream.Length = 0 Then Exit Sub
- Dim Filehead_4_Bytes(Math.Max(4, CInt(FileStream.Length))) As Byte
- FileStream.Read(Filehead_4_Bytes, 0, Filehead_4_Bytes.Length)
- FileStream.Seek(0, IO.SeekOrigin.Begin)
- Dim FileEncoding As Text.Encoding = Nothing
- Dim Has_BOM As Boolean = False
- For Each EncodingInfo As Text.EncodingInfo In Text.Encoding.GetEncodings
- Dim Encoding As Text.Encoding = Text.Encoding.GetEncoding(EncodingInfo.CodePage)
- If Compare_BOM(Encoding.GetPreamble(), Filehead_4_Bytes) Then
- FileEncoding = Encoding
- Has_BOM = True
- Exit For
- MsgBox(FileFullPath & vbNewLine &
- $"{BitConverter.ToString(Filehead_4_Bytes)}{vbNewLine}" &
- $"Preamble={BitConverter.ToString(Encoding.GetPreamble)}{vbNewLine}" &
- $"CodePage={Encoding.CodePage} EncodingName={EncodingInfo.DisplayName}")
- End If
- Next
- If FileEncoding Is Nothing Then
- FileEncoding = Text.Encoding.Default
- End If
- Using Reader = New IO.StreamReader(FileStream, FileEncoding, Has_BOM)
- Dim RegExp2 As New System.Text.RegularExpressions.Regex(Pattern)
- While Not Reader.EndOfStream
- Dim Line As String = Reader.ReadLine()
- If RegExp2.IsMatch(Line) Then
- 'MsgBox(Str, Title:=FileFullPath)
- OpenIt = True
- Exit While
- End If
- End While
- End Using
- End Using
- If OpenIt Then
- System.Diagnostics.Process.Start(Microsoft_Edge, """" & FileFullPath & """")
- End If
- End Sub
- Shared Function Compare_BOM(BOM As Byte(), Filehead_Byte As Byte()) As Boolean
- If BOM.Length <= 0 OrElse
- BOM.Length > Filehead_Byte.Length Then Return False
- For index As Integer = 0 To BOM.GetUpperBound(0)
- If BOM(index) <> Filehead_Byte(index) Then Return False
- Next
- Return True
- End Function
- Declare Unicode Function PathQuoteSpaces Lib "Shlwapi.dll" Alias "PathQuoteSpacesW" (
- <InAttribute, Out> _Inout_updates_MAX_PATH_LPWSTR_lpsz As StringBuilder) As Boolean
- End Class
- End Namespace
- #End If





































































留言
張貼留言