[GH-ISSUE #176] Corrupt output filename on windows #142

Closed
opened 2026-05-05 11:46:31 -06:00 by gitea-mirror · 8 comments
Owner

Originally created by @dirkvdb on GitHub (May 15, 2018).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/176

Originally assigned to: @jmcnamara on GitHub.

When I write a workbook to disk the output filename is corrupt. In my case a file with the name "C" is output.

Cause:
In packager.c:_open_zipfile_win32 the filename is converted to wchar_t and passed as a void pointer to zipOpen2_64.

Eventually in iowion32.c:win32_open64_file_func this void pointer is passed to CreateFile by casting it to LPCSTR, since my program is not compiled with unicode but multibyte character set, this is an invalid cast and results in a corrupt filename.

Originally created by @dirkvdb on GitHub (May 15, 2018). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/176 Originally assigned to: @jmcnamara on GitHub. When I write a workbook to disk the output filename is corrupt. In my case a file with the name "C" is output. Cause: In packager.c:_open_zipfile_win32 the filename is converted to wchar_t and passed as a void pointer to zipOpen2_64. Eventually in iowion32.c:win32_open64_file_func this void pointer is passed to CreateFile by casting it to LPCSTR, since my program is not compiled with unicode but multibyte character set, this is an invalid cast and results in a corrupt filename.
gitea-mirror 2026-05-05 11:46:31 -06:00
Author
Owner

@jmcnamara commented on GitHub (May 15, 2018):

Can you show the steps to reproduce this.

<!-- gh-comment-id:389182670 --> @jmcnamara commented on GitHub (May 15, 2018): Can you show the steps to reproduce this.
Author
Owner

@dirkvdb commented on GitHub (May 15, 2018):

Compile this program

#include <xlsxwriter.h>
int main() {
    lxw_workbook  *workbook  = workbook_new("C:\\Temp\\Test file.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    return workbook_close(workbook);
}

The output file will not get created.
Linked against libxlsxwriter lib compiled as described in #175

<!-- gh-comment-id:389206993 --> @dirkvdb commented on GitHub (May 15, 2018): Compile this program ``` #include <xlsxwriter.h> int main() { lxw_workbook *workbook = workbook_new("C:\\Temp\\Test file.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); return workbook_close(workbook); } ``` The output file will not get created. Linked against libxlsxwriter lib compiled as described in #175
Author
Owner

@jmcnamara commented on GitHub (May 15, 2018):

since my program is not compiled with unicode but multibyte character set, this is an invalid cast and results in a corrupt filename.

I don't think your app needs to be compiled with Unicode but the library does. Try the CMake instructions below.

<!-- gh-comment-id:389257884 --> @jmcnamara commented on GitHub (May 15, 2018): > since my program is not compiled with unicode but multibyte character set, this is an invalid cast and results in a corrupt filename. I don't think your app needs to be compiled with Unicode but the library does. Try the CMake instructions below.
Author
Owner

@jmcnamara commented on GitHub (May 15, 2018):

Open a Windows CMD or Command Window and set up your MSVC environment, if required.

Then create a work directory and and an install directory that the include and library files will be installed to.

set    WORK_DIR=C:/Users/Molly/tmp
set INSTALL_DIR=C:/Users/Molly/tmp/install_dir

Build the Zlib library:

cd %WORK_DIR%

git clone https://github.com/madler/zlib.git
cd zlib
mkdir build
cd    build

cmake .. -G "Visual Studio 14 Win64" -DCMAKE_INSTALL_PREFIX:PATH="%INSTALL_DIR%/zlib"

cmake --build . --config Release --target install  

Build the xlsxwriter library :

cd %WORK_DIR%

git clone https://github.com/jmcnamara/libxlsxwriter.git
cd libxlsxwriter
mkdir build
cd    build

cmake .. -G "Visual Studio 14 Win64" -DCMAKE_INSTALL_PREFIX:PATH="%INSTALL_DIR%/libxlsxwriter" -DZLIB_ROOT:STRING="%INSTALL_DIR%/zlib"

cmake --build . --config Release --target install  

Create a new Win32 Console (or other C/C++) application in Visual Studio:

File
  -> New 
    -> Project

Visual C++ 
  -> Win32 
    -> Win32 Console Application

Change the ARCH to "x64" (or to match the parameter to cmake).

Replace the empty main with a libxlsxwriter example, make sure to maintain the "stdafx.h" include:

#include "stdafx.h"


#include "xlsxwriter.h"

int main() {

    lxw_workbook  *workbook = workbook_new("hello_world.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
    worksheet_write_number(worksheet, 1, 0, 123, NULL);

    workbook_close(workbook);

    return 0;
}

Edit the application properties:

Project 
  -> ConsoleApplication Properties

Set the libxlsxwriter include path to match the path used above:

Configuration Properties 
  -> C/C++ 
    -> General 
      -> Additional Include Directories

Set it to the following (or similar path used above):
      
C:\Users\Molly\tmp\install_dir\libxlsxwriter\include

Set the linker directories to match the path created above:

Configuration Properties       
  -> Linker
    -> General
      -> Additional Library Directories

Add the following (or similar paths used above):
      
C:\Users\Molly\tmp\install_dir\libxlsxwriter\lib\x64\Release
C:\Users\Molly\tmp\install_dir\zlib\lib

Set the linker additional libraries to match the zlib and xlsxwriter libs created above:

Configuration Properties       
  -> Linker
     -> Input
        -> Additional Dependencies

Add the following:
        
xlsxwriter.lib
zlib.lib

Build the solution and run the output executable. It should create a hello_world.xlsx file in the same directory you ran it from.

<!-- gh-comment-id:389260861 --> @jmcnamara commented on GitHub (May 15, 2018): Open a Windows CMD or Command Window and set up your MSVC environment, if required. Then create a work directory and and an install directory that the include and library files will be installed to. ``` set WORK_DIR=C:/Users/Molly/tmp set INSTALL_DIR=C:/Users/Molly/tmp/install_dir ``` Build the Zlib library: ``` cd %WORK_DIR% git clone https://github.com/madler/zlib.git cd zlib mkdir build cd build cmake .. -G "Visual Studio 14 Win64" -DCMAKE_INSTALL_PREFIX:PATH="%INSTALL_DIR%/zlib" cmake --build . --config Release --target install ``` Build the xlsxwriter library : ``` cd %WORK_DIR% git clone https://github.com/jmcnamara/libxlsxwriter.git cd libxlsxwriter mkdir build cd build cmake .. -G "Visual Studio 14 Win64" -DCMAKE_INSTALL_PREFIX:PATH="%INSTALL_DIR%/libxlsxwriter" -DZLIB_ROOT:STRING="%INSTALL_DIR%/zlib" cmake --build . --config Release --target install ``` Create a new Win32 Console (or other C/C++) application in Visual Studio: ``` File -> New -> Project Visual C++ -> Win32 -> Win32 Console Application ``` Change the ARCH to "x64" (or to match the parameter to cmake). Replace the empty main with a libxlsxwriter example, make sure to maintain the "stdafx.h" include: ```C #include "stdafx.h" #include "xlsxwriter.h" int main() { lxw_workbook *workbook = workbook_new("hello_world.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); worksheet_write_string(worksheet, 0, 0, "Hello", NULL); worksheet_write_number(worksheet, 1, 0, 123, NULL); workbook_close(workbook); return 0; } ``` Edit the application properties: ``` Project -> ConsoleApplication Properties ``` Set the libxlsxwriter include path to match the path used above: ``` Configuration Properties -> C/C++ -> General -> Additional Include Directories Set it to the following (or similar path used above): C:\Users\Molly\tmp\install_dir\libxlsxwriter\include ``` Set the linker directories to match the path created above: ``` Configuration Properties -> Linker -> General -> Additional Library Directories Add the following (or similar paths used above): C:\Users\Molly\tmp\install_dir\libxlsxwriter\lib\x64\Release C:\Users\Molly\tmp\install_dir\zlib\lib ``` Set the linker additional libraries to match the zlib and xlsxwriter libs created above: ``` Configuration Properties -> Linker -> Input -> Additional Dependencies Add the following: xlsxwriter.lib zlib.lib ``` Build the solution and run the output executable. It should create a hello_world.xlsx file in the same directory you ran it from.
Author
Owner

@dirkvdb commented on GitHub (May 15, 2018):

I performed very similar steps, as soon as I put a space in the filename the file was not created anymore. I'm pretty sure projects generated with the "Visual Studio 14 Win64" generator default to multi byte instead of unicode.

<!-- gh-comment-id:389266761 --> @dirkvdb commented on GitHub (May 15, 2018): I performed very similar steps, as soon as I put a space in the filename the file was not created anymore. I'm pretty sure projects generated with the "Visual Studio 14 Win64" generator default to multi byte instead of unicode.
Author
Owner

@jmcnamara commented on GitHub (May 15, 2018):

'm pretty sure projects generated with the "Visual Studio 14 Win64" generator default to multi byte instead of unicode.

It does. I thought that the CmakeList had been modified to ensure Unicode but I didn't merge that change in the end.

as soon as I put a space in the filename the file was not created anymore.

That isn't the same issue as a the file name truncation issue you reported. The above Cmake recipe and code will generated a file called "hello world.xlsx" without issue. I just tested it.

So can you try the above, see if it works, and then we can figure out what is the difference with your setup.

<!-- gh-comment-id:389294255 --> @jmcnamara commented on GitHub (May 15, 2018): > 'm pretty sure projects generated with the "Visual Studio 14 Win64" generator default to multi byte instead of unicode. It does. I thought that the CmakeList had been modified to ensure Unicode but I didn't merge [that change](https://github.com/jmcnamara/libxlsxwriter/pull/154/files) in the end. > as soon as I put a space in the filename the file was not created anymore. That isn't the same issue as a the file name truncation issue you reported. The above Cmake recipe and code will generated a file called "hello world.xlsx" without issue. I just tested it. So can you try the above, see if it works, and then we can figure out what is the difference with your setup.
Author
Owner

@dirkvdb commented on GitHub (May 16, 2018):

In my test I was still using release 0.7.6.
It appears to be fixed in master by this commit:
3b25ad611b (diff-ea69bda3932b6e3711b5bee1a83d7ed5)

<!-- gh-comment-id:389418729 --> @dirkvdb commented on GitHub (May 16, 2018): In my test I was still using release 0.7.6. It appears to be fixed in master by this commit: https://github.com/jmcnamara/libxlsxwriter/commit/3b25ad611b5e7ef6e367debb90f66b4b78c86380#diff-ea69bda3932b6e3711b5bee1a83d7ed5
Author
Owner

@jmcnamara commented on GitHub (May 16, 2018):

In my test I was still using release 0.7.6.
It appears to be fixed in master by this commit:

This is why I asked for the steps to reproduce the issue and why I didn't mention this fix when you indicated that you were using a git clone.

Nevertheless, this fix should be packaged up into a release. I've haven't had a chance to do that recently but I'll try get to it on the weekend.

Thanks for following up. Closing this issue.

<!-- gh-comment-id:389433385 --> @jmcnamara commented on GitHub (May 16, 2018): > In my test I was still using release 0.7.6. > It appears to be fixed in master by this commit: This is why I asked for the steps to reproduce the issue and why I didn't mention this fix when you indicated that you were using a git clone. Nevertheless, this fix should be packaged up into a release. I've haven't had a chance to do that recently but I'll try get to it on the weekend. Thanks for following up. Closing this issue.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#142
No description provided.