Compiler une dll pour Excel VBA
Bonjour à tous,
une question qui est entre du C++ et du VBA (j'ai choisi de la mettre ici) :
Je veux compiler une dll pour l'utiliser ensuite en VBA sous Excel. C'est la première fois que je fais ça et je veux juste assimiler le principe avec un exemple très simple.
J'ai créé un projet sous code block. Voici mon .h :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
int DLL_EXPORT carre(int chiffre);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__ |
et voici mon .cpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include "main.h"
// a sample exported function
int DLL_EXPORT carre(int chiffre)
{
return chiffre*chiffre;
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
} |
Je compile ma dll, ça a l'air de fonctionner. Je vais sous Excel et je déclare dans un module :
Option Explicit
Private Declare Function carre Lib "C:\Users\Nicolas\Desktop\DLL C++\TestDLL\bin\Release\TestDLL.dll" (ByVal chiffre As Integer) As Integer
Sub toto()
Debug.Print carre(10)
End Sub
J'obtiens l'erreur 49 :
Convention d'appel de dll incorrecte
J'ai tenté de changer le type des variables en double dans les deux (C et VBA), mais sans succès. Est ce que quelqu'un peut me dire d'où vient le problème ?
Je vous remercie