bool CRegeditExport::ExportBinaryValue( IN const LPBYTE lpBinValue, IN const DWORD nBinValueSize, IN const BYTE nRegType, IN CString strValueName, OUT CString &strRetVal)
{
ASSERT(AfxIsValidAddress(lpBinValue, nBinValueSize));
ASSERT(AfxIsValidString(strValueName));
ASSERT(_tcslen(strValueName));
const DWORD nBufCount = _tcslen(strValueName) + _tcslen(_T("\"\"=hex:(?)\r\n")) + (nBinValueSize * 3 ) + ((nBinValueSize * 6) / 18) + 9 ;
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('@')) )
{
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 );
if(lpBinValue[i] <= 0x0f )
{
hexBuf[1] = hexBuf[0];
hexBuf[0] = _T('0');
}
hexBuf[2] = 0;
buf[nBufCurPos++] = hexBuf[0];
buf[nBufCurPos++] = hexBuf[1];
if( (i + 1) < nBinValueSize)
{
buf[nBufCurPos++] = _T(',');
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)
{
uint nBufCount = (uint)(strValueName.Length + "\"\"=hex:(?)\r\n".Length + (nBinValueSize * 3 ) + ((nBinValueSize * 6) / 18) + 9) ;
char[] buf = new char[nBufCount + 1];
char[] hexBuf = new char[9];
string strFormatValueName;
if( (strValueName.Length < 2) && (strValueName[0] == '@') )
{
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 );
if(lpBinValue[i] <= 0x0f )
{
hexBuf[1] = hexBuf[0];
hexBuf[0] = _T('0');
}
hexBuf[2] = 0;
buf[nBufCurPos++] = hexBuf[0];
buf[nBufCurPos++] = hexBuf[1];
if( (i + 1) < nBinValueSize)
{
buf[nBufCurPos++] = ',';
if( (nBufCurPos % 77) < 3 )
{
buf[nBufCurPos++] = '\\';
buf[nBufCurPos++] = '\r';
buf[nBufCurPos++] = '\n';
buf[nBufCurPos++] = ' ';
buf[nBufCurPos++] = ' ';
}
}
else
{
buf[nBufCurPos++] = '\r';
buf[nBufCurPos++] = '\n';
}
}
if(!(buf[nBufCurPos]) && (buf[nBufCurPos - 1] != _T('\n') ) )
{
buf[nBufCurPos++] = '\r';
buf[nBufCurPos++] = '\n';
}
buf[nBufCurPos++] = 0;
strRetVal = buf;
return (nBinValueSize < strRetVal.Length) ;
}