API Calls in C++

Discussion in 'C++' started by Lars Uffmann, Jun 19, 2008.

  1. Lars Uffmann

    Lars Uffmann Guest

    Hi!

    I have a closed-source software that comes with an API, and I can't seem
    to understand how to define the API functions so that my program will
    actually try to access the API.

    What is needed to successfully access an API in C++? Just a correct
    function header definition and the running application that provides the
    programming interface?

    The accompanying examples (visual basic only) came with this header file:
    /* ********************** */
    #ifdef _WIN32
    #define CCONV _stdcall
    #define NOMANGLE
    #else
    #define CCONV FAR PASCAL _export
    #define NOMANGLE EXTERN_C
    #endif

    NOMANGLE int CCONV UplinkUserCommand( const char *dest_name_ptr,
    const char *cmd_name_ptr,
    const unsigned char *cmd_data_ptr,
    unsigned long len_of_cmd_data );
    /* ********************** */

    If I just include that, however, the linker complains about a missing
    reference. So does that mean I need the dll for this api at link time?

    Grateful for any tips!

    Lars
    Lars Uffmann, Jun 19, 2008
    #1
    1. Advertising

  2. Lars Uffmann

    Stefan Ram Guest

    Lars Uffmann <> writes:
    >I have a closed-source software that comes with an API, and I can't seem


    If the provider of the software has claimed his product to be
    designated for use with certain programming language
    implementations, he usually will provide information which
    files to include and how to configure the linker.

    Usually, one needs to both include a header file and to
    link to a library. In the case of a DLL, this library will
    serve as a static interface to the DLL. Or else, operating
    system specific methods can be used to access the DLL.
    Stefan Ram, Jun 19, 2008
    #2
    1. Advertising

  3. Lars Uffmann

    Lars Uffmann Guest

    X-Posted to gnu.g++.help, F'Up set also

    Mike Wahler wrote:
    > Usually an API (aka 'library') is provided with headers to include
    > in your source, along with a library file containing compiled code
    > that you link with your executable.


    Well - the headers are there, the library file is a *.lib, apparently
    compiled in Visual C++. Are such library-files compiler-independant
    formatted?

    > Somewhere there needs to be a file (either source or compiled)
    > that contains the implementation of the function 'UplinkUserCommand()'.


    Yes of course - I was expecting that this was the dll-files in
    %WINDIR%\system32 - however I have no idea how to tell the linker to
    look for the function in a dll (Only now occured to me, because I have
    used dlls before, but those abode by a name schema so that I could use
    -lxerces for example). Guess I will have to ask in a gnu newsgroup?

    > This is usually a file called an 'object file' or 'library file'
    > (compiled code). Include it in the list of files submitted to your
    > linker. If the source is provided, then of course it must first be
    > compiled as well.


    I have the *.lib from Visual C++ (I suppose thats VC++'s *incompatible*
    equivalent of gnu's .a object files?) and the *.dll files. Can I use the
    dll-files for linking? From what I know it should be possible... Now if
    I only knew how to tell that to my linker...

    Best Regards & Thanks for the answers so far,

    Lars
    Lars Uffmann, Jun 19, 2008
    #3
  4. Lars Uffmann

    Lars Uffmann Guest

    Re: [OT] Re: API Calls in C++

    Paavo Helde wrote:
    > Note that the behavior of linkers and specific platforms is off topic in
    > this group.


    "Noted" even before you commented on it, thus the follow-up. But I
    wasn't aware of the general way to define API calls in C++, which is
    pretty on-topic here.

    > From your later posts I gather that you are trying to link a VC++-
    > compiled library with a gcc-compiled application, in Windows platform,
    > right?

    Yup. I wish Microsoft had never started messing around with C++, then
    people wouldn't be using it *g*

    > In case of C interface you should be able to do that. In case of C++
    > interface this is undefined behavior, meaning that it might even work,
    > because the function signature seems quite C-like. From your post it is
    > hard to tell whether your particular DLL actually uses C or C++
    > interface.


    Ouch. TBH I have no idea whether the library is C or C++. I'd have to
    ask the developers. How comes he behaviour or linking vs. a dll created
    with another compiler is undefined when that is written in C++?

    > Windows-specific: the function implementation is in the .DLL (or .LIB
    > file for static library), but one has to link with the corresponding .LIB
    > file in any case. The .DLL must be present at run-time only.


    Ouch. That's what I thought & feared. But - from what you're saying, I
    gather that it should be possible to link against VC++ lib-files from
    other compilers?

    > The first thing to do is to look up the actual exported function name
    > inside the DLL (with Dependency Walker tool, for example) and see if it
    > is mangled or not.

    Thank you for this - especially the tool name, I am doing some web
    research on that now :)

    > compiled by other compilers (C ABI is standardized, more or less, for a
    > given platform, which is not the case for C++ ABI).

    Okay, that answers my above question about undefined behaviour. Thanks
    again.

    I'll continue to post in gnu.g++.help once I find out more.

    Best Regards,

    Lars
    Lars Uffmann, Jun 20, 2008
    #4
  5. Lars Uffmann

    Lars Uffmann Guest

    Re: API Calls in C++ (SOLUTION)

    One solution (and one that I can definitely live with) was to load the
    DLL at runtime and extract the functions entry point, as described in
    http://en.wikipedia.org/wiki/Dynamic-link_library#C_and_C.2B.2B_2
    In my case, this resulted in code like this:

    #include <windows.h>
    #include <stdio.h>

    // DLL function signature
    typedef int (*_AddHeaderAndUplinkCommand)(const char *, const char *,
    const char *, const unsigned char *, unsigned long);
    _AddHeaderAndUplinkCommand AddHeaderAndUplinkCommand

    HINSTANCE hinstLib;

    int loadAPIFunctions()
    {
    hinstLib = LoadLibrary("myLibrary.dll");
    if (hinstLib == NULL) {
    printf("ERROR: unable to load DLL\n");
    return 1;
    }

    // Get function pointer
    AddHeaderAndUplinkCommand =
    (_AddHeaderAndUplinkCommand)GetProcAddress(hinstLib,
    "AddHeaderAndUplinkCommand");
    if (AddHeaderAndUplinkCommand == NULL) {
    printf("ERROR: unable to find DLL function\n");
    FreeLibrary(hinstLib);
    return 1;
    }

    // Unload DLL file
    FreeLibrary(hinstLib); // not sure if this is a good idea if
    the function is still gonna be used... Any comments?

    return 0;
    }


    I'm currently not using the FreeLibrary call at this point in the
    application, rather before exiting. Anyways this works very well for
    me :)
    Lars Uffmann, Jun 20, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?aG93YXJkIGRpZXJraW5n?=

    API calls fail in ASP.Net

    =?Utf-8?B?aG93YXJkIGRpZXJraW5n?=, Jan 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,919
    bruce barker
    Jan 6, 2005
  2. Honne Gowda A
    Replies:
    2
    Views:
    853
    Karl Heinz Buchegger
    Oct 31, 2003
  3. andy6
    Replies:
    2
    Views:
    744
    andy6 via DotNetMonster.com
    Jun 9, 2006
  4. Richard Tobin
    Replies:
    24
    Views:
    758
  5. Bob
    Replies:
    5
    Views:
    240
Loading...

Share This Page