Skip to content

Instantly share code, notes, and snippets.

@Benshi
Last active November 8, 2021 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benshi/4b73898bf4da515fb643e99909e1e298 to your computer and use it in GitHub Desktop.
Save Benshi/4b73898bf4da515fb643e99909e1e298 to your computer and use it in GitHub Desktop.
[VB] .NET 5 製 COM コンポーネント
'Guid は固有の値を指定する必要があります
Imports System.Runtime.InteropServices
Imports System.Net.Http
Imports System.Text
<ComVisible(True)>
<CoClass(GetType(ClassLibraryNet5))>
<Guid("E001C99F-F4BC-4A40-8C4C-3CF4D8E701EE")>
<InterfaceType(ComInterfaceType.InterfaceIsDual)>
Public Interface IClassLibraryNet5
Function GetByteArray(url As String) As Byte()
Function GetString(url As String) As String
Function GetStringWithCharset(url As String, charset As String) As String
End Interface
<ComVisible(True)>
<ClassInterface(ClassInterfaceType.None)>
<Guid("A3975DA3-B8A9-43A5-B302-2CC6CAA728FF")>
<ProgId("Orator.ClassLibraryNet5")>
Public Class ClassLibraryNet5
Implements IClassLibraryNet5
Private Shared ReadOnly client As New HttpClient()
Public Function GetString(url As String) As String Implements IClassLibraryNet5.GetString
Return client.GetStringAsync(url).GetAwaiter().GetResult
End Function
Public Function GetByteArray(url As String) As Byte() Implements IClassLibraryNet5.GetByteArray
Return client.GetByteArrayAsync(url).GetAwaiter().GetResult
End Function
Public Function GetStringWithCharset(url As String, charset As String) As String Implements IClassLibraryNet5.GetStringWithCharset
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
Return Encoding.GetEncoding(charset).GetString(GetByteArray(url))
End Function
End Class
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EnableComHosting>true</EnableComHosting>
<EnableRegFreeCom>true</EnableRegFreeCom>
<RootNamespace>ClassLibraryNet5</RootNamespace>
<TargetFramework>net5.0</TargetFramework>
<!--
32bit/64bit いずれの ActiveX DLL にするかで
RID Catalog の指定を切り替える必要があります
https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
-->
<NETCoreSdkRuntimeIdentifier>win10-x86</NETCoreSdkRuntimeIdentifier>
<!--
<NETCoreSdkRuntimeIdentifier>win10-x64</NETCoreSdkRuntimeIdentifier>
-->
</PropertyGroup>
</Project>
'VBA から呼び出す場合
Public Sub Test()
Dim o As Object, s As String
Set o = CreateObject("Orator.ClassLibraryNet5")
s = o.GetString("http://rucio.cloudapp.net/ThreadList.aspx")
MsgBox s
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment