2
votes

I have created a 64bit DLL file using Microsoft Visual Studio 2015. Visual C --> Win32 Project.

The code for squareMain.c is as follows :

 long _stdcall square(long a)
 {
  return a*a;
 }

The code for Defile.def is as follows :

    LIBRARY "square"
    EXPORTS
    squareforExl=square

I have build the DLL using configuration manager settings set platform and active solution platform to x64 for 64bit DLL.

I have to access this DLL functions through Excel vba7. The function in VBA is as follows :

Private Declare PtrSafe Function squareforExl Lib "C:\math.dll" (ByVal x As Long) As Long

Sub square()
    Dim b As Long
    b = 5
    Dim c As Long
    Debug.Print b
    c = squareforExl(b)
    Debug.Print "Square : " & c
 End Sub

While trying to access DLL function squareforExl() as in the code above, it shows the following error msgBox:

Run time Error '53':  File not Found :"C:\math.dll"

I have tried placing file in different location directories, even in systems folder.

I have successfully compiled and executed 32bit DLL on in a 32 bit version of Office. My question is how can I access the DLL functions for a 64 bit dll in a 64 bit version of Office ?

1
You cannot call code from a 64-bit DLL in a 32-bit process, nor vice versa. You will need to change to a 64-bit version of Office, or re-compile the DLL. - Cody Gray
Yes I am trying to execute 32-bit dll in a 32-bit Office environment and trying to execute 64-bit dll in a 64-bit Office environment on two different machines. The C source code and .def source code have been added to the post. - Pranav Bhagwat
Since it says file does not exist, does C:\math.dll actually exist?Maybe you don't have permission to read this.Did you try C:\\math.dll? - Gaurav Sehgal
I have used C:\math.dll for 32-bit dll with 32-bit office environment. C:\\math.dll also works for 32-bit DLL. Also math.dll have been placed in D and E drives as well for both scenarios. Both above path changes have been done and the same problem persists for 64bit DLL on 64bit environment. - Pranav Bhagwat
"file not found" from the system means a lot. It's not "open failed" or something. I would put the dll in the system path and reference it without path, cheap & quick... - Jean-François Fabre

1 Answers

0
votes

Please Check your Office installation whether it is 64bit installation or 32bit installation. Meaning that, if VBA editor is in VBA7, it doesn't mean office is 64bit. It seems, you have 32bit office installation (i.e. later 2007 version, so that VBA7) on 64bit OS. Please create DLL in 32bit and use Ptrsafe in VBA (as it is VBA7), the error will be nullified. For 64bit Office Installation on 64bit OS, Calling 64bit DLL with Ptrsafe, will not have any issue. I'd both usages i.e.(1. 64bit office on 64bit os. 2. 32bit office with VBA7 on 64bit OS) and working.