简体   繁体   English

如何将此C#代码转换为C ++ / CLI

[英]How can convert this C# code to C++/CLI

How can I convert this segment of C# code to C++/CLI: 如何将此段C#代码转换为C ++ / CLI:

protected string GetMD5HashFromFile(string fileName)
{
  FileStream file = new FileStream(fileName, FileMode.Open);
  MD5 md5 = new MD5CryptoServiceProvider();
  byte[] retVal = md5.ComputeHash(file);
  file.Close();
  ASCIIEncoding enc = new ASCIIEncoding();
  return enc.GetString(retVal);
}

Specially this part byte[] retVal = md5.ComputeHash(file); 特别是这部分byte[] retVal = md5.ComputeHash(file);

Making liberal use of the stack semantics available in C++/CLI to automatically dispose objects. 自由使用C ++ / CLI中可用的堆栈语义来自动处理对象。 An emulation of the Holy C++ RAII pattern, the object gets disposed even when the code throws an exception. 仿真Holy C ++ RAII模式,即使代码抛出异常,对象也会被处理掉。 Think of it as the compiler automatically generating the C# using statement. 可以把它想象成编译器自动生成C# using语句。 Look like this: 看起来像这样:

using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;
using namespace System::Text;

ref class Example {
protected:
    String^ GetMD5HashFromFile(String^ fileName)
    {      
        FileStream file(fileName, FileMode::Open);
        MD5CryptoServiceProvider md5;
        array<Byte>^ retVal = md5.ComputeHash(%file);
        return Convert::ToBase64String(retVal);
    }
};

There's an example of using the crypto service provider from C++ to generate an MD5 in the top answer to this question: 有一个使用C ++加密服务提供程序在这个问题的最佳答案中生成MD5的示例:

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/c0f97655-d953-4e3f-82b9-b70edaf1625b/ http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/c0f97655-d953-4e3f-82b9-b70edaf1625b/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM