Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / October 2007

Tip: Looking for answers? Try searching our database.

Late binding to a FORTRAN DLL routine with a string paramter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marek - 23 Oct 2007 14:38 GMT
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.GetExecutingAssembly().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(assemblyName,
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(MethodImplAttributes.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
Walter Wang [MSFT] - 24 Oct 2007 07:24 GMT
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/archive/2004/07/14/181932.aspx

#Late binding with unmanaged code
http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/t
opic19848.aspx

Regards,
Walter Wang (wawang@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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.