1

I am trying to call a dll file created in vb6 from F#. I have written the following dll.

Public Function AddTwoNumbers(ByVal a As Integer, ByVal b As Integer)
    AddTwoNumbers = a + b
End Function

Now I want to call it in my F# program, I wrote this code

open System.Runtime.InteropServices

module InteropWithNative =
   [<DllImport(@"C:\add", CallingConvention = CallingConvention.Cdecl)>]  
   void AddTwoNumbers(int, int)

InteropWithNative.AddTwoNumbers(3,4)

let result = AddTwoNumbers_ 2.0 3.0

It gives me errors and doesn't recognize the function.

Functional_S
  • 1,159
  • 6
  • 9
biscuit
  • 21
  • 3
  • Please at least state the errors – John Palmer Oct 30 '15 at 23:08
  • Unexpected keyword 'void' in implementation file Incomplete structured construct at or before this point in definition – biscuit Oct 30 '15 at 23:11
  • Please suggest me a better way to call a dll funtion through F# as well – biscuit Oct 30 '15 at 23:13
  • 3
    Seems to (at least) miss the [extern](https://msdn.microsoft.com/en-us/library/dd393785.aspx) before `void` (which shouldn't be `void` but `int` because the VB code is a Function not a Sub – Sehnsucht Oct 31 '15 at 00:02
  • 1
    Just out of curiosity, why are you trying to create a library in age-old VB6? Better to either convert any existing VB6 to VB.NET so that you can call the methods directly, or switch to another language if you need native code. – Abel Oct 31 '15 at 05:48
  • 1
    Define the EntryPoint="AddTwoNumbers" in [] – Functional_S Oct 31 '15 at 08:33
  • @Abel > we have a project in vb6 and want to convert it to F#. Our First approach is to convert it to dll and call it from the F#. Later we will convert the whole vb6 project to F#. – biscuit Nov 02 '15 at 17:12
  • @Functional_S > I tried the entry point method, but it never works. Can you paste here a running code as an example. – biscuit Nov 02 '15 at 17:17

1 Answers1

2

A working interop example with an EntryPoint

open System.Runtime.InteropServices // for DllImport

module KernelInterop = 
    [<DllImport("kernel32.dll", EntryPoint="Beep")>]
    extern void Beep( int frequency, int duration )

KernelInterop.Beep  // val Beep : int * int -> unit
KernelInterop.Beep(440, 1000)
Functional_S
  • 1,159
  • 6
  • 9
  • @Funtional_S > The above code works well, and I played it. But how can I use it for the question, open System.Runtime.InteropServices module InteropWithNative = [] void AddTwoNumbers(int, int) InteropWithNative.AddTwoNumbers(3,4) let result = AddTwoNumbers_ 2.0 3.0 – biscuit Nov 02 '15 at 18:55
  • An unhandled exception of type 'System.BadImageFormatException' occurred in ConsoleApplication32.exe Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) – biscuit Nov 03 '15 at 17:39
  • This may help [http://stackoverflow.com/questions/2728560/badimageformatexception-when-loading-32-bit-dll-target-is-x86] – Functional_S Nov 03 '15 at 20:06
  • :> I read the article. It focuses only on changing the compiler mode from x64 to x84. Afterwards, I tried to run the code and still get the same error. Can you please test it on your system to see.. module InteropWithNative = [] extern int32 AddTwoNumbers(int32 a, int32 b) let result = InteropWithNative.AddTwoNumbers(2,5) printf "Result is: %d" result – biscuit Nov 03 '15 at 21:59