1

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

3
  • You've mentioned that string should be in UTF-8. Have you tried console.log(returnedValue.toString('utf8'));? Commented Dec 18, 2019 at 22:28
  • Did you tried to print that Buffer? I mean value, not a type.. Commented Dec 19, 2019 at 1:48
  • @igor.codes neither of these worked. Commented Dec 19, 2019 at 14:26

1 Answer 1

1

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:

  1. Passing in an argument from the client side node.js into the server side C++
  2. Changing the value on the server side
  3. Returning that same argument.
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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.