Thread: From C to DLL

  1. #1
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15

    From C to DLL

    I have written a code for maximum power point tracking. The purpose was to create DLL file and use it in a circuit base simulation program. I used Microsoft visual c++ 2008 express and created that DLL file but I only get first value from the created array in C. I mean circuit based simulation program only get first value (although I see all array values after executing in "ChSciTE"). Do I need to store data somewhere? I am kind of rookie about writing C code? The code is below and the algorithm is attached as pdf. If anyone help me I would be so happy... I have been playing with this for 3 weeks...

    Code:
    #include <math.h>
    
        main() {
    
    	
        static double y[21];                       //controlled variable
        static double m_A[21];              //modulation index          
        double dm_A=0.05;                   //small change for modulation index
        int n;
        double i_cell[]={4.65, 4.68, 4.7, 3.7, 2.1, 2.8,4.68, 4.65, 3.9, 3.7, 2.1, 2.8,4.68, 4.65, 3.9, 4.3, 2.1, 3.3, 4, 3.7,4.6}; 
    	
            
            
            for (n=1;n<=19;n++){
            
            m_A[0]=0.75;
            m_A[1]=0.65;
                
            y[n-1]=i_cell[n-1]*m_A[n-1];                 //estimated initial controlled variable
            y[n]=i_cell[n]*m_A[n];  
    
        if (((m_A[n]>=m_A[n-1]) && (y[n]>=y[n-1])) ||    //algorithm for maximum power point tracking
            (((m_A[n]<m_A[n-1]) && (y[n]<y[n-1]))))	
                       
            m_A[n+1]=m_A[n]+dm_A;           //increase modulation index
                                             
        else                                //while one of them is increasing and the other one decreasing
             
            m_A[n+1]=m_A[n]-dm_A;           //decrease modulation index  
      
     
            printf("&#37;f \n",m_A[n+1]);       //display modulation indices
                
                            }
                }
    The code above for seeing results in "ChSciTE" program but the one I pasted below creates that DLL file to use in simulation.

    Code:
    #include <math.h>
    __declspec(dllexport) void simuser (t, delt, in, out)
    double t, delt;
    double *in, *out;
    {
    
    
    	static double y[21];				//controlled variable
    	static double m_A[21];				//modulation index
    	double i_cell=4.65;			       //estimated initial current value
    	int n;
    		
    		
    	for (n=1;n<=19;n++){
    		m_A[0]=0.75;					//estimated initial current value
    		m_A[1]=0.65;					//second estimated initial current value
    	
    		y[n-1]=i_cell*m_A[0];			//estimated initial controlled variable
    		y[n]=in[0]*m_A[n];				//in[0] current input value which comes from simulation 					
    
    	
    	if (((m_A[n]>=m_A[n-1]) && (y[n]>=y[n-1])) || 
    		(((m_A[n]<m_A[n-1]) && (y[n]<y[n-1]))))	
                
    		m_A[n+1]=m_A[n]+in[1];			//in[1] which is input 2 from circuit based simulation
    	
    	else								//while one of them increases and the other one decreases
            m_A[n+1]=m_A[n]-in[1];			//decrease modulation index
    
    	out[0]=m_A[n+1];					// out[0] is output which is transfered to the simulation with DLL
    	
    						}		
    		 	
    
    }
    Last edited by gurhan; 05-04-2008 at 06:36 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15
    I also added there before. Sorry for any inconvenience.

  4. #4
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15
    in[0]: first input comes from simulation program
    in[1]: second input comes from simulation program
    out[0]: output from C code or DLL file goes to simulation or taken by simulation program

    Those values taken by simulation should be m_A[n+1]? As far as I understand which is an array. However only the first, I mean m_A[1+1] = m_A[2] has taken by simulation. How could I write the code all values from m_A[2], m_A[3]........m_A[20] can be taken by simulation or DLL sends those values to simulation program one by one for every 20 ms or different interval...

    http://cboard.cprogramming.com/showt...=102217&page=2

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by gurhan View Post
    in[0]: first input comes from simulation program
    in[1]: second input comes from simulation program
    out[0]: output from C code or DLL file goes to simulation or taken by simulation program

    Those values taken by simulation should be m_A[n+1]? As far as I understand which is an array. However only the first, I mean m_A[1+1] = m_A[2] has taken by simulation. How could I write the code all values from m_A[2], m_A[3]........m_A[20] can be taken by simulation or DLL sends those values to simulation program one by one for every 20 ms or different interval...

    http://cboard.cprogramming.com/showt...=102217&page=2
    Code:
    __declspec(dllexport) void simuser (t, delt, in, out)
    double t, delt;
    double *in, *out;
    Wrong. Or well, old. Does visual studio not cry havoc when you compile that?

  6. #6
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15
    Code:
    __declspec(dllexport) void simuser (double t, double delt, double *in, double *out)
    vart suggested to use this code. Old one and this one are working in vc++ and devc++. Any idea why I can not send all array values to DLL?
    Last edited by gurhan; 05-05-2008 at 07:26 AM.

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. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM