LabWindows/CVI

annulla
Visualizzazione dei risultati per 
Cerca invece 
Intendevi dire: 

General Protection Fault using ExcelRpt_WriteData with string data

I get a general protection fault when I try to write multiple strings to a range of cells.

What am I doing wrong?  This is what my statements look like:

 

int SaveDataToExcel(void)
{
char strModuleLabels[6][3];

 //Dummy labels for now...
 strcpy(strModuleLabels[0] , "1A");  //These labels will come from
 strcpy(strModuleLabels[1] , "2A");  //different source when
 strcpy(strModuleLabels[2] , "3A");  //the Excel code is working
 strcpy(strModuleLabels[3] , "1B"); 
 strcpy(strModuleLabels[4] , "2B"); 
 strcpy(strModuleLabels[5] , "3B");
 

 //Open the app, workbook and worksheet
 xclChk(ExcelRpt_ApplicationNew(1, &applicationHandle));
 xclChk(ExcelRpt_WorkbookOpen (applicationHandle , strFnameOut, &workbookHandle));
 xclChk(ExcelRpt_GetWorksheetFromName (workbookHandle, "State Data", &wsHnd_StateData ));

 //Write the labels
 //The single value cell version works
 xclChk(ExcelRpt_SetCellValue (wsHnd_StateData, "N2", CAVT_CSTRING, strModuleLabels[0]));
 
 //The following line gives general protection fault - why?
 xclChk(ExcelRpt_WriteData( wsHnd_StateData, "N3:N8", CAVT_CSTRING, 6, 1, &strModuleLabels[0]));

 xclChk(ExcelRpt_WorkbookSave (workbookHandle, NULL, 0));
 xclChk(ExcelRpt_ApplicationQuit (applicationHandle));

 

ExcelErr:   
 if (HResult)
 {
  CA_GetAutomationErrorString(HResult, strDir, sizeof(strDir));
  MessagePopup("Excel Error" , strDir);
 }
 return iExcelResult;
} //End Function: SaveDataToExcel

0 Kudos
Messaggio 1 di 6
4.971Visualizzazioni

Hi jkin00,

 

Have you looked at any onf the examples that we ship with CVI? One of them show you how to do exactly what you want to do. It is called excalreportdemo.cws. Take a look at it.

 

Regards,

 

Perry S.

Applications Engineer
National Instruments
0 Kudos
Messaggio 2 di 6
4.965Visualizzazioni

Perry,

Thanks for your relpy.  I did actually start with that example.  Unfortunately it uses  ExcelRpt_WriteDataFromTableControl instead.  I don't have a table to write from - just an array of strings.

0 Kudos
Messaggio 3 di 6
4.960Visualizzazioni

Hi jkin00,

 

I have been working on recreating your issue and then finding a solution for it. I was successful on both accounts. I found that the data type input for the ExcelRpt_WriteData function was expecting a char *[ ][ ] while I was only giving it a char [ ][ ]. Also the dimentions of your variable need to match those used in the function. Here is some example code that works.

 

char *myStrings[3][1];
myStrings[0][0] = "Howdy";
myStrings[1][0] = "Sup";
myStrings[2][0] = "Dude";
			
ExcelRpt_ApplicationNew (VTRUE, &applicationHandle);
			
ExcelRpt_WorkbookNew (applicationHandle, &workbookHandle);
			
ExcelRpt_WorksheetNew (workbookHandle, 1, &worksheetHandle);
			
ExcelRpt_WriteData (worksheetHandle, "B1:B3", ExRConst_dataString, 3, 1, myStrings);

 

I hope that this helps.

 

Regards,

 

Perry S.

Applications Engineer
National Instruments
Messaggio 4 di 6
4.947Visualizzazioni

Thanks a lot, that fixed my problem!

0 Kudos
Messaggio 5 di 6
4.934Visualizzazioni

Just in case anyone else makes the same mistake I did; Perry S's solution is not complete. The line:

char *myStrings[3][1];

Only declares a one dimensional array of pointers to strings. You need to allocate the mempry for each string in the array yourself.

At least in CVI13.

0 Kudos
Messaggio 6 di 6
896Visualizzazioni