Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Multi-Language Programming: More Complicated Function Calls Between Languages: 5

, 20 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Doing more complicated function calls between languages.

Components or libraries provide some functions. Other modules may access these functions through their interface. In some cases, the interface functions may be simple. The caller constructs input arguments, calls the function, and gets the return value. These type interfaces are easy to define and have no more restrictions. But in other cases, interface functions may be more complicated. Let’s use an example to explain this in detail.

Suppose that there is a C/C++ component. It provides a function to draw and fill a circle into memory pixel buffer. If the component uses a simple interface, it should take parameters as input arguments, such as line type, color, width, fill color, fill pattern, fill bitmap, etc. We define an interface object that contains this function in a common object environment. The function may be called by other languages, for example, Java.

q8EqOmMQia9ilVbtCJZzeQ.jpg

In this case, there are some disadvantages. First, the interface function may have many input arguments, and for each call, these arguments should be input although their values do not change. Second, if a new parameter is added, all related functions must be changed. This will increase the burden of software maintenance.

In general, we often define two other objects: pen and brush objects. The draw function takes pen and brush objects as input arguments. The pen object has attributes color, type, and width, and the brush object has attributes color, pattern, image, etc. We also define an interface object that contains the draw function in the common object environment. As a result, there are three objects defined by C/C++ in the environment, which are pen, brush, and the object containing the draw function. For other languages using the function, for example Java, it should create instances of the three interface class objects. And the instances can be accessed by C/C++ and Java language based on the support of the environment.

9aSlrxnVah9d9gnskJ8z5A.jpg

In this case, the function call is more complicated. The caller needs to create instances of objects defined in C/C++. The instances can be accessed by the two languages. They are used as input arguments of the draw function. Without the support of common object environment, it is difficult to realize it in this way, especially to support the call from applications programmed with multiple different languages.

It is clear that the C/C++ service can be called by any other language. Is it easy and concise? You can compare it with traditional methods using the JNI interface.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

li9705

China China
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 20 Oct 2011
Article Copyright 2011 by li9705
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid