Hi,

I am trying to use the libavcodec.dll in my dialog based VC++ program. After a lot of messing around I have a program that successfully links to the dll and the lib file(libavcodec and libavformat) and can run without any errors. I then started to try and encode an audio file.

The below is my entire code so far

Code:
AVCodecContext *avctx;
AVCodec *codec;
avctx = new AVCodecContext;
codec = new AVCodec;
avctx=NULL;
//int codec_id;

HMODULE hDll = LoadLibrary("libavcodec.dll");
   if(hDll== NULL)
   {
	   GetLastError();
AfxMessageBox("Failed loading1");
   }
//(hDll should not be NULL here)
typedef void (avcodec_init)();
avcodec_init* p = (avcodec_init*)GetProcAddress(hDll, "avcodec_init");
if(p==NULL)
AfxMessageBox("Failed loading2");
p();
FreeLibrary(hDll);

HMODULE hDll3 = LoadLibrary("libavcodec.dll");
typedef void (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll3, "avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MP2);
if(p3==NULL)
AfxMessageBox("Failed loading3");
FreeLibrary(hDll3);

codec=(AVCodec *)p3;

HMODULE hDll4 = LoadLibrary("libavcodec.dll");
//(hDll should not be NULL here)
typedef void (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll4, "avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();
FreeLibrary(hDll4);

avctx=(AVCodecContext *)p4;
avctx->bit_rate=64000;
avctx->sample_rate=44100;
avctx->channels=2;

HMODULE hDll2 = LoadLibrary("libavcodec.dll");
//(hDll should not be NULL here)
typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll2, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2((AVCodecContext *)p4,(AVCodec *)p3);
FreeLibrary(hDll2);
The bold part(avctx->......) is where I am getting Unhandled Exception/Access Violation. Up until I got there,I didnt have any errors whatsoever. You will notice that I numbered my pointers, so that gives you an idea as to in which order i proceeded.

Can somebody point out the error of my ways??
Thank you so much