Error importing DLL

Asked

Viewed 269 times

3

I have a DLL made in C#. When importing it to be used on another computer, I find the class. However, her methods are not published;

Follows my code:

using System;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace PortalRH.DLL
{
[ComVisible(true), ClassInterface(ClassInterfaceType.None),
Guid("00AC4F7E-71B0-4BC7-AD8E-1175CD88457A")]
public class Criptografia 
{
    private string chave = "chave";
    public Criptografia(){}


    // Essa seqüência constante é usada como um valor "salt" para as chamadas de função PasswordDeriveBytes .
    // Este tamanho da IV (em bytes) devem = (KeySize / 8). KeySize padrão é 256, portanto, a IV deve ser
    // 32 bytes de comprimento. Usando uma seqüência de 16 caracteres aqui nos dá 32 bytes quando convertido para um array de bytes.
    private static readonly byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2");

    // Esta constante é utilizado para determinar o tamanho da chave do algoritmo de encriptação.
    private const int keysize = 256;

    public static string Encrypt(string plainText, string passPhrase)
    {
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
        {
            byte[] keyBytes = password.GetBytes(keysize / 8);
            using (RijndaelManaged symmetricKey = new RijndaelManaged())
            {
                symmetricKey.Mode = CipherMode.CBC;
                using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            byte[] cipherTextBytes = memoryStream.ToArray();
                            return Convert.ToBase64String(cipherTextBytes);
                        }
                    }
                }
            }
        }
    }

    public static string Decrypt(string cipherText, string passPhrase)
    {
        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
        using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
        {
            byte[] keyBytes = password.GetBytes(keysize / 8);
            using (RijndaelManaged symmetricKey = new RijndaelManaged())
            {
                symmetricKey.Mode = CipherMode.CBC;
                using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
                {
                    using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                        {
                            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                        }
                    }
                }
            }
        }
    }

}

}

This DLL was sent to another programmer to use her methods. However, he could not see her methods.

I checked, and the methods don’t show up in the Archive .TLB that he’s using.

  • And that this DLL was sent to another programmer, to use her methods. However he could not visualize the methods of the same.

  • And in the VB Browser Object, the methods do not appear. I wonder if I have any error in the code, so it can n visualize the methods there

  • I get it. did your colleague add all the references to his project? If I’m not mistaken he will have to add the DLL reference and then add the namespace to the project.

  • Yes, it is not able to view the methods even in the VB Object Browser

  • However, on my computer, it works perfectly.

  • Some problem or some he did not do. If it was the code, you would have problems too, agree? I still think there’s some using left for him to do just by seeing the code and the way he’s trampling. You can post his code, his calls and so on...?

  • He is not able to visualize the methods, nor using the VB Object Browser. He did not implement code.

  • I checked, and the methods do not appear in the.TLB file, which he is using. In . DDL all methods appear

Show 3 more comments

1 answer

4


"TLB"? Then he is trying to consume his DLL as an Activex component.

The problem is that the WITH does not support static methods and when your colleague creates the proxy for your DLL the static methods are not included in the proxy.

Remove the keyword Static declaration of your methods and your colleague may consume them as an Activex object.

Tip: if you don’t want to refactor now all your own code that consumes these methods, instead of removing the Static, declare new instance methods that consume these static methods. Sort of:

public class Criptografia 
{
    public static string Encrypt(string plainText, string passPhrase)
    { // ... este é o seu método original
        ...
    }

    public string _Encrypt(string plainText, string passPhrase)
    { // ... este é o novo método, que consome o original.
      // ... é este novo método que o seu colega vai consumir
        return Criptografia.Encrypt(plainText, passPhrase);
    }
    ...
}

Of course, your code can probably get nicer than that and this is just a suggestion to use during testing.

  • I removed the Static method and ran a test. The test worked perfectly( with the expected result), but when building the project, I get the following error: An Object Reference is required for the non-static field, method, or Property. Accuses the lack of Static. It will cause me problems, or I can leave like this?

  • @Renilsonandrade Show the line of code causing this error.

  • And when I call the method( where I removed the statico attribute) Console.Writeline("Its encrypted string and: "); string contract = Encryption.Encrypt(text, key); Console.Writeline(contract); Console.Writeline("");

  • @Renilsonandrade Ôps! The method names were equal. I edited my answer by adding an "underline" at the beginning of the name of one of the methods.

  • It worked perfectly, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.