none
Calling a win32 API at runtime RRS feed

  • Question

  • The requirement of my application is such that I need to call the win32 API which is mentioned in the xml file.

    The XMl file contains the name of the Win32 API, the input parameters values. And parameters in XML are in sequence as per MSDN.

    like for example CreateFile APII have the API name = CreateFile, Filename = C:\sample\notepad.txt, etc.

    Now I need to call this CreateFile API with the argument values present in the XML file.

    I need to write the generic code such that the application should be able to execute any Win32 API present in the XML file.

    Please suggest any tutorial or guide me.

    i refered http://stackoverflow.com/questions/8696653/dynamically-load-a-function-from-a-dll

    but there also you need to declare the function pointer as per the perameters, but then this approach wont be generic, and also I cannot add the details for all the Win32 APIs.

    Please help.

    Thanks in Advance.


    Thanks & Regards, Mayank Agarwal

    Friday, December 23, 2016 12:00 PM

All replies

  • Is it your intent to call any Win32 API function or is there a defined subset of functions that your application should be able to handle?

    Do an internet search for "scripting Win32 API functions".  You will find that this will not be a trivial task.

    • Edited by RLWA32 Friday, December 23, 2016 12:38 PM
    Friday, December 23, 2016 12:36 PM
  •  I need to call the win32 API which is mentioned in the xml file.

    The XMl file contains the name of the Win32 API, the input parameters values. And parameters in XML are in sequence as per MSDN.

    Try to write it in Python, using the ctypes module.

    For example:

    import ctypes

    kernel32 = ctypes.windll.LoadLibrary("kernel32.dll")

    createfile = kernel32.CreateFileW

    hnd = createfile(.... your parameters )

    Many tutorials on ctypes are available in internet. Here you can get Python tools for Visual Studio.

    Good luck,

    - pa




    • Edited by Pavel A Friday, December 23, 2016 4:03 PM
    Friday, December 23, 2016 3:59 PM
  • Yes RLWA32, any win32 API can be called, but it will defined in xml with all input parameters value. As of now there are some defined file operation related APIs which I need to support through my application, but the application requirement is to make the application logic generic to support all Win32 APIs.

    I have to do with a Dialog based application using MFC support. As On the dialog of my application I will be displaying the xml file contents(API parameters in a list control, API name in static control). and there is one execute button which will actually execute the API.

     

    Thanks & Regards, Mayank Agarwal

    Saturday, December 24, 2016 6:12 AM
  • Thanks Pavel A, But I have to use only the VC++ dialog based application to achieve this. If a python script can do this job and which can be executed with my application, then will that be a good idea?

    if yes, then please guide me.


    Thanks & Regards, Mayank Agarwal

    Saturday, December 24, 2016 6:20 AM
  • Yes RLWA32, any win32 API can be called, but it will defined in xml with all input parameters value. As of now there are some defined file operation related APIs which I need to support through my application, but the application requirement is to make the application logic generic to support all Win32 APIs.


    Many Win32 functions require certain structures as parameters and those structures often require that a member is initialized with the size of the structure.  Many Win32 functions return non-zero on success or zero on failure.  But not all functions follow this pattern.  So in order to call Win32 functions successfully your code would need to have specific knowledge of the parameters and conventions used by that function.  I don't think you can "generically" support the entire Win32 API without including specific knowledge of the entire Win32 API in your implementation.

    Perhaps you should revisit your requirement.  Life is too short to re-implement the entire Win32 API.


    • Edited by RLWA32 Saturday, December 24, 2016 11:38 AM
    • Proposed as answer by Baron Bi Monday, December 26, 2016 6:41 AM
    Saturday, December 24, 2016 11:38 AM
  • If a python script can do this job and which can be executed with my application, then will that be a good idea?

    One can call Python from VC++ (moderately complicated) since one has managed to display GUI in VC++ (MFC? winforms?) and there are some GUI modules for pure Python (Qt, the "classic" Tct/Tk and others. Moderately+ complicated).

    Python (namely CPython with ctypes) is a good idea because it is friendly to C. More friendly than .NET PInvoke stuff.  Win32 API is C. You can manipulate C structures, pointers and so on. I don't know about IronPython which works with .NET (= modern well supported GUI). As RLWA32 wrote, you'll need to know the details of each API, but ctypes.windll is a good tool for the task.

    -- pa


    /* Something about Python, btw. */



    • Edited by Pavel A Sunday, December 25, 2016 2:08 AM
    Sunday, December 25, 2016 12:31 AM