Lars Uffmann <ar**@nurfuerspam.dekirjutas:
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?
Note that the behavior of linkers and specific platforms is off topic in
this group. I just make some general comments applicable to most cross-
compiler cases (I don't know the gnu details anyway).
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?
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. Note that if you manage to link the code it does not
necessarily mean it will work.
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.
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. Then you have correctly declare the function for your
code (possibly writing your own declaration) and correctly link to the
..LIB file. The _stdcall thing may complicate the issues. The details are
far too off-topic, when you have found out the exact exported name inside
the DLL you should ask a GNU group. Or you could ask help from the DLL
provider, of course (though they might not necessarily know anything
about GNU or g++).
A correct way to use a C++ interface from another implementation is to
write a wrapper library and compile it with the same compiler (and
version, and compile flags, etc) than the original library. The wrapper
has to provide a C interface instead, which can be called from code
compiled by other compilers (C ABI is standardized, more or less, for a
given platform, which is not the case for C++ ABI).
hth
Paavo