Click here to Skip to main content
15,918,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
<pre>// Concatenate two wchar_t*
void mergeWChar(wchar_t*& dest, const wchar_t* source) {
	if (dest == nullptr) {
		dest = const_cast<wchar_t*>(source);
		return;
	}

	wchar_t *newdest = (wchar_t*)malloc((wcslen(dest) + wcslen(source) + 1) * sizeof *newdest); //thay + 1 bằng + 0
	wcscpy(newdest, dest);      // the *_s functions are useless here
	wcscat(newdest, source);    // as you know that newdest point to
	// a long enough buffer
	dest = newdest;

}



And then I create a Function and reg it with UB
<pre lang="c++">
<pre>__declspec(dllexport) LPXLOPER12 sorachu(double so)
{
	static XLOPER12  xResult;
	xResult.xltype = xltypeStr;

		
	wchar_t* msg = nullptr;
	wchar_t* wcs = L"Sơn";
	wchar_t* wcs1 = L"Văn";
		
	mergeWChar(msg, wcs);	
	mergeWChar(msg, wcs1);
	
	xResult.val.str = msg;

	return (LPXLOPER12)&xResult;
}



Open Excel
Type "=sorachu(123)"
Result: ơnVăn...........................

What I have tried:

If I change <pre>wchar_t* wcs = L"Sơn";
is
wchar_t* wcs = L"\006Sơn";
then results = Sơn Văn" >>> TRUE

How to change L "\ 006" automatically when the variables wcs or wcs1 change.
Help me, pls.
Posted
Updated 5-Sep-19 18:20pm
v2

I tried this code in VS2017 and it worked correctly. I made only slight changes.
C++
void mergeWChar( wchar_t*& dest, const wchar_t* source )
{
    if( dest == nullptr )
    {
        dest = const_cast<wchar_t*>( source );
        return;
    }

    size_t count = wcslen( dest ) + wcslen( source ) + 1;
    wchar_t * newdest = (wchar_t*)malloc( count * sizeof( wchar_t ) );
    wcscpy( newdest, dest );
    wcscat( newdest, source );

    // a long enough buffer
    dest = newdest;
}

void DoMergeCharTest()
{
    wchar_t* msg = nullptr;
    wchar_t* wcs1 = _T( "Son" );
    wchar_t* wcs2 = _T( "Van" );

    trace( _T( "MergeCharTest : '%s' and '%s'\n" ), wcs1, wcs2 );

    mergeWChar( msg, wcs1 );
    mergeWChar( msg, wcs2 );

    trace( _T( "result is '%s'\n" ), msg );

    free( msg );
}
The output was :
MergeCharTest : 'Son' and 'Van'
result is 'SonVan'
 
Share this answer
 
v2
I think that mergeWChar is TRUE and I try it in VS 2013, it is OK (with console).

but It is error when run it in XLL Excel.

PS: "Sơn" not "Son"
(unicode char)
 
Share this answer
 
v2
My problem is in __declspec(dllexport) LPXLOPER12 sorachu(double so) for XLL Excel.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900