【发布时间】:2018-01-17 01:15:02
【问题描述】:
VB6 项目中的 Net framework 2.0 dll。 .Net Framework 4.5 中的相同代码(命名空间是 PasswordHashLibrary 而不是 PasswordHashLibrary2.0)在 vb6 项目上运行良好,但这次我缺少一些东西。
这是我在 .net v2.0 上的 c# 代码
//using statements here
namespace PasswordHashLibrary2._0
{
public interface ComClassInterface
{
}
public class Hash : ComClassInterface
{
private const int PBKDF2IterCount = 1000; // default for Rfc2898DeriveBytes
private const int PBKDF2SubkeyLength = 256 / 8; // 256 bits
private const int SaltSize = 128 / 8; // 128 bits
//[ComVisible(true)]
public string HashPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password cannot be null");
}
// Produce a version 0 (see comment above) text hash.
byte[] salt;
byte[] subkey;
var deriveBytes = new Rfc2898DeriveBytes(password, SaltSize, PBKDF2IterCount);
salt = deriveBytes.Salt;
subkey = deriveBytes.GetBytes(PBKDF2SubkeyLength);
var outputBytes = new byte[1 + SaltSize + PBKDF2SubkeyLength];
Buffer.BlockCopy(salt, 0, outputBytes, 1, SaltSize);
Buffer.BlockCopy(subkey, 0, outputBytes, 1 + SaltSize, PBKDF2SubkeyLength);
return Convert.ToBase64String(outputBytes);
}
// some other functions
}
}
在项目属性中选中“注册 COM 互操作”。 在 assemblyInfo.cs 中
[程序集:ComVisible(true)]
之后我用 regasm 注册了这个 dll
现在在 vb6 项目中,此 .tlb 可用作 PasswordHashLibrary2_0,我将其添加为参考。我的VB 6项目代码如下
Private Sub Form_Load()
Dim objHash As New PasswordHashLibrary2_0.Hash
Dim temp As String
temp = objHash.HashPassword("fsfds")
End Sub
在我前进这一行 vb6 给我错误
运行时错误'-2147024894 (80070002)':
自动化错误系统找不到指定的文件
【问题讨论】:
-
勾选“Register for COM Interop”复选框足以开始测试代码。如果您也使用 Regasm,那么运行正确的版本并使用正确的命令行参数很重要。 32 位和 /codebase /tlb。测试代码的最佳方法是使用 Project > Properties > Debug 并选择 vb6.exe 作为启动程序。现在您可以诊断运行时异常并设置断点。