1

I create a dll in c++. My dll exploits some methods of the gsl dll.

I call my dll in Excel and on my pc it works well, but if I try to call the dll from Excel in another pc returns an error:

"File not found".

All files are in the correct path.

What could be the problem??

Further informations:

I declare the function in Excel in the follow way:

Declare Function MY_DLL_P Lib "C:\Users\Baiso\Desktop\MY_DLL.dll" (ByVal file As String, ByRef results As Double) As Integer

This is a part of Excel function:

   sol = MY_DLL_P(objDom.XML, results(0))

    Debug.Print CStr(results(0))
    Debug.Print CStr(results(1))
    Debug.Print CStr(results(2))

    Debug.Print CStr(sol)

Dll is in the correct path.

In my c++ project this is the header file MY_DLL.h:

static __declspec(dllexport) int _stdcall MY_DLL_P(char* file, double* result); 

and this is MY_DLL.cpp file:

#include "MY_DLL.h"
#include "gsl\gsl_linalg.h"
#include "gsl\gsl_poly.h"



int MY_DLL_P(char* file, double* result)
{   

   ...

}

and this is the file.def

LIBRARY "MY_DLL"


EXPORTS
MY_DLL_P
Baiso
  • 79
  • 8
  • 3
    *All files are in the correct path.* The evidence suggests otherwise. Without you supplying any details we cannot give you detailed help. Clearly the problem is that a file could not be found. If you want more help than that you'll need to give more information. You will need to investigate further. – David Heffernan Oct 24 '14 at 10:07
  • Add information. @DavidHeffernan – Baiso Oct 24 '14 at 11:22
  • What file is not found? What part of the system (Excel, the DLL, or something else) is generating the file not found error message? If everything works in one environment and when you move to another PC, another environment, and it does not work then that indicates that there is something missing in the new environment, the new PC, that was available in the original environment. So you need to determine what is different. One thing to try is a dependency walker [how to check for dll dependency](http://stackoverflow.com/questions/7378959/how-to-check-for-dll-dependency) – Richard Chambers Oct 24 '14 at 11:33
  • You may also want to take a look at this Microsoft article, [Understanding the Dependencies of a C++ Application](http://msdn.microsoft.com/en-us/library/ms235265.aspx) – Richard Chambers Oct 24 '14 at 11:36

2 Answers2

4

The most likely explanation is that the dependencies of your DLL are failing to be resolved. Either:

  1. The GSL libraries cannot be located on the target machine, or
  2. The MSVC runtime cannot be located on the target machine.

The module loader will attempt to resolve the GSL libraries using the Dynamic-Link Library Search Order. You'll need to make sure that the GSL libraries can be found that way.

As for the MSVC runtime, you need to make sure that is available on the target machine. Typically you do that by installing the MSVC runtime redistributable package on the target machine.

You can use a tool like Dependency Walker to help you diagnose these kind of issues.

One other possibility is that the error comes from the function that you implemented, MY_DLL_P. Perhaps the file that cannot be found is that which you passed to MY_DLL_P in its first parameter.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • MY_DLL_P, I think works well because in my pc it is ok. Now, in the target pc I installed the MSVC runtime and I put the directory gsl in the path C:\CPP\gsl and I find the follow error message: CAN'T FIND DLL ENTRY POINT. Have you some suggestion? – Baiso Oct 24 '14 at 12:03
  • Fine. I'm not suggesting otherwise. – David Heffernan Oct 24 '14 at 12:04
  • I create a simple dll conteining the square function, that is no longer dependent on gsl library. I installed the MSVC runtime and I checked that in the directory C:\Windows\SysWOW64 I have the msvcr110.dll and msvcp110.dll. But I obtain the same error: FILE NOT FOUND. Could it be a problem concerning the Microsoft Update? – Baiso Oct 27 '14 at 16:54
  • Microsoft Update? Doubtful. There's no need to guess and be helpless. Use Dependency Walker in Profile mode. Possibly you are building the DLL in debug mode and so it depends on the debug version of the runtime. Fix that by building release mode DLL. – David Heffernan Oct 27 '14 at 17:08
  • In MY_DLL using Dependency Walker, I don't have the dependency with gsl, I only have the dependency with msvcr110, msvcp110 and kernel32. My dll is 64bit. Why? Have you some suugestion? – Baiso Oct 28 '14 at 08:21
  • Er, is excel 64 bit too? – David Heffernan Oct 28 '14 at 08:22
  • And you installed the 64 bit msvc runtime? – David Heffernan Oct 28 '14 at 08:27
  • Yes. Yes. And on my pc runs well. In another pc 64bit, excel 64 with installed 64bit msvc runtime doesn't run. – Baiso Oct 28 '14 at 08:38
  • You still have not shown the complete verbatim error message that you receive. It's hard to help unless you supply precise details. – David Heffernan Oct 28 '14 at 09:14
1

You cannot declare the function like this:

Declare Function MY_DLL_P Lib "C:\Users\Baiso\Desktop\MY_DLL.dll" (ByVal file As String, ByRef results As Double) As Integer

and expect it to work on anyone's PC but yours. You have explicitly called out 'Baiso' as the user folder.

Stewbob
  • 16,759
  • 9
  • 63
  • 107
  • @Baiso, I think I have not communicated my point properly. The folder 'C:\Users\Baiso....' only exists on your PC. On other PC the folder will have a different name, so a 'file not found' error is expected. – Stewbob Oct 24 '14 at 12:17
  • On the other machine he changes the VBA to have the appropriate path – David Heffernan Oct 24 '14 at 12:52