0
votes

Net framework 2.0 dll in VB6 project. The same code in .Net Framework 4.5 (namespace was PasswordHashLibrary instead of PasswordHashLibrary2.0) was working fine on vb6 project but there is something that i am missing this time.

Here is my c# code on .net v2.0

//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
}

}

In project properties "Register for COM Interop" is checked. In assemblyInfo.cs

[assembly: ComVisible(true)]

after which i registered this dll with regasm

Now on vb6 project this .tlb is available as PasswordHashLibrary2_0 which i added as a reference. My Vb 6 project code is as follows

Private Sub Form_Load()

Dim objHash As New PasswordHashLibrary2_0.Hash
Dim temp As String

temp = objHash.HashPassword("fsfds")

End Sub

When i run this program, objHash is not set as you can seeenter image description here

After i move forward this line vb6 gives me error

Run-time error '-2147024894 (80070002)':

Automation error The system cannot find the file specified

2
Ticking the "Register for COM Interop" checkbox is enough to start testing the code. If you also use Regasm then it is important that you run the correct version and use the correct command line arguments. 32-bit and /codebase /tlb. The best way to test the code is by using Project > Properties > Debug and select vb6.exe as the start program. Now you can diagnose runtime exceptions and set breakpoints. - Hans Passant

2 Answers

0
votes

You can register it in GAC (Global Assembly Cache) with gacutil to make sure CLR recognizes it:

gacutil /l PasswordHashLibrary2_0.dll

Afterwards, try to use codebase parameter switch when registering .NET DLL assembly with regasm:

regasm [directory_path]\PasswordHashLibrary2_0.dll /codebase /tlb:[directory_path]\PasswordHashLibrary2_0.tlb

Normally CLR finds the assembly name in GAC before pointing to same directory where VB project stored in, check if the DLL already stored in GAC and TLB file resides at same location as its DLL after those steps taken.

-1
votes

Heyyy, So i found the issue!

First as @Tetsuya had pointed out gacutil, so i added strong name to gacutil and then tried installing the assembly which gave me error

Failure adding assembly to cache the module was expected to contain an assembly manifest.

This meant the assembly is corrupt so i changed the assembly name to PasswordHashLibrary2.dll instead and rebuild the project.

Then registered this assembly and added reference. Finally somehow, while testing New key word had been removed from vb6 code due to which i started getting error

Run-time error 91 object variable or block variable not set

So fixed it and now it works :)

Cheers