• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1192
  • Last Modified:

How do I assign a LPBYTE to a a unsigned char* VARIABLE?

I'm getting the following error when I try to assign the LPBYT to a unsigned char*.
Unhandled exception at 0x1300b38a (UnityExtProperty.dll) in mmc.exe: 0xC0000005: Access violation writing location 0x00000000.
From the follwoing code:


            VariantArrayToBytes(var, &pByte, &plcb);
            pBlob->pData = pByte;
            DecodeAsnGroup(pBlob, &newGroupData);
            VariantClear(&var);



typedef struct
{
      Blob *            pBlob;
      AsnNode *      pNode;
      Bit32            nReadOffset;
} AsnData;

typedef struct
{
      Bit8*      pData;
      Int32      nLength;
      Int32      nSize;
} Blob;

typedef unsigned char                  Bit8;

HRESULT CPropSheetExtension::VariantArrayToBytes(VARIANT Variant,
                            LPBYTE *ppBytes,  
                            DWORD *pdwBytes)

{
    if(!(Variant.vt & VT_ARRAY) ||
        !Variant.parray ||
        !ppBytes ||
        !pdwBytes)
    {
        return E_INVALIDARG;
    }

    *ppBytes = NULL;
    *pdwBytes = 0;

    HRESULT hr = E_FAIL;
    SAFEARRAY *pArrayVal = NULL;
    CHAR HUGEP *pArray = NULL;
   
    // Retrieve the safe array.
    pArrayVal = Variant.parray;
    DWORD dwBytes = pArrayVal->rgsabound[0].cElements;
    *ppBytes = (LPBYTE)AllocADsMem(dwBytes);
    if(NULL == *ppBytes)
    {
        return E_OUTOFMEMORY;
    }

    hr = SafeArrayAccessData(pArrayVal, (void HUGEP * FAR *) &pArray);
    if(SUCCEEDED(hr))
    {
        // Copy the bytes to the safe array.
        CopyMemory(*ppBytes, pArray, dwBytes);
        SafeArrayUnaccessData( pArrayVal );
        *pdwBytes = dwBytes;
    }
   
    return hr;
}
0
lapucca
Asked:
lapucca
  • 2
  • 2
  • 2
1 Solution
 
grg99Commented:
I dont see where your first four lines fit into the other code.  Are they supposed to?

In general you should check an pointer you're unsure of to make sure it isnt NULL.  The exception you're getting is just like the one you get when you use a NULL pointer.

0
 
lapuccaAuthor Commented:
The first 4 lines is from a method that calls the the VariantArrayToBytes().  It's the 2nd line that got me the error,           pBlob->pData = pByte;

I thought that   VariantArrayToBytes().  is supposed to allocate and return the pByte pointer for me?  It is declare and initilize to null.
0
 
jkrCommented:
I agree that the function that copies the VARIANT probably is not the one causing the problem. Wheat happens if you use

          VariantArrayToBytes(var, &pByte, &plcb);
          if (!pBlob) MessageBox(NULL,"No valid BLOB!","ERROR",MB_OK);
          pBlob->pData = pByte;
          DecodeAsnGroup(pBlob, &newGroupData);
          VariantClear(&var);

?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
grg99Commented:
you better check pBlob isnt NULL.  I don't see where pblob is set!

How about you show us ALL the code, in context.





0
 
lapuccaAuthor Commented:
This is the declaration for pBlob.  I guess this might be causing the problem.  How should I initilize and declare pBlob?
Thank you.
      Blob* pBlob = NULL;
0
 
jkrCommented:
>>How should I initilize and declare pBlob?

Well, usually like

          VariantArrayToBytes(var, &pByte, &plcb);
          pBlob = new Blob;
          pBlob->pData = pByte;
          DecodeAsnGroup(pBlob, &newGroupData);
          VariantClear(&var);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now