473,498 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

API Calls in C++

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
Jun 27 '08 #1
6 2619
"Lars Uffmann" <ar**@nurfuerspam.dewrote in message
news:6b*************@mid.dfncis.de...
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?
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.
>
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 );
/* ********************** */
Somewhere there needs to be a file (either source or compiled)
that contains the implementation of the function 'UplinkUserCommand()'.
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.
>
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?
Some kind of library, might or might not be a shared library (known
as 'DLL' in Windows-Land).

Did not this package provide any documentation?

-Mike

Jun 27 '08 #2
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
Jun 27 '08 #3
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
Jun 27 '08 #4
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
Jun 27 '08 #5
Lars Uffmann <ar**@nurfuerspam.dekirjutas:
Paavo Helde wrote:
[...]
>
>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++?
The library can be implemented, but what is important is the API
interface which can still be C. This is achieved by declaring API
functions extern "C". From the #ifdef snippet from an earlier post I got
a feeling that extern "C" might probably not be used in _WIN32 branch, so
I was not sure if this is the case or not.
>
>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?
Yes, if they are using C-style interface functions.
>
>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 :)
OK. If the function name exported by DLL is just UplinkUserCommand or
_UplinkUserCommand, then it's C ABI and should be callable from other
compilers. If its something like UplinkUserCommand@24, then it is the
__stdcall ABI and the calling compiler has to support this kind of
calling convention. If it is like ??4UplinkUserCommand@@QAEAAV01@ABV01@@Z
or you can see the actual parameter list in the Dependency Walker, then
it is C++ and you are on your own. You might be able to call the function
by using the mangled name and declaring this as extern "C", but this is
not guaranteed to work.

hth
Paavo
Jun 27 '08 #6
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..._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(hinstLi b,
"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 :)
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3404
by: Gil | last post by:
I am running a C++ process on Solaris that needs to find out how much diskspace is free and used on the system. Is 'system' in stdlib.h the only way to make OS calls from C++? In this case,...
9
1715
by: Keith Briggs | last post by:
How can I trap library calls, say to puts, and get them to call a puts function I write, which then performs some special action before calling the real puts from libc? I tried all kinds of...
5
3503
by: markus | last post by:
Hi, I have a question that deals with the standard c library VS (Unix) system calls. The question is: which header files (and functions) are part of the C library and which header files (and...
2
1527
by: ghost | last post by:
As the Subject indicates I have written a web page that makes use of several web service calls. Some of the web service calls are very fast so they called synchronously. Some web service calls...
11
6918
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then...
6
2104
by: A.M-SG | last post by:
Hi, We are developing a SmartClient application and we are planning to expose business objects layer to SmartClient application by using ASP.NET SOAP web services.
10
2608
by: Brian Parker | last post by:
I inherited a C++ DLL that I need to remotely call multiple times asynchronously. What I have developed is: CSharp web application that makes asynchronous calls to a CSharp Web Service. The...
6
2249
by: Dasn | last post by:
Hi, there. 'lines' is a large list of strings each of which is seperated by '\t' I wanna split each string into a list. For speed, using map() instead of 'for' loop. 'map(str.split, lines)'...
21
2423
by: omkar pangarkar | last post by:
Hi all, I have two simple hello world programs one using printf() and other using write() --prog 1-- #include<stdio.h> #include<stdlib.h> int main() { printf("Hello"); /* up to here...
1
1959
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... There are a few questions wrapped up in this, but the main one is that the WebService.MyMethodAsync() methods that are automatically generated in the client code by VS 2005 don't seem to...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.