LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get and list exportable functions from DLL seen from other application

Good afternoon, 

I am programming a SW application which contains a List Box in its UIR that will show all the functions defined in an external DLL.

That DLL was compiled with LabWindows/CVI as debuggable DLL (32 bits) and its .lib and .h files were added to my project which will show the list of exportable functions, from that DLL, whose prototypes are defined in that .h file as:

extern void  __declspec(dllexport) __stdcall FUNCTION(......)

I know that i can access those functions using LoadLibrary and GetProcAddress functions from Windows Driver Kit (WDK) but i want to "see" all those functions to list them into the List Box programmatically to be selectable by the user later on.

Thanks in advance!

Regards, 

Alejandro Pinto

0 Kudos
Message 1 of 2
(3,752 Views)

Hello

 

i used the following code in one of my projects using the sdk lib

you have to #include "ImageHlp.h"

and also add the lib to your prj

 

int GetFunctionNames (char *dllFileName , int panel , int list_ctrl)
{
int ret_val = 0 ;
char func_name[256] = {'\0'};
HANDLE hFile = NULL , hFileMapping = NULL ;
LPVOID        lpFileBase = NULL ;
PIMAGE_DOS_HEADER     pDOS;
PIMAGE_NT_HEADERS     pNT;

DWORD        i;
PIMAGE_EXPORT_DIRECTORY   pED;
PDWORD       *pEN;
PIMAGE_IMPORT_BY_NAME    pOrdinalName;
PIMAGE_IMPORT_DESCRIPTOR   pIDesc;
DWORD        rvaINT;
DWORD        rvaIAT;
PIMAGE_THUNK_DATA     pINT;
PIMAGE_THUNK_DATA     pIAT;
ULONGLONG       ordinal;


 hFile = CreateFile (dllFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
 if (hFile == INVALID_HANDLE_VALUE)
 { 
  MessagePopup ("", "Unable to open DLL File"); return 0 ;
 } 

 hFileMapping = CreateFileMapping (hFile, NULL, PAGE_READONLY, 0, 0, NULL);
 if (hFileMapping == 0)
  goto SOF;

 lpFileBase = MapViewOfFile (hFileMapping, FILE_MAP_READ, 0, 0, 0);
 if (lpFileBase == 0)
  goto SOF;

 pDOS = (PIMAGE_DOS_HEADER)lpFileBase;
 pNT = (PIMAGE_NT_HEADERS)((LONG)pDOS + (LONG)pDOS->e_lfanew);

 if (IsBadReadPtr (pNT, sizeof (IMAGE_NT_HEADERS)))
  goto SOF;
 else
 if (pNT->Signature != IMAGE_NT_SIGNATURE )
 {
    switch ((WORD)pNT->Signature)
    {
     case IMAGE_DOS_SIGNATURE: break;
     case IMAGE_OS2_SIGNATURE: break;
     case IMAGE_VXD_SIGNATURE: break;
    }       
 goto SOF;
 }

 pED = (PIMAGE_EXPORT_DIRECTORY)pNT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
 if (!pED)
 goto SOF;
 pED = (PIMAGE_EXPORT_DIRECTORY)ImageRvaToVa (pNT, pDOS, (DWORD)pED, 0);
 pEN = (PDWORD*) pED->AddressOfNames;
 pEN = (PDWORD*)ImageRvaToVa (pNT, pDOS, (DWORD)pEN, 0);
 if(!pEN)
  goto SOF;

 for (i = 0; i < pED->NumberOfNames; i++)
 {
 sprintf (func_name, "%s", (PSTR)ImageRvaToVa (pNT, pDOS, (DWORD)*pEN, 0));
 InsertListItem(panel , list_ctrl , -1 , func_name , 0) ;
    pEN++;
 }

 pIDesc = (PIMAGE_IMPORT_DESCRIPTOR)pNT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
 if (!pIDesc)
 goto SOF;
 
 pIDesc = (PIMAGE_IMPORT_DESCRIPTOR)ImageRvaToVa (pNT, pDOS, (DWORD)pIDesc, 0);
 while (1)
 {
 // See if we've reached an empty IMAGE_IMPORT_DESCRIPTOR
    if ((pIDesc->TimeDateStamp == 0 ) && (pIDesc->Name == 0))
        break;

 sprintf (func_name, "%s", (PSTR)ImageRvaToVa (pNT, pDOS, (DWORD)pIDesc->Name, 0));
 rvaINT = pIDesc->OriginalFirstThunk;
    rvaIAT = pIDesc->FirstThunk;
    if (rvaINT == 0 )   // No Characteristics field?
    {
        //FirstThunk field then.
           rvaINT = rvaIAT;
           if (rvaINT == 0 )   // No FirstThunk field
      goto SOF;
    }
               
    pINT = (PIMAGE_THUNK_DATA)ImageRvaToVa (pNT, pDOS, rvaINT, 0);
    if (!pINT )
  goto SOF;
    pIAT = (PIMAGE_THUNK_DATA)ImageRvaToVa (pNT, pDOS, rvaIAT, 0);
    while (1)
 {
        if (pINT->u1.AddressOfData == 0)
   break;
        ordinal = -1;
  if (IMAGE_SNAP_BY_ORDINAL32 (pINT->u1.Ordinal))
   ordinal = IMAGE_ORDINAL32 (pINT->u1.Ordinal);
        if (ordinal == -1)
        {
         pOrdinalName = (PIMAGE_IMPORT_BY_NAME)ImageRvaToVa (pNT, pDOS,(DWORD)(pINT->u1.AddressOfData), 0);
        }
        pINT++;         // Advance to next thunk
        pIAT++;         // advance to next thunk
    }
    pIDesc++;

 }

ret_val = 1 ;

SOF:
if(lpFileBase != NULL)
 UnmapViewOfFile (lpFileBase);
 if(hFileMapping != NULL)
  CloseHandle (hFileMapping);
 if(hFile != NULL)
  CloseHandle (hFile);
 return ret_val ;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

0 Kudos
Message 2 of 2
(3,589 Views)