This is a question based on C code for Win32. In one of my functions I have the following code:
void SeparateStuff()
{
HGLOBAL hMem;
IStream* pStream;
Status result;
unsigned char* pBytes;
DWORD size;
FILE* fp;
if (hMem = GlobalAlloc(GMEM_MOVEABLE, 0))
{
if (CreateStreamOnHGlobal(hMem, FALSE, &pStream) == S_OK)
{
// save the buffer to a file
result = Scramble(pStream);
pStream->Release();
if (result == Ok)
{
if (pBytes = (unsigned char *) GlobalLock(hMem))
{
size = GlobalSize(hMem);
// pBytes has the buffer now
// in this case write it to a file
if(fp = fopen("c:\\SomeFile.bin", "wb"))
{
fwrite(pBytes, size, 1, fp);
fclose(fp);
}
}
}
}
else
{
printf("error CreateStreamOnHGlobal. Last err: %d\n", GetLastError());
return;
}
}
else
{
printf("error GlobalAlloc. Last err: %d\n", GetLastError());
return;
}
}
In this case the unsigned char* buffer is being saved to a file. Everything is fine. Now, I need to modify this code so the unsigned char* is returned. Something like:
void SeparateStuff(unsigned char* ReturnedBytes)
Where ReturnedBytes will be modified inside the function. I tried copying the pointer, i tried copying the data inside the pointer. I tries a lot of different approaches. It doesn't matter what I do I either get garbage in the calling function or an exception. The calling function (the main() for example) has to pass to SeparateStuff() also an unsigned char*.
How do I do that?
thanks!