Click here to Skip to main content
15,891,751 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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()) ;
}


What I have tried:

 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) ;
}
Posted
Updated 29-Nov-17 6:59am
Comments
Richard MacCutchan 29-Nov-17 8:51am    
What is the question?
OriginalGriff 29-Nov-17 10:05am    
And?
What does it do that you didn't expect, or not do that you did?
What help do you need?
Dave Kreskowiak 29-Nov-17 10:45am    
If you're asking other people to do your work for you, you've come to the wrong site. CodeProject isn't a "convert this code for me" site. If you want someone to do your work for you, I suggest Freelancer.com and get out your credit card.

1 solution

I would suggest that you write some very simple test data for your funtions. Try it in your C++ code to see the results, than you can easy debug the C# where the problems are.

Another helpful tip is that you make debug output like with TRACE macro to see what is going on.

One common issue is that the byte size is double than the string length. Than you need to convert strings. Take a look at these articles about encoding and Bitconverter.
 
Share this answer
 
Comments
srilekhamenon 29-Nov-17 20:11pm    
Thank you Sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900