views:

209

answers:

3

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!

A: 

You can use one of the following syntaxes:

unsigned char* SeparateStuff(); //Returns a pointer
void SeparateStuff(unsigned char** ppBuffer); //Pointer to the output buffer

Or in C++:

void SeparateStuff(unsigned char*& pBuffer) //Reference to the output buffer
Naveen
Thank you for the reply. I tried this (the C) approach and inside the code I have:ppBuffer = pBufferor *ppBuffer = *pBuffereither case the returned data is not the same as in pBuffer
wonderer
@wonderer: You need to set *ppBuffer = pBuffer. If you do ppBuffer = pBuffer nothing changes at all when you return. If you do *ppBuffer = *pBuffer, ppBuffer will have the value of the first character and if you take that as a pointer ...
Markus Schnell
Great! thanks for the help.Thanks everyone for the help.
wonderer
@Markus: Thanks for providing the clarification.
Naveen
A: 
SeparateStuff(&ReturnedBytes);

void SeparateStuff(unsigned char** ReturnedBytes)
{
    ...
}
Stephen Doyle
Thank you for the answer.the buffer I need to save is often a few kb's so I don't think it'll fit on a single unsigned char.unless i'm missing something.
wonderer
Ah ok - I mis-unsderstood. I thought that returnedBytes was a length field and not the data itself. Updated the answer to correct this.
Stephen Doyle
A: 

Try this:

void SeparateStuff(unsigned char **handleToBuffer)
{
    ...

    if (*handleToBuffer = (unsigned char *) GlobalLock(hMem))
    {
        ...
    }
    else
    {
        return;
    }
    ...
}

Please make sure that all arguments are properly initialized. Also you have to guarantee that what GlobalLock returns stays there.

Markus Schnell