Calling an API without the Header
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Calling an API without the Header

  1. #1
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Calling an API without the Header

    well ive been looking around and i found that you can call an API using LoadLibrary() without using the header (using inline asm)

    i find it very useful couze i dont need to include the "windows.h" if im just gone use the MessageBox API and makes the application small and fast

    the only code i could dig up was this
    Code:
    __asm{
     push par1
     push par2
     mov eax, AdressOfApi ; ret of GetProcAddress(LoadLibrary("Dll name"),"Api name")
     call eax
    }
    
    
    GetProcAddress(LoadLibrary("Dll name"),"Api name")
    how can i call a simple MessageBox through this code?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: Calling an API without the Header

    You can call any exported function in a DLL using GetProcAddress and LoadLibrary. No assembly language required. I wouldn't bother using the Windows API that way though.

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Smile Re: Calling an API without the Header

    This will also not compile for 64-bit
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: Calling an API without the Header

    Quote Originally Posted by Cpp_Noob View Post
    ... and makes the application small and fast
    How much (how many bytes) "smaller" and how many milliseconds faster do you expect to have your application using this "technique"?
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Calling an API without the Header

    Quote Originally Posted by Cpp_Noob View Post
    well ive been looking around and i found that you can call an API using LoadLibrary() without using the header (using inline asm)

    i find it very useful couze i dont need to include the "windows.h" if im just gone use the MessageBox API and makes the application small and fast

    the only code i could dig up was this
    Code:
    __asm{
     push par1
     push par2
     mov eax, AdressOfApi ; ret of GetProcAddress(LoadLibrary("Dll name"),"Api name")
     call eax
    }
    
    
    GetProcAddress(LoadLibrary("Dll name"),"Api name")
    how can i call a simple MessageBox through this code?
    An you plan to put that all over your code? What kind of application is this that you need it "small and fast"? Have you run benchmarks to see what's the difference in speed between the two approaches? I don't think you can measure anything significant.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    May 2005
    Posts
    4,954

    Re: Calling an API without the Header

    Quote Originally Posted by Cpp_Noob View Post
    how can i call a simple MessageBox through this code?
    Even if its fatser in couple of micros ( just a guess maybe not but lets say it faster ) , what’s the benefit here if you end up showing a message box that waits for use input anyway ?

    Cheers
    Last edited by golanshahar; February 23rd, 2009 at 10:26 AM.
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Re: Calling an API without the Header

    thanks for the replys
    i was hoping for a simple MessageBox example so i can run some test


    Quote Originally Posted by VictorN View Post
    How much (how many bytes) "smaller" and how many milliseconds faster do you expect to have your application using this "technique"?
    what if i use 5 or more headers in my app? whould that drop the app size alot?

    im really interested on lightweight applications

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: Calling an API without the Header

    Quote Originally Posted by Cpp_Noob View Post
    thanks for the replys
    i was hoping for a simple MessageBox example so i can run some test




    what if i use 5 or more headers in my app? whould that drop the app size alot?

    im really interested on lightweight applications
    No. Why are you concerned about app size anyway?

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: Calling an API without the Header

    Quote Originally Posted by Cpp_Noob View Post
    what if i use 5 or more headers in my app? whould that drop the app size alot?
    Define *alot*.

    Quote Originally Posted by Cpp_Noob View Post
    im really interested on lightweight applications
    What "weight" would for you *light* and what not?
    Victor Nijegorodov

  10. #10
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Re: Calling an API without the Header

    Quote Originally Posted by GCDEF View Post
    No. Why are you concerned about app size anyway?
    not only size but speed, they go hand by hand


    Quote Originally Posted by VictorN
    Define *alot*.
    Well , instead of including multiple headers on the project i can call them directly with GetProcAddress() and LoadLibrary(), wouldn't that eliminate the multiple headers size added to the application? And with the asm code making the API call faster?

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: Calling an API without the Header

    Quote Originally Posted by Cpp_Noob View Post
    not only size but speed, they go hand by hand




    Well , instead of including multiple headers on the project i can call them directly with GetProcAddress() and LoadLibrary(), wouldn't that eliminate the multiple headers size added to the application? And with the asm code making the API call faster?
    Where did you come up with size of the exe is related to speed of the app? Adding headers to an application doesn't slow it down.

    If anything that would appear to make it run slower, as the DLL would be loaded at the call instead of startup. Calling GetProcAddress at runtime instead of link time, and loading the DLL as the call is made instead of at startup will slow the app down.

  12. #12
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Re: Calling an API without the Header

    well, in "theory" it looked logical to me

    thanks for explaining it

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Calling an API without the Header

    The number of headers only affects compile speed, not run speed.

  14. #14
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Calling an API without the Header

    Quote Originally Posted by Cpp_Noob View Post
    thanks for the replys
    i was hoping for a simple MessageBox example so i can run some test




    what if i use 5 or more headers in my app? whould that drop the app size alot?

    im really interested on lightweight applications
    Well, I already asked: what kind of app are you developing? If you have a few hundred lines application, that it might not be of much importance how you write it. But when you're developing serious, large applications, and you put that kind of code in you make the maintenance (which always require more work than developing) a nightmare. Especially for new people unfamiliar with it. So I would recommend doing it the classic, right way.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  15. #15
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Re: Calling an API without the Header

    its not for any particular application , but for my future application and so i can make it run faster and with smaller code

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center