473,699 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing address of a C function to Fortran - syntax??

I am trying to pass the address of a C++ function into a Fortran
routine to enable the Fortran routine to call this C++ function. I
have to do it this way as our build process does not allow circular
dependencies of DLL's.

Does anyone know how to do this, I have tried everything in my book.

I have a C++ function GetP:

DllExport void GetP()
{
...code...
return;
}

I am trying to pass the address of this function into a Fortran
routine called BIND_FTN. The call is made from another area of the
C++ code as follows:

void (GetP)();
DllImport void __stdcall BIND_FTN(int* GetP);

void Initialize()
{
...code...
BIND_FTN(GetP); // inform Fortran of address of callback
function
return;
}

The C++ will not compile, I get:
error C2664: 'BIND_FTN' : cannot convert parameter 1 from 'void
(__cdecl *)(void)' to 'int *'

Can anyone see the problem here?

Here is the Fortran code for BIND_FTN:

subroutine BIND_FTN_EXTERN ALS[DLLEXPORT](ext)
external ext

interface
subroutine cb_GetP()
!DEC$ ATTRIBUTES STDCALL :: cb_GetP
end subroutine cb_GetP
end interface

pointer (p_GetP, cb_GetP)
p_GetP = loc(ext)

call cb_GetP ! call the C++ function from Fortran

return
end

(I haven't got to the Fortran yet - there may be syntax errors in
there)

Adrian
Nov 14 '05 #1
5 2333
On 23 Nov 2004 14:31:57 -0800, ac***@hotmail.c om (Adrian) wrote in
comp.lang.c:
I am trying to pass the address of a C++ function into a Fortran
Your header said a C function, now you say C++ function. C++
functions, and C++ itself for that matter, are off-topic in
comp.lang.c. But before you run off to comp.lang.c++.. .
routine to enable the Fortran routine to call this C++ function. I
have to do it this way as our build process does not allow circular
dependencies of DLL's.
Neither C nor C++ defines an interface to any other language.
Does anyone know how to do this, I have tried everything in my book.


Perhaps you need another book.

In any case, this is not a language question (C, C++, or FORTRAN) at
all. This is a question about specific compiler and linker operations
for your particular platform.

You need to ask in one or more groups that support the particular C++
and FORTRAN compilers involved.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
Jack Klein <ja*******@spam cop.net> wrote in message news:<uu******* *************** **********@4ax. com>...
On 23 Nov 2004 14:31:57 -0800, ac***@hotmail.c om (Adrian) wrote in
comp.lang.c:
I am trying to pass the address of a C++ function into a Fortran
Your header said a C function, now you say C++ function. C++
functions, and C++ itself for that matter, are off-topic in
comp.lang.c. But before you run off to comp.lang.c++.. .


Doesn't matter - it's actually C code but it exists in a C++
application - I posted it here because it is really a C question -
there is nothing C++ specific about it.
routine to enable the Fortran routine to call this C++ function. I
have to do it this way as our build process does not allow circular
dependencies of DLL's.
Neither C nor C++ defines an interface to any other language.


Wrong. I have called Fortran routines from C / C++ for years.
Does anyone know how to do this, I have tried everything in my book.


Perhaps you need another book.


Your sarcasm is not appreciated - I came here for advice not cynical
comment.

In any case, this is not a language question (C, C++, or FORTRAN) at
all. This is a question about specific compiler and linker operations
for your particular platform.

You need to ask in one or more groups that support the particular C++
and FORTRAN compilers involved.


If you don't have any constructive comment and certainly if you don't
know what I'm talking about, it's best not to post.

Adrian
Nov 14 '05 #3
On Wed, 24 Nov 2004 07:51:50 -0800, Adrian wrote:
Jack Klein <ja*******@spam cop.net> wrote in message news:<uu******* *************** **********@4ax. com>...
On 23 Nov 2004 14:31:57 -0800, ac***@hotmail.c om (Adrian) wrote in
comp.lang.c:
> I am trying to pass the address of a C++ function into a Fortran


