For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2004 > calling a dll fortran with wxwindows









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author calling a dll fortran with wxwindows
rom

2004-04-29, 8:10 am

Hi,


I use wxwindows for my application. I need to call a dll in fortran
but I have problem with stings.
My dll has a simple subroutine witch accept :
i (long)
r (real, 4 octets)
d (double précision)
str (string, 128 octets)

It returns a file 'dlltest.out' with the values of i,r,d and str.


I use this code im my program to load the dll:


wxDllType mydll=wxDllLoader::LoadLibrary("test.dll");

typedef void (*inti)(long*,float*,double*,char *,int);
inti pint;
pint = (inti) wxDllLoader::GetSymbol(mydll,"DLLTEST");


long a;
a=12;
float b;
b=123;
double c;
c=1234;

char st[13];
strcpy(st,"Testing...");




pint(&a,&b,&c,st,13);
wxDllLoader::UnloadLibrary(mydll);}


It creates a file 'dlltest.out'. Its ok for a,b and c, but it write st
with many bad characters after "Testing...". I thinks its a problem of
lengh but I don't know how to solve it.

thanks
Jugoslav Dujic

2004-04-29, 8:10 am

rom wrote:
| Hi,
|
|
| I use wxwindows for my application. I need to call a dll in fortran
| but I have problem with stings.
| My dll has a simple subroutine witch accept :
| i (long)
| r (real, 4 octets)
| d (double précision)
| str (string, 128 octets)
|
| It returns a file 'dlltest.out' with the values of i,r,d and str.
|
<snip>
| typedef void (*inti)(long*,float*,double*,char *,int);
<snip>
| pint(&a,&b,&c,st,13);
| wxDllLoader::UnloadLibrary(mydll);}
|
|
| It creates a file 'dlltest.out'. Its ok for a,b and c, but it write st
| with many bad characters after "Testing...". I thinks its a problem of
| lengh but I don't know how to solve it.

It's not problem of length -- the calling code looks correct on the
first sight, but the inherent problem is that Fortran does *not*
terminate strings with \0. Instead, Fortran strings are fixed-length
and typically padded with blanks at the end (thus the need to
pass the buffer length all around, but on the other-hand they're
quite safe from notorious C buffer overflow problems).

If you have Fortran source, you might append char(0) to the TRIMmed
string, or roll your own C convertor routine, like:

char* FtoCstring(char* st, int len)
//appends the \0 at the very end
{
st[len-1] = 0;
return st;
}

or

char* FtoCtrim(char* st, int len)
//inserts \0 instead of first trailing blank; fails if no room
//Written ad hoc so check the logic.
{

while(int i=len-1; i>=0; i--)
{
if (((st[i] != ' ') && (i<len-1)) || i==0)
{
st[i+1] = 0;
return st;
}
}
return NULL;
}

--
Jugoslav
___________
www.geocities.com/jdujic

Please reply to the newsgroup.
You can find my real e-mail on my home page above.

Pablo

2004-05-04, 3:05 pm

mrom34@wanadoo.fr (rom) wrote in message news:<553a2fcf.0404290138.7ea30e59@posting.google.com>...
> Hi,
>
>
> I use wxwindows for my application. I need to call a dll in fortran
> but I have problem with stings.


Well... now it's called "wxWidgets".

Pablo
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com