it技術:vb2clr
差分
このページの2つのバージョン間の差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| it技術:vb2clr [2025/06/17 11:46] – 作成 yajuadmin | it技術:vb2clr [2025/06/20 15:40] (現在) – [使い方] yajuadmin | ||
|---|---|---|---|
| 行 4: | 行 4: | ||
| ===== 使い方 ===== | ===== 使い方 ===== | ||
| - | https:// | + | ExitHandler.bas と CLRHost.cls ファイルをプロジェクトにドラッグ&ドロップする。\\ |
| - | + | ||
| - | ExitHandler.bas と CLRHost.cls ファイルをプロジェクトにドラッグ&ドロップする。 | + | |
| {{: | {{: | ||
| === 補足 === | === 補足 === | ||
| - | vb2clrは日本人の方が開発しており、ソース内コメントなどがShift-JISで扱われている。\\ | + | vb2clrは日本人の方が開発しており、Excel関連ファイルはShift-JISを用いている。\\ |
| - | そのため、海外向けに日本コメントを除去したファイルが用意されている。 | + | そのため、海外向けにUTF-8用に変換されたファイルが用意されている。 |
| * ExitHandler.utf8.bas | * ExitHandler.utf8.bas | ||
| * CLRHost.utf8.cls | * CLRHost.utf8.cls | ||
| 行 19: | 行 17: | ||
| {{: | {{: | ||
| + | |||
| + | ==== 公開キートークン ==== | ||
| + | .NET Frameworkのライブラリーを使用するのに、公開キートークンを知る必要がある。 | ||
| + | |||
| + | === 調べ方 === | ||
| + | [[https:// | ||
| + | |||
| + | VS 2022用開発者コマンドプロンプトを起動します。\\ | ||
| + | ネイティブx64バージョン([ x64 Native Tools Command Prompt for VS 2022 ] )を選択してください。 | ||
| + | |||
| + | 例として、「System.dll」を調べます。 | ||
| + | |||
| + | < | ||
| + | cd " | ||
| + | |||
| + | C: | ||
| + | |||
| + | Microsoft(R) .NET Framework Strong Name Utility バージョン 4.0.30319.0 | ||
| + | Copyright (c) Microsoft Corporation. All rights reserved. | ||
| + | |||
| + | 公開キー トークン b77a5c561934e089 | ||
| + | </ | ||
| + | |||
| + | === よく使用するもの ==== | ||
| + | アセンブリにより同じPublicKeyTokenを使用している。 | ||
| + | |||
| + | ^System.Collections|System.Collections, | ||
| + | ^System.IO|System.IO, | ||
| + | ^System.Text.Encoding|System.Text.Encoding, | ||
| + | ^System.Drawing|System.Drawing, | ||
| + | ^System|System, | ||
| + | ^System.IO.Compression.FileSystem|System.IO.Compression.FileSystem, | ||
| + | |||
| + | === どのアセンブリを使っているのか === | ||
| + | 例えば、「System.Security.Cryptography.SHA256Managed」は、どのdllなのか? | ||
| + | |||
| + | マイクロソフトのサイトで検索、バージョンを「.NET Framework 4.8.1」を指定する。 | ||
| + | SHA256Managed クラスにアセンブリ: | ||
| + | https:// | ||
| + | |||
| + | < | ||
| + | SN.exe -T " | ||
| + | 公開キー トークン b77a5c561934e089 | ||
| + | </ | ||
| + | |||
| + | System.dll と同じなので、下記を使用すればよい。 | ||
| + | < | ||
| + | Dim Assem As mscorlib.Assembly | ||
| + | host.CLRLoadAssembly (" | ||
| + | </ | ||
| ===== サンプル ===== | ===== サンプル ===== | ||
| 行 256: | 行 304: | ||
| Set host = Nothing | Set host = Nothing | ||
| End Sub | End Sub | ||
| + | </ | ||
| + | |||
| + | ==== SHAハッシュの取得 ==== | ||
| + | <code vb> | ||
| + | Private Sub ComputeSHATest() | ||
| + | | ||
| + | Debug.Print " | ||
| + | Debug.Print GetSHA256(Range(" | ||
| + | |||
| + | Debug.Print " | ||
| + | Debug.Print GetSHA384(Range(" | ||
| + | | ||
| + | Debug.Print " | ||
| + | Debug.Print GetSHA512(Range(" | ||
| + | End Sub | ||
| + | </ | ||
| + | |||
| + | <code text 結果> | ||
| + | ' A1セルに" | ||
| + | SHA256 | ||
| + | a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e | ||
| + | SHA384 | ||
| + | 99514329186b2f6ae4a1329e7ee6c610a729636335174ac6b740f9028396fcc803d0e93863a7c3d90f86beee782f4f3f | ||
| + | SHA512 | ||
| + | 2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b | ||
| + | </ | ||
| + | |||
| + | RelaxTools-Addin のソースコードを元に書き換え、これを使うことで.NET Framweork 3.5のインストールが不要になる。\\ | ||
| + | https:// | ||
| + | |||
| + | <code vb basFunction> | ||
| + | Option Explicit | ||
| + | Private Const C_SHA256 As Long = 1 | ||
| + | Private Const C_SHA384 As Long = 2 | ||
| + | Private Const C_SHA512 As Long = 3 | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | Public Function GetSHA256(ハッシュ値算出範囲 As Range) As Variant | ||
| + | Application.Volatile | ||
| + | GetSHA256 = ComputeSHA(C_SHA256, | ||
| + | |||
| + | End Function | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | Public Function GetSHA384(ハッシュ値算出範囲 As Range) As Variant | ||
| + | Application.Volatile | ||
| + | GetSHA384 = ComputeSHA(C_SHA384, | ||
| + | |||
| + | End Function | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | Public Function GetSHA512(ハッシュ値算出範囲 As Range) As Variant | ||
| + | Application.Volatile | ||
| + | GetSHA512 = ComputeSHA(C_SHA512, | ||
| + | |||
| + | End Function | ||
| + | |||
| + | Private Function ComputeSHA(lngType As Long, r As Range) As Variant | ||
| + | |||
| + | Dim host As New CLRHost | ||
| + | Call host.Initialize(False) | ||
| + | host.CLRLoadAssembly (" | ||
| + | | ||
| + | Dim str As String | ||
| + | ' | ||
| + | str = Application.WorksheetFunction.Concat(r) | ||
| + | | ||
| + | ' | ||
| + | Dim code() As Byte | ||
| + | Dim objUTF8 As mscorlib.Object | ||
| + | Set objUTF8 = host.ToObject(host.CLRCreateObject(" | ||
| + | code = host.CLRInvokeMethod(objUTF8, | ||
| + | Set objUTF8 = Nothing | ||
| + | | ||
| + | ' | ||
| + | Dim hashValue() As Byte | ||
| + | Dim objSHA As mscorlib.Object | ||
| + | Select Case lngType | ||
| + | Case C_SHA256 | ||
| + | Set objSHA = host.ToObject(host.CLRCreateObject(" | ||
| + | Case C_SHA384 | ||
| + | Set objSHA = host.ToObject(host.CLRCreateObject(" | ||
| + | Case C_SHA512 | ||
| + | Set objSHA = host.ToObject(host.CLRCreateObject(" | ||
| + | End Select | ||
| + | hashValue = host.CLRInvokeMethod(objSHA, | ||
| + | Set objSHA = Nothing | ||
| + | | ||
| + | ' | ||
| + | Dim description As String | ||
| + | | ||
| + | description = "" | ||
| + | | ||
| + | Dim i As Long | ||
| + | For i = LBound(hashValue()) To UBound(hashValue()) | ||
| + | description = description & Right(" | ||
| + | Next i | ||
| + | |||
| + | ' | ||
| + | ComputeSHA = LCase(description) | ||
| + | |||
| + | Set host = Nothing | ||
| + | End Function | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
it技術/vb2clr.1750128363.txt.gz · 最終更新: 2025/06/17 11:46 by yajuadmin
