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

Please Help (Unhandled exception at 0x004199ce in IPDVR.exe: 0xC0000005: Access violation reading location 0x00000000.)

Hi could someone help me out here I don't seem to be able to get my head around this exception I am new to C++ but a quick learner. From what I understand the hex 0xC0000005 indicates a memory issue and 0x0000000 indicates that noting is written to that memory location please if you can explain the exception so I can resolve this my self in the future.
What I am doing is loading a dll and then using function pointers to call the functions from that pointer.
My code is as follows:
This file is loading the Dll's
// LoadDll.cpp : implementation file
//

#include "stdafx.h"
#include "IPDVR.h"
#include "LoadDll.h"


// LoadDll

IMPLEMENT_DYNAMIC(LoadDll, CWnd)
LoadDll::LoadDll()
{
}

LoadDll::~LoadDll()
{
}


BEGIN_MESSAGE_MAP(LoadDll, CWnd)
END_MESSAGE_MAP()


BOOL LoadDll::LoadDLLModules()
{
      //###################################################################################################
      //## LOAD DLL MODULES
      HMODULE hModule = NULL;
                hModule = LoadLibrary("NVS400.dll"); // IP Video Server Driver and codec
      hModule = LoadLibrary("ECPSV.dll"); //Capture board Driver
      if(hModule){
            *(FARPROC*)&m_Uda5Api.Cmn5CreateInstance  = GetProcAddress(hModule,"Cmn5CreateInstance");
      }
      
      IUnknown * pUnknown;
      m_Uda5Api.Cmn5CreateInstance(&pUnknown);
      
      pUnknown->QueryInterface (IID_ICod5,(void**)&m_pCod5Api);
      pUnknown->QueryInterface (IID_INet5,(void**)&m_pNet5Api);
      pUnknown->QueryInterface (IID_ICap5,(void**)&m_pCap5Api);
      pUnknown->QueryInterface (IID_IOvr5,(void**)&m_pOvrApi);
                pUnknown->QueryInterface (IID_IAud5,(void**)&m_pAud5Api);
      pUnknown->Release();
      
    //## END OFF DLL MODULES
      //####################################################################################################
      

      return TRUE;
}
// LoadDll message handlers

This is the header file for LoadDll.ccp

//LoadDll.h
#pragma once
#include "IPDVR.h"
#include "IPDVRDlg.h"

// LoadDll
//Load Dlls

struct UDA5GlobAPI{
      BOOL (__stdcall *Cmn5CreateInstance)(IUnknown **pUnknown);
};        

class LoadDll : public CWnd
{
      DECLARE_DYNAMIC(LoadDll)

public:
      LoadDll();
      virtual ~LoadDll();
    BOOL LoadDLLModules();
      UDA5GlobAPI  m_Uda5Api;
    ICod5*            m_pCod5Api;
      INet5*            m_pNet5Api;
      ICap5*            m_pCap5Api;
      IAud5*            m_pAud5Api;
      IOvr5*            m_pOvrApi;
      
      ICod5* GetCodApi() const { return m_pCod5Api;}
                INet5* GetNetApi() const { return m_pNet5Api;}
      ICap5* GetCapApi() const { return m_pCap5Api;}
      IAud5* GetAudApi() const { return m_pAud5Api;}
      IOvr5* GetOvrApi() const { return m_pOvrApi;}
      
    CMN5_SYSTEM_INFO      m_SysInfo;
      CAP5_BOARD_INFO* m_CapBoardInfo;
      AUD5_BOARD_INFO* m_AudSystemInfo;
      COD5_BOARD_INFO* m_CodSystemInfo;
      OVR5_BOARD_INFO* m_OvrSystemInfo;
      
protected:
      DECLARE_MESSAGE_MAP()
};

 LoadDll* GetLoadDll();

This part works fine and the Dlls load in my project.
The next process is to use the function pointers in any *.cpp file within the class of that file so my next peice of code looks like this:
This is the main form

#include "stdafx.h"
#include "LoadDll.h"
#include "IPDVR.h"
#include "IPDVRDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int totalboards;
GetSN getSN; //GetSN.h THis loads serial number of hardware
RegistrySettings m_RegSettings; //RegistrySettings.h Loads preset registry settings

LoadDll m_LoadDlls;
LoadDll* GetLoadDll() { return &m_LoadDlls;}



OvrControl m_OvrControl;

// CIPDVRDlg dialog



CIPDVRDlg::CIPDVRDlg(CWnd* pParent /*=NULL*/)
      : CBDialog(CIPDVRDlg::IDD, pParent)
{
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
      if(!m_RegSettings.LoadRegSetting())
          m_RegSettings.DefaultSetting();
}

void CIPDVRDlg::DoDataExchange(CDataExchange* pDX)
{
      CBDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CIPDVRDlg, CBDialog)
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_WM_DESTROY()
      ON_WM_TIMER()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// CIPDVRDlg message handlers