Your header said a C function, now you say C++ function. C++
functions, and C++ itself for that matter, are off-topic in
comp.lang.c. But before you run off to comp.lang.c++.. .


Doesn't matter - it's actually C code but it exists in a C++
application - I posted it here because it is really a C question -
there is nothing C++ specific about it.


Except that C++ has an extern "???" mechanism whereas C does not,
typically code that interfaces to other languages that is compiled with a
C++ compiler will need to be be different to such code compiled with a C
compiler.
> routine to enable the Fortran routine to call this C++ function. I
> have to do it this way as our build process does not allow circular
> dependencies of DLL's.


Neither C nor C++ defines an interface to any other language.


Wrong. I have called Fortran routines from C / C++ for years.


Jack is correct, noting that C++ has extern "C" but of course in this
context C isn't an "other" language.

You need to understand the distinction between what the language supports
and what a particular compiler/implemnentation supports. For example in
your code you have DllExport which is not a part of either the C or C++
languages, I'm guessing it is an extension provided by some Windows
related compiler/implementation you are using.
> Does anyone know how to do this, I have tried everything in my book.


Perhaps you need another book.


Your sarcasm is not appreciated - I came here for advice not cynical
comment.


Whether you consider it sarcastic or not, it could very well be what you
need.
In any case, this is not a language question (C, C++, or FORTRAN) at
all. This is a question about specific compiler and linker operations
for your particular platform.

You need to ask in one or more groups that support the particular C++
and FORTRAN compilers involved.


If you don't have any constructive comment and certainly if you don't
know what I'm talking about, it's best not to post.


Jack gave you the best possible advice. Neither the C nor C++ languages
address your question, you need to find a platform-specific answer that
does. You need to find the experts in your platform in a newsgroup where
such discussion is topical. There are *lots* of Windows related newsgroups
available, maybe try something like comp.os.ms-windows.program mer.win32,
or one of the microsoft.publi c.* newsgroups.

Lawrence

Nov 14 '05 #4


Lawrence Kirby wrote:
On Wed, 24 Nov 2004 07:51:50 -0800, Adrian wrote:
Jack Klein <ja*******@spam cop.net> wrote in message news:<uu******* *************** **********@4ax. com>...
On 23 Nov 2004 14:31:57 -0800, ac***@hotmail.c om (Adrian) wrote in
comp.lang.c:

> I am trying to pass the address of a C++ function into a Fortran

Your header said a C function, now you say C++ function. C++
functions, and C++ itself for that matter, are off-topic in
comp.lang.c. But before you run off to comp.lang.c++.. .


Doesn't matter - it's actually C code but it exists in a C++
application - I posted it here because it is really a C question -
there is nothing C++ specific about it.


Except that C++ has an extern "???" mechanism whereas C does not,
typically code that interfaces to other languages that is compiled with a
C++ compiler will need to be be different to such code compiled with a C
compiler.
> routine to enable the Fortran routine to call this C++ function. I
> have to do it this way as our build process does not allow circular
> dependencies of DLL's.

Neither C nor C++ defines an interface to any other language.


Wrong. I have called Fortran routines from C / C++ for years.


Jack is correct, noting that C++ has extern "C" but of course in this
context C isn't an "other" language.

You need to understand the distinction between what the language supports
and what a particular compiler/implemnentation supports. For example in
your code you have DllExport which is not a part of either the C or C++
languages, I'm guessing it is an extension provided by some Windows
related compiler/implementation you are using.
> Does anyone know how to do this, I have tried everything in my book.

Perhaps you need another book.


Your sarcasm is not appreciated - I came here for advice not cynical
comment.


Whether you consider it sarcastic or not, it could very well be what you
need.
In any case, this is not a language question (C, C++, or FORTRAN) at
all. This is a question about specific compiler and linker operations
for your particular platform.

