home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13354 < prev    next >
Encoding:
Text File  |  1992-09-08  |  2.0 KB  |  66 lines

  1. Path: sparky!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!darwin.sura.net!jvnc.net!nuscc!solomon.technet.sg!ipser
  2. From: ipser@solomon.technet.sg (Ed Ipser)
  3. Newsgroups: comp.windows.ms.programmer,comp.lang.c++
  4. Subject: BC++/MS Windows:  How Do You Call Anonymous Functions?
  5. Summary: How Do You Call Anonymous Functions?
  6. Message-ID: <1992Sep7.042033.4884@nuscc.nus.sg>
  7. Date: 7 Sep 92 04:20:33 GMT
  8. Sender: usenet@nuscc.nus.sg
  9. Organization: TECHNET, Singapore
  10. Lines: 54
  11.  
  12.  
  13.  
  14. How Do You Call Anonymous Functions?
  15.  
  16. Here is the problem: I want to write an interpreter under Borland C++
  17. and MS Windows and I need to be able to call arbitrary functions from
  18. within the interpreter such as Windows API functions as well as
  19. user-defined functions from DLLs. Of course, normal function pointers
  20. will not work for two reasons: first, they would require a function
  21. type for every type of function and, worse, the types of functions are
  22. not known when the interpreter is compiled since the user can define
  23. his own.
  24.  
  25. I know a solution exists because many applications have this capability.
  26.  
  27. Let me phrase the problem more precisely:
  28.  
  29. I want to write two functions:
  30.  
  31.   function_type* get_function (
  32.     char* dll_file, char* name) ;
  33.  
  34. the get_function function would return a far pointer to a function from the
  35. name of the DLL containing the function and the name of the function.
  36.  
  37. and:
  38.  
  39.   void apply_function (
  40.     function_type* function,
  41.     int parameter_size, void* parameters,
  42.     int result_size, void* result) ;
  43.  
  44. which would push the parameters and result on the stack, call the function, and then
  45. copy the result into the result parameter.
  46.  
  47. for example, the following would call the Windows max function:
  48.  
  49. void dummy ()
  50. {
  51.   struct
  52.   {
  53.     int x ;
  54.     int y ;
  55.   } xy ;
  56.   int max ;
  57.  
  58.   apply_function(get_function("windows","max"),4,&xy,2,&max) ;
  59.   cout << max ;
  60. } ;
  61.  
  62.  
  63.  
  64. The next question, of course, is how do you do this with data members and
  65. member functions of classes? How do you find the virtual tables?
  66.