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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to extract Exported functions from a DLL

How to extract Exported functions from a DLL

Scheduled Pinned Locked Moved C / C++ / MFC
toolshelptutorialquestion
12 Posts 4 Posters 3 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.
  • P poda

    Hi Friends, I am in need to extract the exported functions of a DLL. If a DLL name is given,all exported functions in that DLL must be listed out. I used Dumpbin and tdump utility tools to do so. But I need to make this without using any utility tools, just mere by coding. Could anyone please help me?

    N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #3

    poda123 wrote:

    But I need to make this without using any utility tools, just mere by coding.

    Use SymEnumSymbols. It takes a callback function pointer which in turn gets called for every symbol found, as a parameter to the function you get a pointer to a SYMBOL_INFO structure. Use the Flags member of this struct along with SYMFLAG_EXPORT to find out whether this particular symbol is exported or not. I am doing the same in my Process viewer article here -> http://www.codeproject.com/KB/cpp/processviewer.aspx[^] I guess you will have to load the DLL into your process. Not sure!

    Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

    P 1 Reply Last reply
    0
    • P poda

      Hi Friends, I am in need to extract the exported functions of a DLL. If a DLL name is given,all exported functions in that DLL must be listed out. I used Dumpbin and tdump utility tools to do so. But I need to make this without using any utility tools, just mere by coding. Could anyone please help me?

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #4

      Here's a program I knocked up which lists the functions exported by name from a dll:

      // Exports.cpp : Defines the entry point for the console application.
      //
       
      #include "stdafx.h"
      #include <iostream>
      #include <windows.h>
      #include <Imagehlp.h>
      #pragma comment(lib, "Imagehlp.lib")
      using namespace std;
       
      int main(int argc, char* argv[])
      {
      if (argc!=2)
      {
      cerr << "Usage:\n\tExports <path to dll>" << endl;
      return 1;
      }
       
      // Try to map the DLL into memory.
      LOADED_IMAGE li;
      BOOL bOK = MapAndLoad(
      argv[1], // PSTR ImageName
      NULL, // PSTR DllPath
      &li, // PLOADED_IMAGE LoadedImage
      TRUE, // BOOL DotDll
      TRUE // BOOL ReadOnly
      );
      if (!bOK)
      {
      cerr << "MapAndLoad failed!" << endl;
      return 2;
      }
       
      // Now get the address of the export table.
      DWORD expVA = li.FileHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
      PIMAGE_EXPORT_DIRECTORY pExp = (PIMAGE_EXPORT_DIRECTORY)ImageRvaToVa(
      li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
      li.MappedAddress, // IN PVOID Base
      expVA, // IN ULONG Rva
      NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
      );
       
       
      // Now dump the functions names.
      DWORD rvaNames = pExp->AddressOfNames;
      DWORD *prvaNames = (DWORD*)ImageRvaToVa(
      li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
      li.MappedAddress, // IN PVOID Base
      rvaNames, // IN ULONG Rva
      NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
      );
      for (DWORD i=0; i<pExp->NumberOfNames; ++i)
      {
      DWORD rvaName = prvaNames[i];
      const char *pName = (const char*)ImageRvaToVa(
      li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
      li.MappedAddress, // IN PVOID Base
      rvaName, // IN ULONG Rva
      NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
      );
       
      cout << pName << endl;
      }
       
      // Unmap it.
      UnMapAndLoad(&li);
       
      return 0;
      }

      Steve

      modified on Wednesday, February 13, 2008 10:58 PM

      P 2 Replies Last reply
      0
      • S Stephen Hewitt

        Can you give more details? Is the DLL loaded into the memory space of the process used to list the exports?

        Steve

        P Offline
        P Offline
        poda
        wrote on last edited by
        #5

        The DLL is chosen by the user. Once it is chosen,the names of the exported functions in that DLL has to shown. So it is during runtime the DLL is known, for which its functions are listed out. Thanks!

        1 Reply Last reply
        0
        • S Stephen Hewitt

          Here's a program I knocked up which lists the functions exported by name from a dll:

          // Exports.cpp : Defines the entry point for the console application.
          //
           
          #include "stdafx.h"
          #include <iostream>
          #include <windows.h>
          #include <Imagehlp.h>
          #pragma comment(lib, "Imagehlp.lib")
          using namespace std;
           
          int main(int argc, char* argv[])
          {
          if (argc!=2)
          {
          cerr << "Usage:\n\tExports <path to dll>" << endl;
          return 1;
          }
           
          // Try to map the DLL into memory.
          LOADED_IMAGE li;
          BOOL bOK = MapAndLoad(
          argv[1], // PSTR ImageName
          NULL, // PSTR DllPath
          &li, // PLOADED_IMAGE LoadedImage
          TRUE, // BOOL DotDll
          TRUE // BOOL ReadOnly
          );
          if (!bOK)
          {
          cerr << "MapAndLoad failed!" << endl;
          return 2;
          }
           
          // Now get the address of the export table.
          DWORD expVA = li.FileHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
          PIMAGE_EXPORT_DIRECTORY pExp = (PIMAGE_EXPORT_DIRECTORY)ImageRvaToVa(
          li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
          li.MappedAddress, // IN PVOID Base
          expVA, // IN ULONG Rva
          NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
          );
           
           
          // Now dump the functions names.
          DWORD rvaNames = pExp->AddressOfNames;
          DWORD *prvaNames = (DWORD*)ImageRvaToVa(
          li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
          li.MappedAddress, // IN PVOID Base
          rvaNames, // IN ULONG Rva
          NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
          );
          for (DWORD i=0; i<pExp->NumberOfNames; ++i)
          {
          DWORD rvaName = prvaNames[i];
          const char *pName = (const char*)ImageRvaToVa(
          li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
          li.MappedAddress, // IN PVOID Base
          rvaName, // IN ULONG Rva
          NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
          );
           
          cout << pName << endl;
          }
           
          // Unmap it.
          UnMapAndLoad(&li);
           
          return 0;
          }

          Steve

          modified on Wednesday, February 13, 2008 10:58 PM

          P Offline
          P Offline
          poda
          wrote on last edited by
          #6

          Awesome! Thanks a lot Steve for your help. Thank you so much!

          1 Reply Last reply
          0
          • N Nibu babu thomas

            poda123 wrote:

            But I need to make this without using any utility tools, just mere by coding.

            Use SymEnumSymbols. It takes a callback function pointer which in turn gets called for every symbol found, as a parameter to the function you get a pointer to a SYMBOL_INFO structure. Use the Flags member of this struct along with SYMFLAG_EXPORT to find out whether this particular symbol is exported or not. I am doing the same in my Process viewer article here -> http://www.codeproject.com/KB/cpp/processviewer.aspx[^] I guess you will have to load the DLL into your process. Not sure!

            Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            P Offline
            P Offline
            poda
            wrote on last edited by
            #7

            Thanks for your reply. I tried to execute the process viewer, but could not run due to the error message "...desktop\procviewer\utils.h(829) : error C2065: 'SLR_NOSEARCH' : undeclared identifier" Could you please help me out to run your application,where I can learn more things out there. Thanks!

            N D 2 Replies Last reply
            0
            • S Stephen Hewitt

              Here's a program I knocked up which lists the functions exported by name from a dll:

              // Exports.cpp : Defines the entry point for the console application.
              //
               
              #include "stdafx.h"
              #include <iostream>
              #include <windows.h>
              #include <Imagehlp.h>
              #pragma comment(lib, "Imagehlp.lib")
              using namespace std;
               
              int main(int argc, char* argv[])
              {
              if (argc!=2)
              {
              cerr << "Usage:\n\tExports <path to dll>" << endl;
              return 1;
              }
               
              // Try to map the DLL into memory.
              LOADED_IMAGE li;
              BOOL bOK = MapAndLoad(
              argv[1], // PSTR ImageName
              NULL, // PSTR DllPath
              &li, // PLOADED_IMAGE LoadedImage
              TRUE, // BOOL DotDll
              TRUE // BOOL ReadOnly
              );
              if (!bOK)
              {
              cerr << "MapAndLoad failed!" << endl;
              return 2;
              }
               
              // Now get the address of the export table.
              DWORD expVA = li.FileHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
              PIMAGE_EXPORT_DIRECTORY pExp = (PIMAGE_EXPORT_DIRECTORY)ImageRvaToVa(
              li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
              li.MappedAddress, // IN PVOID Base
              expVA, // IN ULONG Rva
              NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
              );
               
               
              // Now dump the functions names.
              DWORD rvaNames = pExp->AddressOfNames;
              DWORD *prvaNames = (DWORD*)ImageRvaToVa(
              li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
              li.MappedAddress, // IN PVOID Base
              rvaNames, // IN ULONG Rva
              NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
              );
              for (DWORD i=0; i<pExp->NumberOfNames; ++i)
              {
              DWORD rvaName = prvaNames[i];
              const char *pName = (const char*)ImageRvaToVa(
              li.FileHeader, // IN PIMAGE_NT_HEADERS NtHeaders
              li.MappedAddress, // IN PVOID Base
              rvaName, // IN ULONG Rva
              NULL // IN OUT PIMAGE_SECTION_HEADER *LastRvaSection
              );
               
              cout << pName << endl;
              }
               
              // Unmap it.
              UnMapAndLoad(&li);
               
              return 0;
              }

              Steve

              modified on Wednesday, February 13, 2008 10:58 PM

              P Offline
              P Offline
              poda
              wrote on last edited by
              #8

              Could you please share how did you come up with such a nice code to export dll functions. Any website reference? Thanks!

              S 1 Reply Last reply
              0
              • P poda

                Thanks for your reply. I tried to execute the process viewer, but could not run due to the error message "...desktop\procviewer\utils.h(829) : error C2065: 'SLR_NOSEARCH' : undeclared identifier" Could you please help me out to run your application,where I can learn more things out there. Thanks!

                N Offline
                N Offline
                Nibu babu thomas
                wrote on last edited by
                #9

                poda123 wrote:

                Could you please help me out to run your application,where I can learn more things out there.

                Do you have latest platform sdk installed?

                Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                P 1 Reply Last reply
                0
                • P poda

                  Could you please share how did you come up with such a nice code to export dll functions. Any website reference? Thanks!

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #10

                  Many years ago I read this[^] and this[^]. The "WinNT.h" header file is also informative.

                  Steve

                  1 Reply Last reply
                  0
                  • P poda

                    Thanks for your reply. I tried to execute the process viewer, but could not run due to the error message "...desktop\procviewer\utils.h(829) : error C2065: 'SLR_NOSEARCH' : undeclared identifier" Could you please help me out to run your application,where I can learn more things out there. Thanks!

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

                    poda123 wrote:

                    I tried to execute the process viewer, but could not run due to the error message "...desktop\procviewer\utils.h(829) : error C2065: 'SLR_NOSEARCH' : undeclared identifier"

                    This makes no sense. C2065 is a compiler error, which means you would not have had a Process Viewer executable.

                    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    1 Reply Last reply
                    0
                    • N Nibu babu thomas

                      poda123 wrote:

                      Could you please help me out to run your application,where I can learn more things out there.

                      Do you have latest platform sdk installed?

                      Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                      P Offline
                      P Offline
                      poda
                      wrote on last edited by
                      #12

                      Hi, I have downloaded and installed the version Microsoft® Windows Server 2003 R2 Platform SDK - March 2006 Edition. But still I get the same error. If there is a newer version,please send me the link. Thanks!

                      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