Menu

dll for VBA

muster
2006-02-09
2012-09-26
  • muster

    muster - 2006-02-09

    Hi,

    Anybody has experience of making a dev c++ dll for VBA application? Please give me a hint.

    Thank you.

     
    • Nobody/Anonymous

      Could you be more specific about the problem?

      It is usually the calling program that needs to adjust to the dll. There are some differences in the way variables are stored that must be considered. And dlls typically use stdcall. With a custom dll for a specific application, you can do just about anything you want. Without knowing more about the problem it is hard to make recommendations.

       
    • Nobody/Anonymous

       
    • muster

      muster - 2006-02-10

      thank you guys, and sorry,

      I know the _stdcall thing and basically I can make a dll for VBA using VC++, by starting with an empty project, plus a DEF file.

      It seems VBA really needs that DEF file, but DEV C++ doesn't like it. So this is my biggest issue right now.

      The link Lan provided is good and I'll read it. Please keep pouring more quick hints here.

      Thanks again,

       
    • Nobody/Anonymous

      If name mangling is a problem compile the dll with the linker options

      --add-stdcall-alias
      --no-export-all-symbols

      Then look at the output DEF file from the dll compile.

       
    • marcelok

      marcelok - 2006-02-11

      I have succeeded in making a dll which export a function to MS Excel

      add this 2 files "mydll.cpp" and "mydll.h" to a dev-c++ project and compile to a dll.

      The declaration of the function in VBA is as follows

      Public Declare Function NormalDeviate Lib "SearchPatch" (ByVal Mean As Double, ByVal Std As Double) As Double

      The function NormalDeviate returns a Random Normal deviate with specified mean and standard deviation to MS Excel.

      Good luck!

      This is the code in file "mydll.cpp"

      include "mydll.h"

      include <windows.h>

      include <math.h>

      include <time.h>

      DllClass::DllClass()
      {
      }

      DllClass::~DllClass ()
      {
      }

      BOOL APIENTRY DllMain (HINSTANCE hInst / Library instance handle. / ,
      DWORD reason / Reason this function is being called. / ,
      LPVOID reserved / Not used. / )
      {
      switch (reason)
      {
      case DLL_PROCESS_ATTACH:
      break;

        case DLL_PROCESS_DETACH:
          break;
      
        case DLL_THREAD_ATTACH:
          break;
      
        case DLL_THREAD_DETACH:
          break;
      }
      
      /* Returns TRUE on success, FALSE on failure */
      return TRUE;
      srand( (unsigned)time( NULL ) );
      

      }

      define dllexport __declspec(dllexport)

      extern "C"
      {
      double unirand()
      {
      return (rand()+1.0)/(RAND_MAX+1.0);
      }

         dllexport double _stdcall NormalDeviate (double Mean, double Std)
         {
         return Mean + sqrt(-2.0 * log(unirand())) * cos(2.0 * 3.141516 * unirand())* Std;
         }
      

      }

      This is the code in file "mydll.h"

      ifndef DLL_H

      define DLL_H

      if BUILDING_DLL

      define DLLIMPORT __declspec (dllexport)

      else / Not BUILDING_DLL /

      define DLLIMPORT __declspec (dllimport)

      endif / Not BUILDING_DLL /

      class DLLIMPORT DllClass
      {
      public:
      DllClass();
      virtual ~DllClass(void);

      private:

      };

      endif / DLL_H /

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.