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. FreeLibrary problem [modified]

FreeLibrary problem [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 Posts 4 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.
  • R Offline
    R Offline
    Rahul Vaishnav
    wrote on last edited by
    #1

    Hi all, Please help me... I am have written below function in which I am calling repeated as per my requirement..

    typedef HRESULT (*PROCHICON)(HICON*);
    typedef CString (*PROCCSTRNAME)();
    typedef CString (*PROCCSTRCONT)(CString);

    LoadButtonPluginDLL(CString DLLPath,CString &ButtonText,HICON &ImageHandle,CString &ButtonContent)
    {
    PROCHICON ProcButtonHicon;
    PROCCSTRNAME ProcButtonName;
    PROCCSTRCONT ProcButtonContent;
    HINSTANCE hLib = ::LoadLibrary(DLLPath); //load the plugin libarary dll//
    if(hLib==NULL)
    {
    MessageBox(_T("Fails To Load Dll"));
    return;
    }

    ProcButtonHicon =(PROCHICON)::GetProcAddress(hLib,"GetButtonImage");
    if(ProcButtonHicon == NULL) //if dll is not loaded
    {
    ::FreeLibrary(hLib);
    CoUninitialize();
    return;
    }
    ProcButtonName =(PROCCSTRNAME)::GetProcAddress(hLib,"GetButtonText");
    if(ProcButtonName == NULL)
    {
    ::FreeLibrary(hLib);
    CoUninitialize();
    return;
    }
    ProcButtonContent= (PROCCSTRCONT)::GetProcAddress(hLib,"GetPluginContent");
    if(ProcButtonContent == NULL) //if dll is not loaded
    {
    ::FreeLibrary(hLib);
    CoUninitialize();
    return;
    }
    ((PROCHICON)ProcButtonHicon)(&ImageHandle);
    ButtonText =((PROCCSTRNAME)ProcButtonName)();
    ButtonContent =((PROCCSTRCONT)ProcButtonContent)("1");

    if(hLib)
    FreeLibrary(hLib); //free the libaray
    }

    When I callabove function first time it works fine I get proper values in ImageHandle,ButtonText,ButtonContent from dll. but when I call this function second time and after calling FreeLibrary(hLib);variables ButtonText & ButtonContent become BadPtr and I can't use these values out side the function. :(

    modified on Friday, August 1, 2008 4:47 AM

    CPalliniC D H 3 Replies Last reply
    0
    • R Rahul Vaishnav

      Hi all, Please help me... I am have written below function in which I am calling repeated as per my requirement..

      typedef HRESULT (*PROCHICON)(HICON*);
      typedef CString (*PROCCSTRNAME)();
      typedef CString (*PROCCSTRCONT)(CString);

      LoadButtonPluginDLL(CString DLLPath,CString &ButtonText,HICON &ImageHandle,CString &ButtonContent)
      {
      PROCHICON ProcButtonHicon;
      PROCCSTRNAME ProcButtonName;
      PROCCSTRCONT ProcButtonContent;
      HINSTANCE hLib = ::LoadLibrary(DLLPath); //load the plugin libarary dll//
      if(hLib==NULL)
      {
      MessageBox(_T("Fails To Load Dll"));
      return;
      }

      ProcButtonHicon =(PROCHICON)::GetProcAddress(hLib,"GetButtonImage");
      if(ProcButtonHicon == NULL) //if dll is not loaded
      {
      ::FreeLibrary(hLib);
      CoUninitialize();
      return;
      }
      ProcButtonName =(PROCCSTRNAME)::GetProcAddress(hLib,"GetButtonText");
      if(ProcButtonName == NULL)
      {
      ::FreeLibrary(hLib);
      CoUninitialize();
      return;
      }
      ProcButtonContent= (PROCCSTRCONT)::GetProcAddress(hLib,"GetPluginContent");
      if(ProcButtonContent == NULL) //if dll is not loaded
      {
      ::FreeLibrary(hLib);
      CoUninitialize();
      return;
      }
      ((PROCHICON)ProcButtonHicon)(&ImageHandle);
      ButtonText =((PROCCSTRNAME)ProcButtonName)();
      ButtonContent =((PROCCSTRCONT)ProcButtonContent)("1");

      if(hLib)
      FreeLibrary(hLib); //free the libaray
      }

      When I callabove function first time it works fine I get proper values in ImageHandle,ButtonText,ButtonContent from dll. but when I call this function second time and after calling FreeLibrary(hLib);variables ButtonText & ButtonContent become BadPtr and I can't use these values out side the function. :(

      modified on Friday, August 1, 2008 4:47 AM

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      1. Please use code block button to surround code snippets with <pre> tags. 2. Why do you need to initialise COM library? 3. Why do you load the DLL and unload it inside your function body (overhead)? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      R 1 Reply Last reply
      0
      • CPalliniC CPallini

        1. Please use code block button to surround code snippets with <pre> tags. 2. Why do you need to initialise COM library? 3. Why do you load the DLL and unload it inside your function body (overhead)? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        R Offline
        R Offline
        Rahul Vaishnav
        wrote on last edited by
        #3

        Thanks for reply Actually this thing is some what new for me so while doing some R&D to overcome this problem I have initialize Com library, I think it is not necessary .. >3. Why do you load the DLL and unload it inside your function body (overhead)? - At runtime Dynamically my dll names & path will also be varying thats why I am Loading & Unloading DLL inside my function only. this bug I am facing when I am calling this function for same dll, second time. I think my dlls are not getting freed properly in first call of this function. Please help me...

        CPalliniC 1 Reply Last reply
        0
        • R Rahul Vaishnav

          Thanks for reply Actually this thing is some what new for me so while doing some R&D to overcome this problem I have initialize Com library, I think it is not necessary .. >3. Why do you load the DLL and unload it inside your function body (overhead)? - At runtime Dynamically my dll names & path will also be varying thats why I am Loading & Unloading DLL inside my function only. this bug I am facing when I am calling this function for same dll, second time. I think my dlls are not getting freed properly in first call of this function. Please help me...

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Could you please edit the OP and reformat your code as requested? Is the DLL's code yours (Are you properly doing cleanup inside it?)? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          R 1 Reply Last reply
          0
          • CPalliniC CPallini

            Could you please edit the OP and reformat your code as requested? Is the DLL's code yours (Are you properly doing cleanup inside it?)? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            R Offline
            R Offline
            Rahul Vaishnav
            wrote on last edited by
            #5

            thanks you for reply.. Code is reformatted please check.. the DLL's code is written by my team member,I need to check with him.. but it is working properly for ImageHandle which is passed by reference in this function. It is causing problem with ButtonText & ButtonContent only...

            CPalliniC 1 Reply Last reply
            0
            • R Rahul Vaishnav

              thanks you for reply.. Code is reformatted please check.. the DLL's code is written by my team member,I need to check with him.. but it is working properly for ImageHandle which is passed by reference in this function. It is causing problem with ButtonText & ButtonContent only...

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Rahul Vaishnav wrote:

              Code is reformatted please check.. the DLL's code is written by my team member,I need to check with him.. but it is working properly for ImageHandle which is passed by reference in this function. It is causing problem with ButtonText & ButtonContent only...

              Well, since the DLL's source code is available you may debug the whole thing. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [Image resize DLL]

              In testa che avete, signor di Ceprano?

              R 1 Reply Last reply
              0
              • CPalliniC CPallini

                Rahul Vaishnav wrote:

                Code is reformatted please check.. the DLL's code is written by my team member,I need to check with him.. but it is working properly for ImageHandle which is passed by reference in this function. It is causing problem with ButtonText & ButtonContent only...

                Well, since the DLL's source code is available you may debug the whole thing. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [Image resize DLL]

                R Offline
                R Offline
                Rahul Vaishnav
                wrote on last edited by
                #7

                Ok I will debug it Thank you very much for reply.. bye :)

                1 Reply Last reply
                0
                • R Rahul Vaishnav

                  Hi all, Please help me... I am have written below function in which I am calling repeated as per my requirement..

                  typedef HRESULT (*PROCHICON)(HICON*);
                  typedef CString (*PROCCSTRNAME)();
                  typedef CString (*PROCCSTRCONT)(CString);

                  LoadButtonPluginDLL(CString DLLPath,CString &ButtonText,HICON &ImageHandle,CString &ButtonContent)
                  {
                  PROCHICON ProcButtonHicon;
                  PROCCSTRNAME ProcButtonName;
                  PROCCSTRCONT ProcButtonContent;
                  HINSTANCE hLib = ::LoadLibrary(DLLPath); //load the plugin libarary dll//
                  if(hLib==NULL)
                  {
                  MessageBox(_T("Fails To Load Dll"));
                  return;
                  }

                  ProcButtonHicon =(PROCHICON)::GetProcAddress(hLib,"GetButtonImage");
                  if(ProcButtonHicon == NULL) //if dll is not loaded
                  {
                  ::FreeLibrary(hLib);
                  CoUninitialize();
                  return;
                  }
                  ProcButtonName =(PROCCSTRNAME)::GetProcAddress(hLib,"GetButtonText");
                  if(ProcButtonName == NULL)
                  {
                  ::FreeLibrary(hLib);
                  CoUninitialize();
                  return;
                  }
                  ProcButtonContent= (PROCCSTRCONT)::GetProcAddress(hLib,"GetPluginContent");
                  if(ProcButtonContent == NULL) //if dll is not loaded
                  {
                  ::FreeLibrary(hLib);
                  CoUninitialize();
                  return;
                  }
                  ((PROCHICON)ProcButtonHicon)(&ImageHandle);
                  ButtonText =((PROCCSTRNAME)ProcButtonName)();
                  ButtonContent =((PROCCSTRCONT)ProcButtonContent)("1");

                  if(hLib)
                  FreeLibrary(hLib); //free the libaray
                  }

                  When I callabove function first time it works fine I get proper values in ImageHandle,ButtonText,ButtonContent from dll. but when I call this function second time and after calling FreeLibrary(hLib);variables ButtonText & ButtonContent become BadPtr and I can't use these values out side the function. :(

                  modified on Friday, August 1, 2008 4:47 AM

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Rahul Vaishnav wrote:

                  ((PROCHICON)ProcButtonHicon)(&ImageHandle); ButtonText =((PROCCSTRNAME)ProcButtonName)(); ButtonContent =((PROCCSTRCONT)ProcButtonContent)("1");

                  This is a very odd way of calling functions. Why not:

                  ProcButtonHicon(&ImageHandle);
                  ButtonText = ProcButtonName();
                  ButtonContent = ProcButtonContent("1");

                  Rahul Vaishnav wrote:

                  if(hLib)

                  Redundant check.

                  "Love people and use things, not love things and use people." - Unknown

                  "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                  1 Reply Last reply
                  0
                  • R Rahul Vaishnav

                    Hi all, Please help me... I am have written below function in which I am calling repeated as per my requirement..

                    typedef HRESULT (*PROCHICON)(HICON*);
                    typedef CString (*PROCCSTRNAME)();
                    typedef CString (*PROCCSTRCONT)(CString);

                    LoadButtonPluginDLL(CString DLLPath,CString &ButtonText,HICON &ImageHandle,CString &ButtonContent)
                    {
                    PROCHICON ProcButtonHicon;
                    PROCCSTRNAME ProcButtonName;
                    PROCCSTRCONT ProcButtonContent;
                    HINSTANCE hLib = ::LoadLibrary(DLLPath); //load the plugin libarary dll//
                    if(hLib==NULL)
                    {
                    MessageBox(_T("Fails To Load Dll"));
                    return;
                    }

                    ProcButtonHicon =(PROCHICON)::GetProcAddress(hLib,"GetButtonImage");
                    if(ProcButtonHicon == NULL) //if dll is not loaded
                    {
                    ::FreeLibrary(hLib);
                    CoUninitialize();
                    return;
                    }
                    ProcButtonName =(PROCCSTRNAME)::GetProcAddress(hLib,"GetButtonText");
                    if(ProcButtonName == NULL)
                    {
                    ::FreeLibrary(hLib);
                    CoUninitialize();
                    return;
                    }
                    ProcButtonContent= (PROCCSTRCONT)::GetProcAddress(hLib,"GetPluginContent");
                    if(ProcButtonContent == NULL) //if dll is not loaded
                    {
                    ::FreeLibrary(hLib);
                    CoUninitialize();
                    return;
                    }
                    ((PROCHICON)ProcButtonHicon)(&ImageHandle);
                    ButtonText =((PROCCSTRNAME)ProcButtonName)();
                    ButtonContent =((PROCCSTRCONT)ProcButtonContent)("1");

                    if(hLib)
                    FreeLibrary(hLib); //free the libaray
                    }

                    When I callabove function first time it works fine I get proper values in ImageHandle,ButtonText,ButtonContent from dll. but when I call this function second time and after calling FreeLibrary(hLib);variables ButtonText & ButtonContent become BadPtr and I can't use these values out side the function. :(

                    modified on Friday, August 1, 2008 4:47 AM

                    H Offline
                    H Offline
                    Hartwin Stuewe
                    wrote on last edited by
                    #9

                    I had a similar problem: Access violations caused by bad pointers after having freed a dll with FreeLibrary. Your problem sounds just the same. The problem was, the dll and the exe each had its own copy of the c-runtime-library and each c-runtime-library had its own heap. Freeing the dll also freed its heap and all dynamically allocated memory. To overcome this problem: Free memory where it had been allocated Her is the link about the 'FreeLibrary problem' where i found the answer to the problem: http://java.codeproject.com/Feature/SubtleBugs.aspx?fid=1647&msg=2660410

                    R 1 Reply Last reply
                    0
                    • H Hartwin Stuewe

                      I had a similar problem: Access violations caused by bad pointers after having freed a dll with FreeLibrary. Your problem sounds just the same. The problem was, the dll and the exe each had its own copy of the c-runtime-library and each c-runtime-library had its own heap. Freeing the dll also freed its heap and all dynamically allocated memory. To overcome this problem: Free memory where it had been allocated Her is the link about the 'FreeLibrary problem' where i found the answer to the problem: http://java.codeproject.com/Feature/SubtleBugs.aspx?fid=1647&msg=2660410

                      R Offline
                      R Offline
                      Rahul Vaishnav
                      wrote on last edited by
                      #10

                      Thanks you for your reply.. I was on leave thats why I couldnt reply..? I will go through this link.. Thanks you.

                      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