You need to ask in one or more groups that support the particular C++
and FORTRAN compilers involved.


If you don't have any constructive comment and certainly if you don't
know what I'm talking about, it's best not to post.


Jack gave you the best possible advice. Neither the C nor C++ languages
address your question, you need to find a platform-specific answer that
does. You need to find the experts in your platform in a newsgroup where
such discussion is topical. There are *lots* of Windows related newsgroups
available, maybe try something like comp.os.ms-windows.program mer.win32,
or one of the microsoft.publi c.* newsgroups.

Lawrence


This becomes a FORTRAN question.

"extern C" tells the compiler that the the parameters a push on the stack (assuming there is one) the C way,
not the C++ way. It is the FORTRAN compiler than must call the foreign library.
Nov 14 '05 #5
In article <41************ ***@mail.asb.co m> ns*@mail.asb.co m writes:
Lawrence Kirby wrote: ....
> Neither C nor C++ defines an interface to any other language.

Wrong. I have called Fortran routines from C / C++ for years.

Might be the case, that does not mean that the way to do it is platform
independent. It is not. And the way to do it may well depend on the
kind of arguments you are passing (especially strings are tricky).
Jack is correct, noting that C++ has extern "C" but of course in this
context C isn't an "other" language. .... Jack gave you the best possible advice. Neither the C nor C++ languages
address your question, you need to find a platform-specific answer that
does.

.... This becomes a FORTRAN question.

"extern C" tells the compiler that the the parameters a push on the stack
(assuming there is one) the C way, not the C++ way. It is the FORTRAN
compiler than must call the foreign library.


Let me expand on an earlier comment (see above):
*Neither C nor C++ nor Fortran defines an interface to any other language.*
The best way is (as remarked before) to ask in a platform or compiler
specific platform. BTW, I have worked on platforms were it was simply
impossible to do directly. You had to write an interface routine in
assembler to properly set up stack and heap environment for the C routines.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #6

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

Similar topics

5
7088
by: Wei-Chao Hsu | last post by:
Hi, I try to put an address of object into an int. Like this myclass obj; int address; address=&obj; "myclass" is an object of class or struct. It seems to illegal to C++. Is there any way to do this task?
3
2867
by: Pushkar Pradhan | last post by:
I need to pass the STL linked list to a function, this function should modify the linked list. So do I need to pass it by address, this is how I do it: void qhull(PARTICLE S, int len, list<PARTICLE> &hull, list<PARTICLE>::iterator &iter1, list<PARTICLE>::iterator &iter2, PARTICLE a, PARTICLE b, int rank, int numtasks) I think that's working but I have another problem: I need to pass two
58
10140
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
6
3525
by: Adrian | last post by:
I am trying to pass the address of a C++ function into a Fortran routine to enable the Fortran routine to call this C++ function. I have to do it this way as our build process does not allow circular dependencies of DLL's. Does anyone know how to do this, I have tried everything in my book. I have a C++ function GetP: DllExport void GetP()
1
6429
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
6
6293
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name like ZIP CODE or ADDRESS LINE. I will call the function with three parameter--UpdateAddress(5,zip_code,person_id) where 5 indicates ZIP_CODE is the fifth column in the table. If 4 is passed, it indicates the address line is to be updated. ...
23
4012
by: David Frank | last post by:
How can I write a string function that encloses the input string in quotes "string" ?? below works for the "123 operation but adding " to it clobbers the "123 main() { char *sfun(char *); printf("%s",sfun("123"));
2
4432
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
5
1471
by: CapCity | last post by:
I'm sure I'm missing something simple - I do not code in C regularly, and the breaks are long enough for me to forget. The situation I have is I need to create an array but I do not know the dimension until runtime. I pass the pointer to a function which then determines the size and then creates and populates it. I can walk the array OK in the function, but the app crashes when I try to do so in the calling routine. Here's a small...
0
8689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2010
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.