Function to load image compiles, but causes error

Yeah, I found this function on the web to load an image directly from the HDD at runtime, and I got it to compile by linking oleaut32.a and uuid.a, but its causing errors and crashing when its used. Can anyone figure out why?

[code]
HBITMAP Loadimage(char * filename)
{
// Use IPicture stuff to use JPG / GIF files
IPicture* p;
IStream* s;
IPersistStream* ps;
HGLOBAL hG;
void* pp;
FILE* fp;

// Read file in memory
fp = fopen(filename,"rb");
if (!fp)
return NULL;
fseek(fp,0,SEEK_END);
int fs = ftell(fp);
fseek(fp,0,SEEK_SET);
hG = GlobalAlloc(GPTR,fs);


if (!hG)
{
fclose(fp);
return NULL;
}
pp = (void*)hG;
fread(pp,1,fs,fp);
fclose(fp);

// Create an IStream so IPicture can
// CreateStreamOnHGlobal(hG,false,&s);
if (!s)
{
GlobalFree(hG);
return NULL;
}

OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);

if (!p)
{
s->Release();
GlobalFree(hG);
return NULL;
}
s->Release();
GlobalFree(hG);

HBITMAP hB = 0;
p->get_Handle((unsigned int*)&hB);

// Copy the image. Necessary, because upon p's release,
// the handle is destroyed.
HBITMAP hBB = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,
LR_COPYRETURNORG);

p->Release();
return hBB;
}
[/code]

Comments

  • :
    : [code]
    : HBITMAP Loadimage(char * filename)
    : {
    : // Use IPicture stuff to use JPG / GIF files
    : pp = (void*)hG;
    [red]^^^ can't do this! hG is a handle to some global memory, that may
    or may not be flushed to disk. You have to lock it in memory.[/red]
    pp = GlobalLock(hG);

    // do something.
    // now unlock the memory before deleting it
    GlobalUnlock(hG);
    : [/code]
    :

  • : :
    : : [code]
    : : HBITMAP Loadimage(char * filename)
    : : {
    : : // Use IPicture stuff to use JPG / GIF files
    : : pp = (void*)hG;
    : [red]^^^ can't do this! hG is a handle to some global memory, that may
    : or may not be flushed to disk. You have to lock it in memory.[/red]
    : pp = GlobalLock(hG);
    :
    : // do something.
    : // now unlock the memory before deleting it
    : GlobalUnlock(hG);
    : : [/code]
    : :
    :
    :
    Sorry, I have NO idea how to fix that... any example code?? Please??

    Thanks for your time - you are appreciated by a lot of people.

  • : :
    : Sorry, I have NO idea how to fix that... any example code?? Please??
    :

    read my post again! delete the first line that I said had an error and add the other two lines.
  • [b][red]This message was edited by thegreenstar at 2005-2-28 22:19:56[/red][/b][hr]
    : :
    : : [code]
    : : HBITMAP Loadimage(char * filename)
    : : {
    : : // Use IPicture stuff to use JPG / GIF files
    : : pp = (void*)hG;
    : [red]^^^ can't do this! hG is a handle to some global memory, that may
    : or may not be flushed to disk. You have to lock it in memory.[/red]
    : pp = GlobalLock(hG);
    :
    : // do something.
    : // now unlock the memory before deleting it
    : GlobalUnlock(hG);
    : : [/code]
    : :
    :
    :
    Now I feel stupid. But actually, I changed it (deleted
    [source]
    pp = (void*)hG;
    //and replaced it with
    pp = GlobalLock

    //then placed
    GlobalUnlock(hG);
    //right before GlobalFree(hG)

    [/source]

    It still gives me the error though, and its during the OleLoadImage function call. Why would it do that? Yargh! Please help me, I am getting REALLY frusterated at it. :(

Sign In or Register to comment.

Howdy, Stranger!

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

Categories