brechtsanders / xlsxio

XLSX I/O - C library for reading and writing .xlsx files
MIT License
417 stars 112 forks source link

can not use both read/write dll in one exe? #39

Open fengcai opened 5 years ago

fengcai commented 5 years ago

I wirte one test.exe agains libxlsxio dll, it's working well respectively on libxlsxio_read.dll and libxlsxio_write.dll but if I combine write and read in one application, error will report

environment windows 7 vc2012 lib: libxlsxio_read.dll.a/libxlsxio_write.dll.a

sample code:

#include <xlsxio_read.h>
#include <xlsxio_write.h>
#include <stdio.h>
#include <string>
#include <Windows.h>
using namespace std;

int read_xlsx() {
  //open .xlsx file for reading
xlsxioreader xlsxioread;
if ((xlsxioread = xlsxioread_open("d:\\0801_test.xlsx")) == NULL) {
  fprintf(stderr, "Error opening .xlsx file\n");
  return 1;
}

//read values from first sheet
char* value;
xlsxioreadersheet sheet;
const char* sheetname = NULL;
printf("Contents of first sheet:\n");
if ((sheet = xlsxioread_sheet_open(xlsxioread, sheetname, XLSXIOREAD_SKIP_EMPTY_ROWS)) != NULL) {
  //read all rows
  while (xlsxioread_sheet_next_row(sheet)) {
    //read all columns
    while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
      printf("%s\t", value);
      //free(value);
    }
    printf("\n");
  }
  xlsxioread_sheet_close(sheet);
}

//clean up
xlsxioread_close(xlsxioread);

return 0;
}

int write_xlsx() {
//open .xlsx file for writing (will overwrite if it already exists)
xlsxiowriter handle;
if ((handle = xlsxiowrite_open("d:\\test_xlsx.xlsx", "MySheet")) == NULL) {
  fprintf(stderr, "Error creating .xlsx file\n");
  return 1;
}

//write column names
xlsxiowrite_add_column(handle, "Col1", 16);
xlsxiowrite_add_column(handle, "Col2", 0);
xlsxiowrite_next_row(handle);

//write data
int i;
for (i = 0; i < 1000; i++) {
  xlsxiowrite_add_cell_string(handle, "test");
  xlsxiowrite_add_cell_int(handle, i);
  xlsxiowrite_next_row(handle);
}

//close .xlsx file
xlsxiowrite_close(handle);

return 0;
}

int main() {
  read_xlsx();
  write_xlsx();

return 0;
}
brechtsanders commented 5 years ago

What's the error? Any chance you can debug?

brechtsanders commented 5 years ago

Just tried your code with MinGW on Windows and it works perfectly. No crashes or errors.

fengcai commented 5 years ago

VC project zip file xlsxio.zip

project settings: xlsx_config error prompt xlsx_error_write

error information: The procedure entry point xlsxiowrite_add_cell_int could not be located in the dynamic library libxlsxio_read.dll

if the source code only contains read or wirte function, it's working well.

another issue for the test is I have to comment out the free(vlaue) as follows or it will crash

    while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
      printf("%s\t", value);
      //free(value);
    }
fengcai commented 5 years ago

hi, I find it is same as #38 and I solved it by lib created manually.

but the free-cause crash is still there in read function

    while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
      printf("%s\t", value);
      //free(value); // heap issue
    }

callstack is

>   msvcr110d.dll!_CrtIsValidHeapPointer(const void * pUserData) Line 2036  C++
    msvcr110d.dll!_free_dbg_nolock(void * pUserData, int nBlockUse) Line 1322   C++
    msvcr110d.dll!_free_dbg(void * pUserData, int nBlockUse) Line 1265  C++
    msvcr110d.dll!free(void * pUserData) Line 49    C++
    xlsx.exe!read_xlsx() Line 59    C++
brechtsanders commented 5 years ago

I just changed CMakeLists.txt so it also generates .def files when building with MinGW. In the next release the .def files will be included, allowing you to create proper .lib files for MSVC. What version of XLSX I/O did you use when you got the free-cause crash?

fengcai commented 5 years ago

latest version: xlsxio-0.2.19-binary-win32.zip from here

brechtsanders commented 5 years ago

The code you posted earlier doesn't have read_xlsx() on line 59. Can you attach the actual file that you were debugging?

brechtsanders commented 5 years ago

Also, can you try with version 0.2.21, which was just released? The binary packages for Windows now also include .def files and README.md explains how to make .lib files from them for use with MSVC.

fengcai commented 5 years ago

vc 2012 project: xlsxio-0.2.21-win32.zip I have tested the latest 0.2.21, crash as picture attached VC2012 Win7

crash
brechtsanders commented 4 years ago

The free() seems to be the same as in #73 Can you you try again with version 0.2.29 using xlsxioread_free() instead of free()? Thanks for letting me know if the issue is resolved now.