Converting to FXP, some problems

Guys, not sure if someone could give me a hand. I created some functions to help me converting stuff to the FXP format, so I can use setStateInformation to a Windows VST. But I’m pretty sure I did something wrong, as it crashes at random times. (can’t really debug) Any help would be appreciated. 8)

[code]//-----------------------------------------------------------------------------------------------------------
void getFileToFXP (MemoryBlock& dest, File& fileLoad)
{
dest.setSize (sizeof (fxProgramSet_convert), true);

fxProgramSet_convert* const set = (fxProgramSet_convert*) dest.getData();
set->chunkMagic = vst_swap ('CcnK');
set->byteSize = 0;
set->fxMagic = vst_swap ('FPCh');
set->version = vst_swap (fxbVersionNum);
set->fxID = vst_swap ('82n8');
set->fxVersion = vst_swap (1);
set->numPrograms = vst_swap (512);

MemoryBlock chunk;
fileLoad.loadFileAsData(chunk);
set->chunkSize = vst_swap (chunk.getSize());
dest.append(chunk.getData(),chunk.getSize());

}

//-----------------------------------------------------------------------------------------------------------
void convertDataToFXB (MemoryBlock& dest, const void* data, int sizeInBytes)
{
// First Check if its not a FXP already //
if (sizeInBytes > 28)
{
fxProgramSet_convert* set = (fxProgramSet_convert*) data;
if (set->chunkMagic == vst_swap (‘CcnK’))
{
dest.append((const void*)data,sizeInBytes);
return; // Its already a FXP //
}
}

// Otherwise, convert to FXP //
dest.setSize (sizeof (fxChunkSet_convert), true);

fxChunkSet_convert* const set = (fxChunkSet_convert*) dest.getData();
set->chunkMagic = vst_swap ('CcnK');
set->byteSize = 0;
set->fxMagic = vst_swap ('FBCh');
set->version = vst_swap (fxbVersionNum);
set->fxID = vst_swap ('82n8');
set->fxVersion = vst_swap (1);
set->numPrograms = vst_swap (512);

set->chunkSize = vst_swap (sizeInBytes);
dest.append((const void*)data,sizeInBytes);

}

//-----------------------------------------------------------------------------------------------------------
void getStringToFXP(MemoryBlock& dest, String string)
{
dest.setSize (sizeof (fxProgramSet_convert), true);

fxProgramSet_convert* const set = (fxProgramSet_convert*) dest.getData();
set->chunkMagic = vst_swap ('CcnK');
set->byteSize = 0;
set->fxMagic = vst_swap ('FPCh');
set->version = vst_swap (fxbVersionNum);
set->fxID = vst_swap ('82n8');
set->fxVersion = vst_swap (1);
set->numPrograms = vst_swap (512);

set->chunkSize = vst_swap (string.length());
char* buffer = new char[string.length()];
string.copyToBuffer(buffer,string.length());
dest.append((void*)buffer,string.length());
deleteAndZero(buffer);

}[/code]

Headers

[code]//-----------------------------------------------------------------------------------------------------------
struct fxProgramSet_convert
{
long chunkMagic; // 'CcnK’
long byteSize; // of this chunk, excl. magic + byteSize
long fxMagic; // ‘FxCh’, ‘FPCh’, or 'FBCh’
long version;
long fxID; // fx unique id
long fxVersion;
long numPrograms;
char name[28];
long chunkSize;
};

struct fxChunkSet_convert
{
long chunkMagic; // 'CcnK’
long byteSize; // of this chunk, excl. magic + byteSize
long fxMagic; // ‘FxCh’, ‘FPCh’, or 'FBCh’
long version;
long fxID; // fx unique id
long fxVersion;
long numPrograms;
char future[128];
long chunkSize;
};

//-----------------------------------------------------------------------------------------------------------
#ifdef JUCE_LITTLE_ENDIAN
static long vst_swap (const long x) throw() { return (long) ByteOrder::swap ((uint32) x); }
static float vst_swapFloat (const float x) throw()
{
union { uint32 asInt; float asFloat; } n;
n.asFloat = x;
n.asInt = ByteOrder::swap (n.asInt);
return n.asFloat;
}
#else
#define vst_swap(x) (x)
#define vst_swapFloat(x) (x)
#endif
const int fxbVersionNum = 1;[/code]