ユーザ用ツール

サイト用ツール


it技術:vb2clr

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
it技術:vb2clr [2025/06/19 10:17] – [使い方] yajuadminit技術:vb2clr [2025/06/20 15:40] (現在) – [使い方] yajuadmin
行 4: 行 4:
  
 ===== 使い方 ===== ===== 使い方 =====
-https://github.com/jet2jet/vb2clr からZipファイルを取得し展開する。 
- 
 ExitHandler.bas と CLRHost.cls ファイルをプロジェクトにドラッグ&ドロップする。\\ ExitHandler.bas と CLRHost.cls ファイルをプロジェクトにドラッグ&ドロップする。\\
 {{:it技術:vbproject.png|}} {{:it技術:vbproject.png|}}
行 23: 行 21:
 .NET Frameworkのライブラリーを使用するのに、公開キートークンを知る必要がある。 .NET Frameworkのライブラリーを使用するのに、公開キートークンを知る必要がある。
  
-調べ方として、SN.exeが利用できる。+=== 調べ方 ===
 [[https://devlights.hatenablog.com/entry/20130827/p1|公開キートークン(Public Key Token)の調べ方 (厳密名ツール, SN.exeを利用)]] [[https://devlights.hatenablog.com/entry/20130827/p1|公開キートークン(Public Key Token)の調べ方 (厳密名ツール, SN.exeを利用)]]
 +
 +VS 2022用開発者コマンドプロンプトを起動します。\\
 +ネイティブx64バージョン([ x64 Native Tools Command Prompt for VS 2022 ] )を選択してください。
 +
 +例として、「System.dll」を調べます。
 +
 +<code>
 +cd "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
 +
 +C:\Windows\Microsoft.NET\Framework64\v4.0.30319>SN.exe -T "System.dll"
 +
 +Microsoft(R) .NET Framework Strong Name Utility バージョン 4.0.30319.0
 +Copyright (c) Microsoft Corporation. All rights reserved.
 +
 +公開キー トークン b77a5c561934e089
 +</code>
  
 === よく使用するもの ==== === よく使用するもの ====
 +アセンブリにより同じPublicKeyTokenを使用している。
  
-^System|System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089| 
 ^System.Collections|System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a| ^System.Collections|System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a|
 ^System.IO|System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a| ^System.IO|System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a|
 ^System.Text.Encoding|System.Text.Encoding, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a| ^System.Text.Encoding|System.Text.Encoding, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a|
-^System.IO.Compression.FileSystem|System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089| 
 ^System.Drawing|System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a| ^System.Drawing|System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a|
 +^System|System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|
 +^System.IO.Compression.FileSystem|System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|
 +
 +=== どのアセンブリを使っているのか ===
 +例えば、「System.Security.Cryptography.SHA256Managed」は、どのdllなのか?
 +
 +マイクロソフトのサイトで検索、バージョンを「.NET Framework 4.8.1」を指定する。
 +SHA256Managed クラスにアセンブリ:mscorlib.dll と記載されている。\\
 +https://learn.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.sha256managed?view=netframework-4.8.1
 +
 +<code>
 +SN.exe -T "mscorlib.dll"
 +公開キー トークン b77a5c561934e089
 +</code>
 +
 +System.dll と同じなので、下記を使用すればよい。
 +<code>
 +Dim Assem As mscorlib.Assembly
 +host.CLRLoadAssembly ("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
 +</code>
  
 ===== サンプル ===== ===== サンプル =====
行 272: 行 305:
 End Sub End Sub
 </code> </code>
 +
 +==== SHAハッシュの取得 ====
 +<code vb>
 +Private Sub ComputeSHATest()
 +    
 +    Debug.Print "SHA256"
 +    Debug.Print GetSHA256(Range("A1"))
 +
 +    Debug.Print "SHA384"
 +    Debug.Print GetSHA384(Range("A1"))
 +    
 +    Debug.Print "SHA512"
 +    Debug.Print GetSHA512(Range("A1"))
 +End Sub
 +</code>
 +
 +<code text 結果>
 +' A1セルに"Hello World"をセット
 +SHA256
 +a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
 +SHA384
 +99514329186b2f6ae4a1329e7ee6c610a729636335174ac6b740f9028396fcc803d0e93863a7c3d90f86beee782f4f3f
 +SHA512
 +2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b
 +</code>
 +
 +RelaxTools-Addin のソースコードを元に書き換え、これを使うことで.NET Framweork 3.5のインストールが不要になる。\\
 +https://software.opensquare.net/relaxtools/archives/3892/
 +
 +<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
 +'--------------------------------------------------------------
 +' SHA256算出関数
 +'--------------------------------------------------------------
 +Public Function GetSHA256(ハッシュ値算出範囲 As Range) As Variant
 +    Application.Volatile
 +    GetSHA256 = ComputeSHA(C_SHA256, ハッシュ値算出範囲)
 +
 +End Function
 +'--------------------------------------------------------------
 +' SHA384算出関数
 +'--------------------------------------------------------------
 +Public Function GetSHA384(ハッシュ値算出範囲 As Range) As Variant
 +    Application.Volatile
 +    GetSHA384 = ComputeSHA(C_SHA384, ハッシュ値算出範囲)
 +
 +End Function
 +'--------------------------------------------------------------
 +' SHA2512算出関数
 +'--------------------------------------------------------------
 +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 ("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
 +    
 +    Dim str As String
 +    'Rangeの文字列結合(ワークシート関数のConcatを流用)
 +    str = Application.WorksheetFunction.Concat(r)
 +    
 +    'バイト読み込み
 +    Dim code() As Byte
 +    Dim objUTF8 As mscorlib.Object
 +    Set objUTF8 = host.ToObject(host.CLRCreateObject("System.Text.UTF8Encoding"))
 +    code = host.CLRInvokeMethod(objUTF8, "GetBytes", str)
 +    Set objUTF8 = Nothing
 +    
 +    'ハッシュ値計算
 +    Dim hashValue() As Byte
 +    Dim objSHA As mscorlib.Object
 +    Select Case lngType
 +        Case C_SHA256
 +            Set objSHA = host.ToObject(host.CLRCreateObject("System.Security.Cryptography.SHA256Managed"))
 +        Case C_SHA384
 +            Set objSHA = host.ToObject(host.CLRCreateObject("System.Security.Cryptography.SHA384Managed"))
 +        Case C_SHA512
 +            Set objSHA = host.ToObject(host.CLRCreateObject("System.Security.Cryptography.SHA512Managed"))
 +    End Select
 +    hashValue = host.CLRInvokeMethod(objSHA, "ComputeHash", code)
 +    Set objSHA = Nothing
 +    
 +    '16進数へ変換
 +    Dim description As String
 +    
 +    description = ""
 +    
 +    Dim i As Long
 +    For i = LBound(hashValue()) To UBound(hashValue())
 +        description = description & Right("0" & Hex(hashValue(i)), 2)
 +    Next i
 +
 +    'return
 +    ComputeSHA = LCase(description)
 +
 +    Set host = Nothing
 +End Function
 +</code>
 +
 +
 +
 +
it技術/vb2clr.1750295842.txt.gz · 最終更新: 2025/06/19 10:17 by yajuadmin