473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Late binding to a FORTRAN DLL routine with a string paramter

Hi
I'm trying to call into a FORTRAN DLL using ModuleBuilder, MethodBuilder and
then doing an Invoke to allow users to specify the Fortran DLL Signature etc.
at runtime. All works fine with ints and doubles etc., but I just can't get
the string to work (AccessViolationException).

I am including the hidden length parameter in a fortran call so it isn't that.

The FORTRAN method is defined as follows:

! Stringtest.f90
!
! FUNCTIONS/SUBROUTINES exported from Stringtest.dll:
! Stringtest - subroutine
!
subroutine Stringtest(STRING, ITEST, LENOUT)

! Expose subroutine Stringtest to users of this DLL
!
!DEC$ ATTRIBUTES DLLEXPORT::Stringtest

! Variables
CHARACTER , INTENT(IN) :: &
STRING*(*) ! String argument

INTEGER, INTENT(OUT) :: &
ITEST, & ! Some calculation on string
LENOUT ! Length of string as check

and the code I use to try and access it (in a simple console application) is
as follows:

string fileName =
Path.Combine(Path.GetDirectoryName(Assembly.GetExe cutingAssembly().Location),
"stringtest.dll");

string entryPointName = "STRINGTEST";
CallingConvention callingConvention = CallingConvention.Winapi;
CharSet charSet = CharSet.Ansi;

//...Set up the parameters:
Type[] parameterTypes = new Type[4];

parameterTypes[0] = typeof(string);
parameterTypes[1] = typeof(int);
parameterTypes[2] = typeof(int).MakeByRefType();
parameterTypes[3] = typeof(int).MakeByRefType();

AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "Assembly Name";
AssemblyBuilder dynamicAsm =
AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
dynamicAsm.DefineDynamicModule("StringTest");

MethodBuilder methodBuilder =
moduleBuilder.DefinePInvokeMethod(entryPointName,

fileName,

MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl,

CallingConventions.Standard,

typeof(void),

parameterTypes,

callingConvention,

charSet);
methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig);
moduleBuilder.CreateGlobalFunctions();

// Get a MethodInfo for the PInvoke method and store it away for
later use:
MethodInfo methodInfo = moduleBuilder.GetMethod(entryPointName);

//...Set up the parameters:
object[] parameterValues = new object[parameterTypes.Length];

parameterValues[0] = "Hello";
parameterValues[1] = 5;
parameterValues[2] = 0;
parameterValues[3] = 0;

ParameterModifier parameterModifiers = new ParameterModifier();

//object returnValue = typeof(void);

try
{
/*returnValue = */methodInfo.Invoke(null, parameterValues);

int loop = 0;
foreach (object parameterValue in parameterValues)
{
Console.WriteLine(String.Format("{0}. {1}", loop,
parameterValue.ToString()));
loop++;
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}

Console.ReadKey();
}

If anyone can shed any light on this that would be great.

Best regards

Marek
Oct 23 '07 #1
1 1544
Hi Marek,

Are you able to call it using DllImport? If DllImport works, it might be
related to the code that defines the dynamic PInvoke.

Are you using .NET 2.0, if yes, there's a better approach that using
Marshal.GetDelegateForFunctionPointer:

#Junfeng Zhang's Windows Programming Notes : Dynamic PInvoke
http://blogs.msdn.com/junfeng/archiv...14/181932.aspx

#Late binding with unmanaged code
http://www.msnewsgroups.net/group/mi...uages.csharp/t
opic19848.aspx
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Oct 24 '07 #2

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

Similar topics

4
by: Andy Franks | last post by:
Hi All, This is driving me nuts, especially since I have this working for my main application with no problem. I suspect it might be due to Namespace conflicts, but I'm not positive. Any help...
1
by: JD Kronicz | last post by:
Hi .. I have an issue I have been beating my head against the wall on for some time. I am trying to use late binding for MS graph so that my end users don't have to worry about having the right...
9
by: Zlatko Matić | last post by:
I was reading about late binding, but I'm not completely sure what is to be done in order to adjust code to late binding... For example, I'm not sure if this is correct: early binding: Dim ws...
30
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
6
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
9
by: Miro | last post by:
VB 2003 and Im still new to vb, so i hope i can explain this as best I can. I have a variable defined as such: ( simple example ) Dim AVariableOfSorts(,) As Object = _ { _ {"Last", "String",...
5
by: b00n1 | last post by:
I'm trying to create a simple vb.net (server) dll, and then through late binding access the public methods of the contained class from another vb.net (client) app. I can do it through early...
52
by: Nomad.C | last post by:
Hi I've been thinking of learning Fortran as number crunching kinda language for my Physics degree......but then looking around the internet, people are saying that the libraries/ Algorithms once...
4
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, what is the difference between the late binding and reflection? clara -- thank you so much for your help
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.