Click to See Complete Forum and Search --> : Program crashes, oleaut32.dll fault.


pirateninja
March 24th, 2007, 09:59 PM
So heres the code. Just a simple test to load a JPG file and bitblt it to the screen, no errors, program just crashes. I am using the LoadAnImage function found here
http://www.codeguru.com/cpp/g-m/bitmap/article.php/c4935/ . I am almost certain that it is the way I am using the LoadAnImage func that is causing the problem but I don't know how else it would be done.


#include <cstdlib>
#include <iostream>
#include <olectl.h>
#include <ole2.h>

/*
../../Dev-Cpp/lib/libgdi32.a
../../Dev-Cpp/lib/libuser32.a
../../Dev-Cpp/lib/libolepro32.a
../../Dev-Cpp/lib/libuuid.a
../../Dev-Cpp/lib/liboleaut32.a
*/


using namespace std;

HBITMAP LoadAnImage(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;
}


int main(int argc, char *argv[])
{
HBITMAP newimage = LoadAnImage("omg.jpg");
HDC dcmem;
SelectObject(dcmem, newimage);
BitBlt(GetDC(0),0,0,500,500,dcmem,0,0,SRCPAINT);
DeleteDC(dcmem);
DeleteObject(newimage);
system("PAUSE");
return EXIT_SUCCESS;
}


I am using Dev-C++ if that matters.

Zaccheus
March 25th, 2007, 07:10 AM
The LoadAnImage function uses COM.

You need to call CoInitialize (http://msdn2.microsoft.com/en-us/library/ms678543.aspx) at the start of your main function and CoUninitialize (http://msdn2.microsoft.com/en-us/library/ms688715.aspx) at the end of your main function.

pirateninja
March 25th, 2007, 11:04 AM
Thanks. I also found out that I needed OleInitialize and CoUninitialize as well.