Reputation: 813
I have server-side code written in C++ that returns a string. The client-side code is hosted in express. The client side code has calls endpoint as follows:
router.post( '/returnMyStringFromCPlusPlus', ( request, response ) => {
var returnedValue;
try {
var returnBuf = new Buffer.alloc( 512, 0 ); // this will fill buffer with 0
returnedValue= serverSideCode.returnMyStringFromCPlusPlus();
...
}
When I get this returnedValue back, I see it is a buffer. I double-checked by testing:
console.log(returnedValue instanceof buffer);
Which obviously returned true. The problem is I can't get that buffer into text! I've tried:
String Decoder: const StringDecoder = require( "string_decoder" ).StringDecoder;
const decoder = new StringDecoder( "utf-8" )
console.log( decoder.write( returnedValue) );
toString():
console.log(returnedValue.toString('ascii'));
String.fromCharCode
var printMe = String.fromCharCode.apply(null, new Uint16Array(returnCode));
console.log(printMe)
Honestly I never really have had much luck asking questions on StackOverFlow and usually end up answering my own questions, but I'd be curious if any of you folks have seen this before. I have no clue how to solve this and have been doing it for over a day now.
Questions I anticipate
Using node version 11 (tried 12 and 13 as well) Using x64 version of Visual Studio It shouldn't matter, but the code returned from C++ is literally:
#include <string>
string testThis()
{
string testIfThisWorks = "TestIfThisWorks";
return testIfThisWorks;
}
This is on Windows 10
Upvotes: 1
Views: 293
Reputation: 813
So I know this is almost 7 months later, but I did eventually get back to finding a solution to this problem. I don't think it's ideal and am still open to better suggestions if I find one, but I found kind of a hacky way to do it. What I ended up doing was:
router.post( '/returnMyStringFromCPlusPlus', ( request, response ) => { var returnedValue; try { var returnBuf = new Buffer.alloc( 512, 0 ); // this will fill buffer with 0 var valueToChange = "valueToChange"; returnedValue= serverSideCode.returnMyStringFromCPlusPlus(valueToChange); ... }
I would then manipulate this value on the C side and return it as a string. <3
Upvotes: 1