srilekhamenon Ответов: 1

Преобразуйте следующий код C++ code в C#


bool CRegeditExport::ExportBinaryValue( IN const LPBYTE lpBinValue, IN const  DWORD nBinValueSize, IN const BYTE nRegType, IN CString strValueName,  OUT CString &strRetVal)
{
	//binary is somewhat different 
	//we also have to keep 80 chars per line limit char including the value name 
	/*
	if a value name is very long then it is terminated after the first byte is written	
	"Converts an integer to a string. More secure versions of these functions are available; see _itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s. Converts an integer to a string. More secure versions of these functions are available; see _itoa_s"=hex:12,\
	22,2a,1a,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,\
	aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,c0,12,22,2a,1a,aa,aa,aa,aa,aa,aa,aa,aa,aa,\
	aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,\
	aa,aa,aa,aa,aa,aa,aa,aa,aa,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,c0
	*/

	//	ASSERT( CString(strValueName) != _T("DispFileName") );

	ASSERT(AfxIsValidAddress(lpBinValue, nBinValueSize));
	ASSERT(AfxIsValidString(strValueName));
	ASSERT(_tcslen(strValueName));//should be "@" when default value 
	const DWORD nBufCount = _tcslen(strValueName) + _tcslen(_T("\"\"=hex:(?)\r\n"))  + (nBinValueSize   * 3 /*each byte x 2 + comma */ ) + ((nBinValueSize *  6) / 18)  + 9/* number of lines for end  + crls*/ ;
	//allocate the buufer
	TCHAR *buf = new TCHAR[nBufCount + 1];
	ZeroMemory(buf, sizeof(TCHAR) * (nBufCount + 1));
	TCHAR hexBuf[9] = {0};
	CString strFormatValueName;

	if( (_tcslen(strValueName) < 2) && (strValueName[0] == _T('@')) )
	{
		//case of default value 
		strFormatValueName = _T("@");
	}
	else
	{
		strValueName.Replace(_T("\\"), _T("\\\\"));
		strValueName.Replace(_T("\""), _T("\\\""));

		strFormatValueName.Format(_T("\"%s\""), strValueName);
	}


	if(nRegType == REG_BINARY)
	{
		_stprintf(buf, _T("%s=hex:"), (LPCTSTR) strFormatValueName );
	}
	else
	{
		_stprintf(buf, _T("%s=hex(%x):"), (LPCTSTR) strFormatValueName, (nRegType & 0x0F) );
	}

	DWORD nBufCurPos = _tcslen(buf);


	for(DWORD   i =0; i < nBinValueSize; i++)
	{
		_itot( lpBinValue[i],hexBuf, 16  );

		//now if byte x it less than 0x0f we have prepend something to string
		if(lpBinValue[i] <= 0x0f )
		{			
			hexBuf[1] = hexBuf[0];
			hexBuf[0] = _T('0'); //swap and put one zero for buf before so 'a' becomes '0a'
		}

		hexBuf[2] = 0; //need only first two bytes

		buf[nBufCurPos++] = hexBuf[0];// nBufCurPos++;
		buf[nBufCurPos++] = hexBuf[1]; //nBufCurPos++;

		if( (i + 1) < nBinValueSize)
		{
			buf[nBufCurPos++] = _T(',');

			//check for line end and put crlf 
			if( (nBufCurPos % 77) < 3   )
			{
				buf[nBufCurPos++] = _T('\\'); 
				buf[nBufCurPos++] = _T('\r'); 
				buf[nBufCurPos++] = _T('\n'); 
				buf[nBufCurPos++] = _T(' '); 
				buf[nBufCurPos++] = _T(' '); 

			}
		}
		else
		{
			buf[nBufCurPos++] = _T('\r'); 
			buf[nBufCurPos++] = _T('\n'); 			
		}	

		ASSERT(nBufCurPos < nBufCount); 
	}

	if(!(buf[nBufCurPos]) &&  (buf[nBufCurPos - 1] != _T('\n') ) )
	{
		buf[nBufCurPos++] = _T('\r'); 
		buf[nBufCurPos++] = _T('\n'); 
	}

	buf[nBufCurPos++] = 0; 

	strRetVal = buf;
	delete [] buf;
	return (nBinValueSize < strRetVal.GetLength()) ;
}


