How do I use MFC class CLongBinary with COleVariant? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How do I use MFC class CLongBinary with COleVariant?

I'm working on this project at work: my program will extract data from a Palm Pilot, and enter them into an Access *.mdb file. Everything is working fine up until I have to set images into the database. Based on the documentation I've read on MSDN website, I have to create a CLongBinary object and associate it with the HGLOBAL which is the loaded image, and then create a COleVariant object, passing the CLongBinary object in the constructor. I received a "This program has performed an illegal operation" error. MSVCRT.DLL is the culprit of the error, and upon debugging the program, I found out that there was a call to memcpy() and it was given a null-pointer (char* 0x00000000) as the source to copy memory from. I know that the constructor of COleVariant makes a copy of the object passed to it, which justifies the call to memcpy. I don't understand why it would try to copy from mem address 0x0000000. Am I just using these classes all wrong, or is there a better way to do this? MSDN recommended using CByteArray instead of CLongBinary but it's seem more confusing to me. Any help would be appreciated.

Here's what the code looks like:

HBITMAP hPic = (HBITMAP)LoadImage(NULL,"filename",IMAGE_BITMAP, 0,0,LR_LOADFROMFILE);
CLongBinary clb;
clb.m_hData = hPic;
clb.m_dwDataLength = 294966; //size of the image
COleVariant oleImage(clb); //this is where the program crashes

Comments

  • tly007tly007 Posts: 10Member
    Here's a little change I made to the code. It no longer crashes but it still does not work. After running the program, the image is not copied into the MDB database in the field I specified.

    /////////////////////////////////////////////////////////////
    /*
    Created and opened CDaoDatabase, CDaoTableDef, CDaoRecordset objects. myRS is a CDaoRecordset*.
    Also called myRS0->AddNew() and
    COleVariant bookmark = myRS->GetLastModifiedBookmark();
    myRS->SetBookmark(bookmark);

    HBITMAP hPic = (HBITMAP)LoadImage(NULL,"filename",IMAGE_BITMAP,
    0,0,LR_LOADFROMFILE);
    CLongBinary clb;
    clb.m_hData = hPic;
    //This time I used ::GlobalSize() to determine datalenth instead
    //of explicitly specifing it.
    clb.m_dwDataLength = ::GlobalSize(hPic);
    COleVariant oleImage(clb);
    myRS->SetFieldValue("Picture",oleImage);
    myRS->Update();
    myRS->Close();
    /////////////////////////////////////////////////////////////

    Any ideas?


    To my best friend, Coca-Cola: Much love for you. We go way back...


Sign In or Register to comment.