Thread: C++ Dll for Excel VBA

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    C++ Dll for Excel VBA

    Hello,
    I wrote a dll in C++ to be called from Excel VBA. Two main problem arise…
    1. Within the vba code there is the call to the dll. Well, after the execution of this line of code, I get an overflow error, I enter the debug mode and the it works fine again. It seems that the VBA code doesn’t wait for the dll to finish its computation.
    2. This dll routine is called thousands of time and I notice during the execution, that the memory allocation (from task manager) grow awfully, till excel crashes since there’s no more memory available.
    With other fortran dll, all this mess doesn’t happen!
    Do you know where the problems can lie?
    Thank you very much!!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Do you know where the problems can lie?
    Not without seeing the code.

    gg

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    It's hard to stick..it is a 500 line code... Sorry!

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Post

    Ok, I've slimmed a lot the original code...
    Within Excel, I run the dll about 30000 times and I notice a steady increase in the memory allocation of excel (from Task Manager). How can I avoid this problem?
    I suppose that, as soon as the dll has been terminated, the used memory is released, but it seems not to be the case...

    Code:
    void _stdcall AreaComputation (double *x,double *y,float omega,double *area,double *volume)
    {
    	double tarea=0.0,tvolume=0.0;
    	int i;
    	int ns=19;
    	int n=(ns-1)/2;
    	double *ra=new double[ns];
    	double *ha=new double[ns];
    
    	for(i=0;i<n;i++)
    	{
    		ha[i]=x[i];
    		ra[i]=y[i];
    		ha[ns-i-2]=-x[i];
    		ra[ns-i-2]=y[i];
    	}
    	ha[ns-1]=ha[0];
    	ra[ns-1]=ra[0];	
    
    	double Cy=0.0;
    	double pi=3.1415926535897;
    	omega=omega*pi/30.0;
    	for(i=0;i<ns-1;i++)
    	{
    		tarea=tarea+(ha[i]*ra[i+1]-ha[i+1]*ra[i]);
    		Cy=Cy+(ra[i]+ra[i+1])*(ha[i]*ra[i+1]-ha[i+1]*ra[i]);
    	}
    	tarea=tarea/2;
    	Cy=Cy/6/tarea;
    
    	tvolume=tarea*2*pi*Cy;
    	
    	double *dummy;
    	dummy=&tarea;
    	*area=*dummy;
    	dummy=&tvolume;
    	*volume=*dummy;
                    delete [] ra,ha;
                    
                    ra=NULL;
                    ha=NULL;
                    dummy=NULL;
    }

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> delete [] ra,ha;
    Make that
    Code:
    delete [] ra;
    delete [] ha;
    gg

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    That's great!!
    Thanx a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Passing UDTs from VBA to C DLL
    By Nelviticus in forum Windows Programming
    Replies: 0
    Last Post: 06-11-2003, 09:57 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM