- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I want to simulate the callback mechanism with C and FORTRAN. The projects can be built successfully. But it still have a problem. The function event should print something but it doesn't. I am not a C expert. Anyone could help me?
In FORTRAN part:
module mymod
implicit none
interface
subroutine do_it(str,callback)
!dec$ attributes c :: do_it
!dec$ attributes reference :: str
implicit none
character(*)::str
external callback
end subroutine do_it
end interface
end module mymod
!_______________________
program test
use mymod
implicit none
character(40)::str
external event
str="Event is raised."
call do_it(str,event)
end program
!________________________
subroutine event(str)
implicit none
character(*)::str
print*,str
end subroutine event
!________________________
In C part:
typedef void (*CALLBACKPROC)(char* str);
extern "C"
{
void do_it(char* str,CALLBACKPROC callback)
{
callback(str);
}
}
I want to simulate the callback mechanism with C and FORTRAN. The projects can be built successfully. But it still have a problem. The function event should print something but it doesn't. I am not a C expert. Anyone could help me?
In FORTRAN part:
module mymod
implicit none
interface
subroutine do_it(str,callback)
!dec$ attributes c :: do_it
!dec$ attributes reference :: str
implicit none
character(*)::str
external callback
end subroutine do_it
end interface
end module mymod
!_______________________
program test
use mymod
implicit none
character(40)::str
external event
str="Event is raised."
call do_it(str,event)
end program
!________________________
subroutine event(str)
implicit none
character(*)::str
print*,str
end subroutine event
!________________________
In C part:
typedef void (*CALLBACKPROC)(char* str);
extern "C"
{
void do_it(char* str,CALLBACKPROC callback)
{
callback(str);
}
}
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I can't find CALLBACKPROC defined anywhere in the MSVC headers or documentation. But in Windows programming, callback procedures are usually STDCALL...
Steve
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve, there's typedef for CALLBACKPROC in the code ;-)
The problem is that it's wrong. You need instead (d'oh, I hate that syntax, I have to look back in the sample every time):
The problem is that it's wrong. You need instead (d'oh, I hate that syntax, I have to look back in the sample every time):
typedef void (__stdcall *CALLBACKPROC)(char*, int)i.e. it must match the prototype of Fortran routine Event. Passing characters between C and Fortran is a bit tricky as there's hidden argument describing length. There are many ways to solve it, depending on what you need with the argument (only reading, reading/writing, writing). One solution can be to terminate it with :
str="Event is raised."//char(0) call do_it(str, event) ... void do_it(char* str, CALLBACKPROC callback) {callback(str, strlen(str)); }Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you,Jugoslav. It works but I don't use __stdcall.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
__stdcall is the default convention for all CVF routines. You can change it in project settings though -- did you do that? (Project/Settings/Fortran/External procedures/Argument passing convention). If you didn't, you'd better either use __stdcall or change the setting above to "C, by reference", or you'll get a smashed stack.
Jugoslav
Jugoslav

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page