[GH-ISSUE #451] Chinese in workbook_add_worksheet cause crash #353

Closed
opened 2026-05-05 12:11:35 -06:00 by gitea-mirror · 0 comments
Owner

Originally created by @xuxiaochun025 on GitHub (Aug 9, 2024).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/451

libxlsxwriter: Reporting Problems

std::string G2U(const std::string& gbk)
{
	int nwLen = ::MultiByteToWideChar(CP_ACP, 0, gbk.c_str(), -1, NULL, 0);
	wchar_t* pwBuf = new wchar_t[nwLen + 1];
	ZeroMemory(pwBuf, nwLen * 2 + 2);
	::MultiByteToWideChar(CP_ACP, 0, gbk.c_str(), static_cast<int>(gbk.length()), pwBuf, nwLen);
	int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
	char* pBuf = new char[nLen + 1];
	ZeroMemory(pBuf, nLen + 1);
	::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
	std::string retStr(pBuf);
	delete[]pwBuf;
	delete[]pBuf;
	pwBuf = NULL;
	pBuf = NULL;
	return retStr;
}

std::string fileName = "报警信息.xlsx";
const char* file_path = "test.xlsx";
lxw_workbook* workbook = workbook_new(file_path);
lxw_worksheet* worksheet = workbook_add_worksheet(workbook, G2U(fileName).data());

However, the program encounters a crash at last line consistently. Interestingly, the same program runs without any issues on other machines. What could be the reason for this discrepancy?

Originally created by @xuxiaochun025 on GitHub (Aug 9, 2024). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/451 # libxlsxwriter: Reporting Problems ``` std::string G2U(const std::string& gbk) { int nwLen = ::MultiByteToWideChar(CP_ACP, 0, gbk.c_str(), -1, NULL, 0); wchar_t* pwBuf = new wchar_t[nwLen + 1]; ZeroMemory(pwBuf, nwLen * 2 + 2); ::MultiByteToWideChar(CP_ACP, 0, gbk.c_str(), static_cast<int>(gbk.length()), pwBuf, nwLen); int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char* pBuf = new char[nLen + 1]; ZeroMemory(pBuf, nLen + 1); ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr(pBuf); delete[]pwBuf; delete[]pBuf; pwBuf = NULL; pBuf = NULL; return retStr; } std::string fileName = "报警信息.xlsx"; const char* file_path = "test.xlsx"; lxw_workbook* workbook = workbook_new(file_path); lxw_worksheet* worksheet = workbook_add_worksheet(workbook, G2U(fileName).data()); ``` However, the program encounters a crash at last line consistently. Interestingly, the same program runs without any issues on other machines. What could be the reason for this discrepancy?
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#353
No description provided.