Что я уже пробовал:

 bool ExportBinaryValue( byte[] lpBinValue, uint nBinValueSize, byte[] nRegType, string strValueName,out  string strRetVal)
{
	//binary is somewhat different 
	//we also have to keep 80 chars per line limit char including the value name 
	/*
	if a value name is very long then it is terminated after the first byte is written	
	"Converts an integer to a string. More secure versions of these functions are available; see _itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s. Converts an integer to a string. More secure versions of these functions are available; see _itoa_s"=hex:12,\
	22,2a,1a,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,\
	aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,c0,12,22,2a,1a,aa,aa,aa,aa,aa,aa,aa,aa,aa,\
	aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,\
	aa,aa,aa,aa,aa,aa,aa,aa,aa,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,\
	cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,cc,c0
	*/

	//	ASSERT( CString(strValueName) != _T("DispFileName") );

    //ASSERT(AfxIsValidAddress(lpBinValue, nBinValueSize));
    //ASSERT(AfxIsValidString(strValueName));
    //ASSERT(_tcslen(strValueName));//should be "@" when default value 
	 uint nBufCount = (uint)(strValueName.Length + "\"\"=hex:(?)\r\n".Length  + (nBinValueSize   * 3 /*each byte x 2 + comma */ ) + ((nBinValueSize *  6) / 18)  + 9)/* number of lines for end  + crls*/ ;
	//allocate the buufer
	char[] buf = new char[nBufCount + 1];
	//ZeroMemory(buf, sizeof(TCHAR) * (nBufCount + 1));
	char[] hexBuf = new char[9];
	string strFormatValueName;

	if( (strValueName.Length < 2) && (strValueName[0] == '@') )
	{
		//case of default value 
		strFormatValueName ="@";
	}
	else
	{
		strValueName.Replace("\\", "\\\\");
		strValueName.Replace("\"", "\\\"");

        strFormatValueName = string.Format("\"{0}\"", strValueName);
	}


	if(nRegType == REG_BINARY)
	{
		_stprintf(buf, _T("%s=hex:"), (LPCTSTR) strFormatValueName );
	}
	else
	{
		_stprintf(buf, _T("%s=hex(%x):"), (LPCTSTR) strFormatValueName, (nRegType & 0x0F) );
	}

	int  nBufCurPos = buf.Count();


    for (uint i = 0; i < nBinValueSize; i++)
	{
		_itot( lpBinValue[i],hexBuf, 16  );

		//now if byte x it less than 0x0f we have prepend something to string
		if(lpBinValue[i] <= 0x0f )
		{			
			hexBuf[1] = hexBuf[0];
			hexBuf[0] = _T('0'); //swap and put one zero for buf before so 'a' becomes '0a'
		}

		hexBuf[2] = 0; //need only first two bytes

		buf[nBufCurPos++] = hexBuf[0];// nBufCurPos++;
		buf[nBufCurPos++] = hexBuf[1]; //nBufCurPos++;

		if( (i + 1) < nBinValueSize)
		{
			buf[nBufCurPos++] = ',';

			//check for line end and put crlf 
			if( (nBufCurPos % 77) < 3   )
			{
				buf[nBufCurPos++] = '\\'; 
				buf[nBufCurPos++] = '\r'; 
				buf[nBufCurPos++] = '\n'; 
				buf[nBufCurPos++] = ' '; 
				buf[nBufCurPos++] = ' '; 

			}
		}
		else
		{
			buf[nBufCurPos++] = '\r'; 
			buf[nBufCurPos++] = '\n'; 			
		}	

		//ASSERT(nBufCurPos < nBufCount); 
	}

	if(!(buf[nBufCurPos]) &&  (buf[nBufCurPos - 1] != _T('\n') ) )
	{
		buf[nBufCurPos++] = '\r'; 
		buf[nBufCurPos++] = '\n'; 
	}

	buf[nBufCurPos++] = 0; 

	strRetVal = buf;
	
	return (nBinValueSize < strRetVal.Length) ;
}

Richard MacCutchan

В чем же вопрос?

OriginalGriff

И что же?
Что он делает такого, чего вы не ожидали, или не делает того, что вы сделали?
Какая помощь вам нужна?

Dave Kreskowiak

Если вы просите других людей сделать вашу работу за вас, вы пришли не на тот сайт. CodeProject-это не сайт "конвертируйте этот код для меня". Если вы хотите, чтобы кто-то делал вашу работу за вас, я предлагаю Freelancer.com и достань свою кредитную карточку.

1 Ответов

Рейтинг:
9

KarstenK

Я бы посоветовал вам написать несколько очень простых тестовых данных для ваших функций. Попробуйте это в своем коде C++, чтобы увидеть результаты, чем вы можете легко отладить C#, где есть проблемы.

Еще один полезный совет заключается в том, что вы делаете отладочный вывод, как с помощью макроса трассировки, чтобы увидеть, что происходит.

Одна из распространенных проблем заключается в том, что размер байта вдвое больше длины строки. Чем вам нужно конвертировать строки. Взгляните на эти статьи о кодирование и Метода bitconverter.


srilekhamenon

Благодарю вас сэр