Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dll Help......

Dll Help......

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++performance
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    keb
    wrote on last edited by
    #1

    I need a help in DLL area. My co-worker is passing me a Carray data type into my dll and that seem to cause a "access violation error" in his exe when he tries to delete that Carray value. When I look into his program, it seem to failing at FreeLibrary call. It was detecting an memory error there. So, I made up a sample program to talk to the dll and it was fine. He is using sometype of Active X component and I am not sure if that may have something to do with it but I was going to see if any of you guys had a problem with passing MFC datatype into Dll. I tried it through MFC extention dll and regular Dll and both failed in same way. Any information would help me. Thanks! My DLL side: extern "C" __declspec(dllexport) int InitProcess(LPSTR lpPortName, HWND hWnd, CArray *pMsg) { pMsg->ADD(type); return 1; } Co-worker's Main side: CArray *m_pCmdArray; m_pCmdArray = new CArray (); typedef int (* LPFNINITPROCESS)(LPCSTR,HWND, CArray *); LPFNINITEPROCESS lpfnInit; lpfnInit lpfnInitProcess; hLib = LoadLibrary("MYDLL.dll"); lpfnInitProcess((LPCSTR)COMPORT, this->m_hWnd, m_pCmdArray); FreeLibrary(hLib); <------------------ memory leak detect; delete m_pCmdArray; <------------------ access viloation error... yack..

    R A 2 Replies Last reply
    0
    • K keb

      I need a help in DLL area. My co-worker is passing me a Carray data type into my dll and that seem to cause a "access violation error" in his exe when he tries to delete that Carray value. When I look into his program, it seem to failing at FreeLibrary call. It was detecting an memory error there. So, I made up a sample program to talk to the dll and it was fine. He is using sometype of Active X component and I am not sure if that may have something to do with it but I was going to see if any of you guys had a problem with passing MFC datatype into Dll. I tried it through MFC extention dll and regular Dll and both failed in same way. Any information would help me. Thanks! My DLL side: extern "C" __declspec(dllexport) int InitProcess(LPSTR lpPortName, HWND hWnd, CArray *pMsg) { pMsg->ADD(type); return 1; } Co-worker's Main side: CArray *m_pCmdArray; m_pCmdArray = new CArray (); typedef int (* LPFNINITPROCESS)(LPCSTR,HWND, CArray *); LPFNINITEPROCESS lpfnInit; lpfnInit lpfnInitProcess; hLib = LoadLibrary("MYDLL.dll"); lpfnInitProcess((LPCSTR)COMPORT, this->m_hWnd, m_pCmdArray); FreeLibrary(hLib); <------------------ memory leak detect; delete m_pCmdArray; <------------------ access viloation error... yack..

      R Offline
      R Offline
      Rickard Andersson20
      wrote on last edited by
      #2

      keb wrote: extern "C" __declspec(dllexport) int InitProcess(LPSTR lpPortName, HWND hWnd, CArray *pMsg) { pMsg->ADD(type); return 1; } Co-worker's Main side: CArray *m_pCmdArray; m_pCmdArray = new CArray (); typedef int (* LPFNINITPROCESS)(LPCSTR,HWND, CArray *); LPFNINITEPROCESS lpfnInit; lpfnInit lpfnInitProcess; hLib = LoadLibrary("MYDLL.dll"); lpfnInitProcess((LPCSTR)COMPORT, this->m_hWnd, m_pCmdArray); FreeLibrary(hLib); <------------------ memory leak detect; delete m_pCmdArray; <------------------ access viloation error... yack.. This is not a paste and cut example code snippet, I can't see any call to GetProcAddress() :) Give us the whole code... perhaps it has to do with MFC and regular DLLs... hm.. Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

      1 Reply Last reply
      0
      • K keb

        I need a help in DLL area. My co-worker is passing me a Carray data type into my dll and that seem to cause a "access violation error" in his exe when he tries to delete that Carray value. When I look into his program, it seem to failing at FreeLibrary call. It was detecting an memory error there. So, I made up a sample program to talk to the dll and it was fine. He is using sometype of Active X component and I am not sure if that may have something to do with it but I was going to see if any of you guys had a problem with passing MFC datatype into Dll. I tried it through MFC extention dll and regular Dll and both failed in same way. Any information would help me. Thanks! My DLL side: extern "C" __declspec(dllexport) int InitProcess(LPSTR lpPortName, HWND hWnd, CArray *pMsg) { pMsg->ADD(type); return 1; } Co-worker's Main side: CArray *m_pCmdArray; m_pCmdArray = new CArray (); typedef int (* LPFNINITPROCESS)(LPCSTR,HWND, CArray *); LPFNINITEPROCESS lpfnInit; lpfnInit lpfnInitProcess; hLib = LoadLibrary("MYDLL.dll"); lpfnInitProcess((LPCSTR)COMPORT, this->m_hWnd, m_pCmdArray); FreeLibrary(hLib); <------------------ memory leak detect; delete m_pCmdArray; <------------------ access viloation error... yack..

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        The memory leak being detected is because the object that is added to the array (inside InitProcess) is allocated by the DLL but not freed by the DLL. I assume you're adding multiple elements to the array, right? Because if it's only one, then you don't need an array. The bottom line is that if the DLL is that one allocating the memory, it must also be the one freeing it. The access violation I'm not sure of, since there's a lot of code missing from your sample. Try posting it again but leave a space after the opening angle bracket and before the closing one (for the template parameters). Also, what is "type"? You don't show where it came from. Regards, Alvaro


        Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)

        K 1 Reply Last reply
        0
        • A Alvaro Mendez

          The memory leak being detected is because the object that is added to the array (inside InitProcess) is allocated by the DLL but not freed by the DLL. I assume you're adding multiple elements to the array, right? Because if it's only one, then you don't need an array. The bottom line is that if the DLL is that one allocating the memory, it must also be the one freeing it. The access violation I'm not sure of, since there's a lot of code missing from your sample. Try posting it again but leave a space after the opening angle bracket and before the closing one (for the template parameters). Also, what is "type"? You don't show where it came from. Regards, Alvaro


          Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)

          K Offline
          K Offline
          keb
          wrote on last edited by
          #4

          Wow, I can't get over the fact how quickly I get some help. Thanks guys. And here is more of the code we used which is failing. I guess I should have just cut and paste rather than typing things in... Also the idea behind here is that I am trying to que up a message that I recieve from a hardware and my co-worker is trying to read message and get rid of the memory space after he reads. What would be the best way to achieve this? Is creating a dll to do this work bad idea? co-workers' side: void CViapcDlg::OnConnect() { char errMsg[MAX_PATH]; DWORD result; hLib=LoadLibrary(COMMDLLNAME); DLL_LOAD = TRUE; if(hLib==NULL) { AfxMessageBox(errMsg); DLL_LOAD = FALSE; } else { char COMPORT[MAX_PATH]; int comport; memset(COMPORT,0,MAX_PATH); m_pHDContainer = new THDContainer(); m_pCommandArray = new CArray (); m_pCommandArray->SetSize(0, 10); m_pHDContainer->ONLINE = false; for(int i =0; i < 5; i++) { if(m_COMPort[i]->GetCheck()) comport = i+1; m_COMPort[i]->EnableWindow(FALSE); } wsprintf(COMPORT, "\\\\.\\COM%d", comport); lpfnInit = (LPFNINIT)GetProcAddress((HMODULE)hLib, "InitProcessor"); lpfnClose = (LPFNCLOSE)GetProcAddress((HMODULE)hLib, "CloseProcessor"); lpfnProcessor = (LPFNPROCESSOR)GetProcAddress((HMODULE)hLib, "Processor"); lpfnAdd = (LPFNADD)GetProcAddress((HMODULE)hLib, "Add"); if(lpfnInit) result = lpfnInit((LPCSTR)COMPORT, this->m_hWnd, m_pHDContainer, m_pCommandArray); } } void CViapcDlg::OnMessage(WPARAM wparam, LPARAM lparam) { while(m_pCommandArray->GetSize()) { TCommandArray insertData = m_pCommandArray->GetAt(0); m_pCommandArray->RemoveAt(0); do things with data here... } } void CViapcDlg::Close() { if(hLib) FreeLibrary((HMODULE)hLib); <----------- Memory leak here if(m_pHDContainer) { delete m_pHDContainer; } if(m_pCommandArray) { delete m_pCommandArray; <---------- Access violatioin error here } } My side: int InitProcessor(LPSTR lpPortName, HWND hWnd, THDContainer *pHDContain, CArray *pHDMsg) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); thread stuff here to get info from a hardware and add stuff into array; TCommandArray data; data.type1 = 1; data.type2 = 2; data.type3 = 3; pHDMsg->add(data); ::PostMessage( hWnd, COMDLL_ME

          A 1 Reply Last reply
          0
          • K keb

            Wow, I can't get over the fact how quickly I get some help. Thanks guys. And here is more of the code we used which is failing. I guess I should have just cut and paste rather than typing things in... Also the idea behind here is that I am trying to que up a message that I recieve from a hardware and my co-worker is trying to read message and get rid of the memory space after he reads. What would be the best way to achieve this? Is creating a dll to do this work bad idea? co-workers' side: void CViapcDlg::OnConnect() { char errMsg[MAX_PATH]; DWORD result; hLib=LoadLibrary(COMMDLLNAME); DLL_LOAD = TRUE; if(hLib==NULL) { AfxMessageBox(errMsg); DLL_LOAD = FALSE; } else { char COMPORT[MAX_PATH]; int comport; memset(COMPORT,0,MAX_PATH); m_pHDContainer = new THDContainer(); m_pCommandArray = new CArray (); m_pCommandArray->SetSize(0, 10); m_pHDContainer->ONLINE = false; for(int i =0; i < 5; i++) { if(m_COMPort[i]->GetCheck()) comport = i+1; m_COMPort[i]->EnableWindow(FALSE); } wsprintf(COMPORT, "\\\\.\\COM%d", comport); lpfnInit = (LPFNINIT)GetProcAddress((HMODULE)hLib, "InitProcessor"); lpfnClose = (LPFNCLOSE)GetProcAddress((HMODULE)hLib, "CloseProcessor"); lpfnProcessor = (LPFNPROCESSOR)GetProcAddress((HMODULE)hLib, "Processor"); lpfnAdd = (LPFNADD)GetProcAddress((HMODULE)hLib, "Add"); if(lpfnInit) result = lpfnInit((LPCSTR)COMPORT, this->m_hWnd, m_pHDContainer, m_pCommandArray); } } void CViapcDlg::OnMessage(WPARAM wparam, LPARAM lparam) { while(m_pCommandArray->GetSize()) { TCommandArray insertData = m_pCommandArray->GetAt(0); m_pCommandArray->RemoveAt(0); do things with data here... } } void CViapcDlg::Close() { if(hLib) FreeLibrary((HMODULE)hLib); <----------- Memory leak here if(m_pHDContainer) { delete m_pHDContainer; } if(m_pCommandArray) { delete m_pCommandArray; <---------- Access violatioin error here } } My side: int InitProcessor(LPSTR lpPortName, HWND hWnd, THDContainer *pHDContain, CArray *pHDMsg) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); thread stuff here to get info from a hardware and add stuff into array; TCommandArray data; data.type1 = 1; data.type2 = 2; data.type3 = 3; pHDMsg->add(data); ::PostMessage( hWnd, COMDLL_ME

            A Offline
            A Offline
            Alvaro Mendez
            wrote on last edited by
            #5

            Again, some of the code is missing because the template parameters (for CArray) are wiped out by the HTML interpreter. The easiest way to avoid it is to put a space after the opening bracket and before the closing bracket: CArray< type1, type2 >. Here are some points to consider: 1. The memory leak I already explained why it occurs. 2. The access violation I'm still not sure of, since it's not clear where m_pCommandArray is created and if your CViapcDlg::Close() is being called multiple times. A word of advice, after deleting a pointer variable, always set it to NULL. This ensures that if you delete it again, it's not pointing to bogus memory and the program doesn't blow up. 3. The question of whether or not to use a DLL in this case is best answered by you. Is the code in the DLL reusable by other apps? Or perhaps, do you intend to "plug in" a different set of source code into your app at run-time, similar to a driver? If the answer to either of these is Yes, then a DLL is the way to go. If not, then it's not worth it. You'll only complicate your life unnecessarily, as you're seeing now. Regards, Alvaro


            Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups