Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to translate this code from C# to C++

Status
Not open for further replies.

RedLion

Programmer
Sep 13, 2000
342
NL
Hello, i'm trying to translate this C# code into C++ and I get constantly some exceptions, like on the type byte.

Code:
using System;
using System.Security.Cryptography;
using System.Text;

public static string getMd5hash(string tekst)
{
  byte[] bs1 = ConvertStringToByteArray(tekst);
  return BitConverter.ToString(((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(bs1));
}

Any help is welcome! Thanks in advance!
 
Do you want it in Managed C++ or normal C++? Managed C++ is easier but it has no end of problems if you try to use it with normal C++; the most common being the loader lock problem.
 
Thanks for your quick response xwb!

I just found out that I can't use this code example anymore because the systems where it has to run on don't have the dotnet framework installed on it. It are just WinXP machines.

What is the problem, I have to write a routine that generates hash-codes for each file in the current directory.

First thing I need is a free algoritme to generate hash-codes. I found out that Windows has an API MsiGetFileHash that could generate a hash, but I can't get it to work (it was to long a go I used c++ :-().
If anyone knows an other easier / better API function to generate hash codes, or knows an api to make use of MD5 or any of the familie of popular hash / checksum functions I would love to to hear it!

This is what I already have for the MsiGetFileHash (just simple to start testing):
Code:
#include "stdafx.h"
#include <msi.h>
#include <string>

using namespace std;

string getMyCheckSum(string filename){
	
	//UINT WINAPI MsiGetFileHashA( LPCSTR, DWORD,
	//	PMSIFILEHASHINFO);

	PMSIFILEHASHINFO pHash;
	//pHash.dwFileHashInfoSize = sizeof(pHash);
	LPCTSTR file = (LPCTSTR)filename;
	MsiGetFileHash( file, 0, pHash );

	return pHash;	
}

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Hash file: " << getMyCheckSum("c:\\test\\test.txt") << endl;
	
	return 0;
}
I get still some errors, on for example pHash.

Thanks in advance!
 
There is an MD5 impl in C on
Note that C++ strings get very inefficient if passed by value and should not be set as return values otherwise you will get a crash. Also, if you're coming from c#, remember to delete your allocations otherwise you will end up with a gigantic memory leak. This is a common problem with C#, Java and php coders using C++.

std::string is not the same as CString. LPCTSTR operator on CStrings is the .c_str() method on std::strings. Actually, you could have passed it as a const char*.

CStrings is a MS object. std::string is an ISO object.

Try this
Code:
#include "stdafx.h"
#include <msi.h>
#include <string>
#include <iostream>

using namespace std;

void getMyCheckSum(const string& filename, PMSIFILEHASHINFO pHash)
{
    
    //UINT WINAPI MsiGetFileHashA( LPCSTR, DWORD,
    //    PMSIFILEHASHINFO);

    pHash->dwFileHashInfoSize = sizeof(*pHash);
    MsiGetFileHash( filename.c_str(), 0, pHash );

}

int _tmain(int argc, _TCHAR* argv[])
{
   MSIFILEHASHINFO hash;
   getMyCheckSum("c:\\test\\test.txt", &hash);

   // I don't know what you want to print here
   cout << hash.? << endl;    
    return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top