BOOL CIPDVRDlg::OnInitDialog()
{
      CBDialog::OnInitDialog();
   // m_setting_next = m_setting;
      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
//      ShowWindow(SW_MAXIMIZE);
      SetBitmapStyle(StyleCenter); //Center Main Dialog Bitmap to center
      SetBitmap(IDB_BMP_BACKGROUND); //Load Main InterFace Bitmap
   
      m_KillEvent=CreateEvent(0,0,0,0);
      // TODO: Add extra initialization here
      m_LoadDlls.LoadDLLModules(); //Load Driver DDLs <-- This is where the Dlls are loaded from LoadDll.cpp
      GetDriverInformation(); //Find Driver Information
      InitializeDriver(); //Initialize Drivers
      StartDriver(); //Start Drivers
      m_OvrControl.OvrSetup();//Start Overlay  <---- This is where I call the class OvrControl
      return TRUE;  // return TRUE  unless you set the focus to a control
}


// OvrControl.cpp : implementation file
//

#include "stdafx.h"
#include "LoadDll.h"
#include "IPDVR.h"
#include "IPDVRDlg.h"
#include "OvrControl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// OvrControl

LoadDll* pLoadDlls = GetLoadDll();
//ICap5* m_pCap5Api = pLoadDlls->GetCapApi();
IOvr5* pOvr5 = pLoadDlls->GetOvrApi();

CMN5_SYSTEM_INFO      m_SysInfo;

OvrControl::OvrControl()
{
}

OvrControl::~OvrControl()
{
}

BEGIN_MESSAGE_MAP(OvrControl, CWnd)
      ON_WM_CREATE()
END_MESSAGE_MAP()


BOOL OvrControl::OvrSetup()
{
      
      pOvr5->Ovr5SetScreenMode(0,OVR5_OSM_SCREEN_MODE_M720);   <-- this is where the Unhandled exception happens
      return FALSE;
}

Header file for OvrControl.cpp

#pragma once
#include "LoadDll.h"

// OvrControl


class OvrControl : public CWnd
{
      //DECLARE_DYNAMIC(OvrControl)

public:
      OvrControl();
      virtual ~OvrControl();
      CWnd* m_pParent;
      BOOL OvrSetup();

      
//CMN5_SYSTEM_INFO      m_SysInfo;
protected:
      
    DWORD g_OvrVideoFormat;
      DECLARE_MESSAGE_MAP()
};

I have been trying to find a soulution for the past few days please some help as soon as posibile
Thanks
0
nchannon
Asked:
nchannon
2 Solutions
 
mahesh1402Commented:
I think the problem is that you aren't initializing the C run-time library in your DLL, so your global objects aren't getting constructed.

Knowledge base article 94248 describes how to call _CRT_INIT from your DllEntryPoint to initialize your routine.

http://support.microsoft.com/default.aspx?scid=kb;en-us;94248 <====

Also try calling 'CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)' in you code before using the common controls or in  InitInstance  of ur application.


MAHESH
0
 
nchannonAuthor Commented:
Hi Mahesh,
 ok the Dll is load fine and I can call all functions from it. for example if I use

 m_LoadDlls.m_pOvr5Api->Ovr5SetScreenMode(0,OVR5_OSM_SCREEN_MODE_M720);  

from my main form IPDVRDlg.cpp all works fine. so basically I have called my function pointer from LoadDll.cpp & h and to use my pointer in IPDVRDlg.cpp i use the following:
// IPDVRDlg.cpp : implementation file

#include "LoadDll.h"
LoadDll m_LoadDlls;
LoadDll* GetLoadDll() { return &m_LoadDlls;}

then I can use any of my function pointers decleared in LoadDll.h

like this

 m_LoadDlls.m_pOvr5Api->Ovr5SetScreenMode(0,OVR5_OSM_SCREEN_MODE_M720);  

Now my problem begins when I try and use the same function pointer m_pOvr5Api-> in another class on OvrControl.cpp I had no access to my function pointers untill I used:
LoadDll* pLoadDlls = GetLoadDll();
IOvr5* pOvr5 = pLoadDlls->GetOvrApi();

when I do this in anyother *.cpp file I get access to my pointers decleared in LoadDll.h but when the code executes
BOOL OvrControl::OvrSetup()
{

      pOvr5->Ovr5SetScreenMode(0,OVR5_OSM_SCREEN_MODE_M720); <-- this point I get the Unhandled exception and only at run time when I just compile the application compiles fine.
0
 
DanRollinsCommented:
THe Constructor for LoadDll object might not be initializing the various pointers to NULL.  Make sure that it does that.  For instance:

LoadDll::LoadDll() {
     m_pCod5Api= 0;
     m_pNet5Api= 0;
     m_pCap5Api= 0;
     m_pAud5Api= 0;
     m_pOvrApi= 0;
}

Then before you acesss any of these pointers, use an ASSERT to validate them  For instance:

BOOL OvrControl::OvrSetup()
{
     ASSERT(pOvr5);
     pOvr5->Ovr5SetScreenMode(0,OVR5_OSM_SCREEN_MODE_M720);

... etc....

This will tell you right away if your pointers have been initialized... no guessing as to the arbitrary global-object initialization sequence.  

=-=-=-=-=-=-=-=-=-=-=-=
Incidently, the specific error message you are seeing indicates that something tried to read data that was pointed to by a NULL pointer.  That is strong evidence that something that assigns pointer values did not succeed in doing correct inititialization.  

I'd put a breakpoint in the code of you LoadDll function and singlestep through that code to see why the pointers are being left NULL.

-- Dan
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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