Creating excel .xll addin
Hi all,
I am creating an addin for excel using visual studio and c++.
i have a calling sub in vba
Public Declare Function calcprob Lib "PA.xll" (ByVal a As Integer, ByVal b As Integer, ByVal c As Integer, ByVal d As Integer, ByVal e As Integer, ByVal f As Integer, ByVal g As Integer, ByVal hsr As Double, ByVal asr As Double, ByVal period As Integer, reactions As Variant, bsr As Variant, bgr As Variant) As Variant
Sub test()
row = calcprob(hscore, ascore, time, 0, periods_min, 2, 2, hsr, asr, return_period, reactions, base_scoring_rates, base_goal_rates)
end sub
the xcalcprob method is called successfully and it runs as its meant to.
the variable 'row' gets filled as expected but when control leaves this sub i get the following error
'Windows has triggered a breakpoint in EXCEL.EXE'
'This may be due to a corruption of the heap, which indicates a bug in EXCEL.exe or any of the dlls it has loaded'
Does anybody know how i can go about finding the solution to this?
thnaks in advance
Re: Creating excel .xll addin
I think I'm missing something because where is the part where you use C++ code ?
Re: Creating excel .xll addin
oh sorry - 'row = calcprob(hscore, ascore, time, 0, periods_min, 2, 2, hsr, asr, return_period, reactions, base_scoring_rates, base_goal_rates)'
is the call with vba to the c++ method
the 'Public Declare Function' tell vba that there is an external lib with these parameters
Re: Creating excel .xll addin
this is the actual code
VARIANT __stdcall calcprob(int home_score,int away_score,int current_time_min,int current_time_sec,int periods_per_min,int ht_it,int ft_it, double home_scoring_rate,double away_scoring_rate,int period_to_return, VARIANT *reaction_rates,VARIANT *base_scoring_rates,VARIANT *base_goal_rates){
VARIANT var;
cpp_xloper reactions(reaction_rates);
cpp_xloper base_sr(base_scoring_rates);
cpp_xloper base_gr(base_goal_rates);
//cpp_xloper hsr(home_scoring_rates);
//cpp_xloper asr(away_scoring_rates);
// Convert the xlopers to xl_arrays of doubles
xl_array * xReactions = reactions.AsDblArray();
xl_array * xBase_scoring_rates = base_sr.AsDblArray();
xl_array * xBase_goal_rates = base_gr.AsDblArray();
//xl_array * xHome_scoring_rates = hsr.AsDblArray();
//xl_array * xAway_scoring_rates = asr.AsDblArray();
//result var
double *pArray=NULL;
typedef double (*prob)[80][3321];
prob prob_store =(prob)new double[80][3321];
//double (*prob_store)[80][3321];
//double **(*prob_store)[80][3321];
setuparrays(xReactions,home_scoring_rate,away_scoring_rate,xBase_scoring_rates,xBase_goal_rates);
////set all preceding positions to 0
for(int period = 0; period <= current_time_min; period++){
for(int index = 0;index < total_scores;index++){
(*prob_store)[period][index] = 0;
}
}
//set current position to 1
(*prob_store)[current_time_min][scores_col[home_score][away_score]] = 1;
//calc tree
for(int period = current_time_min + 1; period <= period_to_return; period++){
for(int index = scores_col[home_score][away_score];index <= total_scores;index++){
//BIG LOOP LOGIC
}
}
VariantInit(&var);
SAFEARRAY *psa = SafeArrayCreateVector(VT_R8, 0, 3321);
SafeArrayAccessData(psa, (void **)&pArray);
for(int index = 0; index <= total_scores;index++)
{
pArray[index] = (*prob_store)[period_to_return][index];
}
SafeArrayUnaccessData(psa);
var.vt = VT_ARRAY|VT_R8;
var.parray = psa;
return var;
}
Re: Creating excel .xll addin
Another quick question - what is the correct way to destroy this object?
typedef double (*prob)[80][2926];
prob prob_store =(prob)new double[80][2926];