From EDM2
Jump to: navigation, search

DosQueryProcAddr is used to find the address of a process in a dynamic link module. Returns the address of the specified procedure within a dynamic link module.


DosQueryProcAddr(hmod, ordinal, pszName, ppfn)


hmod (HMODULE) - input
The handle of the dynamic link module that contains the procedure. (from DosLoadModule)
ordinal (ULONG) - input
The ordinal number of the procedure whose address is desired. If the ordinal number is nonzero, pszName is ignored. It must be less or equal to 65 533. (Ordinal number for the sought process. If 0 then pszProcName is used.)
pszName (PSZ) - input
The address of an ASCIIZ name string that contains the procedure name that is being referenced.
Calls to DosQueryProcAddr for entries within the DOSCALLS module are supported for ordinal references only. References to the DOSCALLS module by name strings are not supported, and will return an error. Dynamic link ordinal numbers for DOSCALLS routines are resolved by linking with OS2386.LIB. (Pointer to string containing the sought processes name. Not case sensitive.)
ppfn (PFN *) - output
A pointer to a PFN in which the procedure address is returned. (Address of a function pointer, which will contain the address of the procedure.)


Indicates if any error occurred.

Include Info

#include <os2.h>


If you receive return code ERROR_INVALID_HANDLE, issue DosLoadModule and repeat this call.

If you issue DosQueryProcAddr to obtain the address of an entry point that may only be accessed via a call gate, you receive the return code ERROR_ENTRY_IS_CALLGATE.

Sample Code

void (* my_dll_function)(int, char*);  /* Pointer to process */
APIRET rc = 0;

/* Load DLL and get hmod with DosLoadModule*/ 

/* Get address for process in DLL */
rc = DosQueryProcAddr(hmod, 0, "MY_DLL_FUNCTION", (PFN *) my_dll_function);
if(rc) {   /* Failure? */
   printf("Error. Cannot get the address for my_dll_function. DosQueryProcAddr returned %d.\n",rc);

/* Call my_dll_function */
my_dll_function(4711,"Hello World!\n");

This example loads the dynamic link module "DISPLAY.DDL", queries its address and type, and finally frees it.

 #define INCL_DOSMODULEMGR     /* Module Manager values */
 #define INCL_DOSERRORS        /* Error values */
 #include <os2.h>
 #include <stdio.h>

 int main(VOID) {

 PSZ      ModuleName     = "C:\\OS2\\DLL\\DISPLAY.DLL";  /* Name of module   */
 UCHAR    LoadError[256] = "";          /* Area for Load failure information */
 HMODULE  ModuleHandle   = NULLHANDLE;  /* Module handle                     */
 PFN      ModuleAddr     = 0;           /* Pointer to a system function      */
 ULONG    ModuleType     = 0;           /* Module type                       */
 APIRET   rc             = NO_ERROR;    /* Return code                       */

   rc = DosLoadModule(LoadError,               /* Failure information buffer */
                      sizeof(LoadError),       /* Size of buffer             */
                      ModuleName,              /* Module to load             */
                      &ModuleHandle);          /* Module handle returned     */
   if (rc != NO_ERROR) {
      printf("DosLoadModule error: return code = %u\n", rc);
      return 1;
   } else {
      printf("Module %s loaded.\n", ModuleName);
   } /* endif */

   rc = DosQueryProcAddr(ModuleHandle,         /* Handle to module           */
                         1L,                   /* No ProcName specified      */
                         NULL,                 /* ProcName (not specified)   */
                         &ModuleAddr);         /* Address returned           */
   if (rc != NO_ERROR) {
      printf("DosQueryProcAddr error: return code = %u\n", rc);
      return 1;
   } else printf("Address of module is 0x%x.\n", ModuleAddr);

   rc = DosQueryProcType(ModuleHandle,         /* Handle to module           */
                         1L,                   /* Indicate no ProcName given */
                         NULL,                 /* ProcName (not specified)   */
                         &ModuleType);         /* Type 0=16-bit   1=32-bit   */
   if (rc != NO_ERROR) {
      printf("DosQueryProcType error: return code = %u\n", rc);
      return 1;
   } else printf("This is a %s module.\n", ( ModuleType ? "32-bit" : "16-bit"));

   rc = DosFreeModule(ModuleHandle);
   if (rc != NO_ERROR) {
      printf("DosFreeModule error: return code = %u\n", rc);
      return 1;
   } else printf("Module %s freed.\n", ModuleName);

   return NO_ERROR;

See Also