1,安装VS2022预览版(Visual Studio Preview)
https://visualstudio.microsoft.com/zh-hans/vs/preview/#download-preview
2,选择安装组件:使用C++的桌面开发 和 .NET桌面开发
创建、编绎动态DLL:
1,C#命令行创建dll工程:
VS2022菜单:工具》命令行》开发者命令提示,输入:
dotnet new classlib -o CSharpDllExport -f net9.0
2,class1.cs修改代码:
vbnet
using System.Runtime.InteropServices;
namespace CSharpDllExport
{
public static class Class1
{
[UnmanagedCallersOnly(EntryPoint = "Add")]
public static int Add(int a, int b)
{
return a + b;
}
}
}
3,编绎生成32位标准Dll,输出函数Add(加法计算)
用时12秒左右,DLL大小,1MB
dotnet publish CSharpDllExport -p:PublishAot=true -r win-x86 -c release /p:NativeLib=Shared -f net9.0
4,VB6中调用API
CSharpDllExport\bin\release\net9.0\win-x86\publish\CSharpDllExport.dll
vbscript
Private Declare Function Add Lib "CSharpDllExport" (ByVal a As Long, ByVal b As Long) As Long
Private Sub Command1_Click()
MsgBox Add(3, 4)
End Sub