【问题标题】:Trying to create a .NET DLL to be used with Non-.NET Application尝试创建与非 .NET 应用程序一起使用的 .NET DLL
【发布时间】:2011-02-05 06:22:18
【问题描述】:

我正在尝试创建一个 .NET DLL,以便我可以在我的非 .NET 应用程序中使用加密函数。

到目前为止,我已经使用以下代码创建了一个类库:

namespace AESEncryption
{
    public class EncryptDecrypt
    {
        private static readonly byte[] optionalEntropy = { 0x21, 0x05, 0x07, 0x08, 0x27, 0x02, 0x23, 0x36, 0x45, 0x50 };

        public interface IEncrypt
        {
            string Encrypt(string data, string filePath);
        };

        public class EncryptDecryptInt:IEncrypt
        {

            public string Encrypt(string data, string filePath)
            {
                byte[] plainKey;

                try
                {
                    // Read in the secret key from our cipher key store
                    byte[] cipher = File.ReadAllBytes(filePath);
                    plainKey = ProtectedData.Unprotect(cipher, optionalEntropy, DataProtectionScope.CurrentUser);

                    // Convert our plaintext data into a byte array

                    byte[] plainTextBytes = Encoding.ASCII.GetBytes(data);

                    MemoryStream ms = new MemoryStream();

                    Rijndael alg = Rijndael.Create();

                    alg.Mode = CipherMode.CBC;
                    alg.Key = plainKey;
                    alg.IV = optionalEntropy;

                    CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

                    cs.Write(plainTextBytes, 0, plainTextBytes.Length);

                    cs.Close();

                    byte[] encryptedData = ms.ToArray();

                    return Convert.ToString(encryptedData);
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }
}

在我的 VC++ 应用程序中,我使用 #import 指令导入从 DLL 创建的 TLB 文件,但唯一可用的函数是 _AESEncryption 和 LIB_AES 等

我没有看到界面或 Encrypt 函数。

当我尝试实例化以便调用 VC++ 程序中的函数时,我使用此代码并收到以下错误:

HRESULT hr = CoInitialize(NULL);

IEncryptPtr pIEncrypt(__uuidof(EncryptDecryptInt));

错误 C2065:“IEncryptPtr”:未声明的标识符

错误 C2146:语法错误:缺少 ';'在标识符“pIEncrypt”之前

【问题讨论】:

    标签: c# .net c++ dll dllimport


    【解决方案1】:

    您似乎没有将接口标记为通过 COM 可见。我希望看到类似的东西:

    namespace AESEncryption
    {
        [Guid("[a new guid for the interface]")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IEncrypt        {
            string Encrypt(string data, string filePath);
        }
    
        [Guid("[a new guid for the class]")]
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        public class EncryptDecryptInt : IEncrypt
        {
            public string Encrypt(string data, string filePath)
            {
                // etc.
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      无需额外工作,C# .Net 库就要求宿主应用程序使用 .net 运行时环境。

      其实这里有一篇文章描述了如何从非托管代码调用.net dll:

      http://support.microsoft.com/kb/828736

      【讨论】:

      • 但是微软没有让它向后兼容,因此 COM 兼容性?
      • 你说得对,忘记了。这是向您展示如何做到这一点的文章。
      【解决方案3】:

      看看this 的问题。 最简单的选择是使用托管 C++ 创建混合模式 DLL。

      如果您需要加密库,为什么不使用 OpenSSL? 与依赖 .NET 相比,它将为您提供更好的性能和更少的依赖。

      【讨论】:

      • 或者只是 CryptoAPI 就足够了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2011-02-20
      • 2020-10-15
      相关资源
      最近更新 更多