コンパイル後は、管理者コマンドプロンプトでレジストリ登録する必要があります。
regsvr32 "C:\YourFolder\ClassLibraryNet5.comhost.dll"
※アンレジストしたい場合は /u オプション
詳細は下記を参照してください。
https://docs.microsoft.com/ja-jp/dotnet/core/native-interop/expose-components-to-com
コンパイル後は、管理者コマンドプロンプトでレジストリ登録する必要があります。
regsvr32 "C:\YourFolder\ClassLibraryNet5.comhost.dll"
※アンレジストしたい場合は /u オプション
詳細は下記を参照してください。
https://docs.microsoft.com/ja-jp/dotnet/core/native-interop/expose-components-to-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 |