Solved

How do I properly return a wide character string from a function?

Posted on 2006-06-21
6
386 Views
Last Modified: 2009-07-29
I have a function which takes 2 strings in (wchar_t's), manipulates them and then I need to output the result.  How do I properly return the string?  With every way that I've tried I've been returning local variables and end up with corrupted data in most instances.

void addMissionToURL(wchar_t* ourUrl, wchar_t* Mission, wchar_t url) {

      // wchar_t url[2048];
      wchar_t ourDomain[256];
      wchar_t ourPath[1024];
      wchar_t* endPointer;

      GetDomain(ourUrl, ourDomain, sizeof(ourDomain));
      GetPath(ourUrl, ourPath, sizeof(ourPath));
      StringCbCatEx( url, sizeof(url), TEXT("http://"), &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), ourDomain,       &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), ourPath,         &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), Mission,         &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), TEXT(".upd"),    &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);

   return;
}
0
Comment
Question by:GENTP
  • 4
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 16953693
You should pass the output URL in as a buffer, e.g.

void addMissionToURL(wchar_t* ourUrl, wchar_t* Mission, wchar_t* urlOutput, size_t szOutput) {

      wchar_t url[2048];
      wchar_t ourDomain[256];
      wchar_t ourPath[1024];
      wchar_t* endPointer;

      GetDomain(ourUrl, ourDomain, sizeof(ourDomain));
      GetPath(ourUrl, ourPath, sizeof(ourPath));
      StringCbCatEx( url, sizeof(url), TEXT("http://"), &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), ourDomain,       &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), ourPath,         &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), Mission,         &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);
      StringCbCatEx( url, sizeof(url), TEXT(".upd"),    &endPointer, NULL, STRSAFE_FILL_BEHIND_NULL);

      if (wcslen(url) < szOutput) wcscpy(urlOutput,url): // copy to output if there's enough spce

   return;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 16953715
Oh, and that is intended to be used like

wchar_t url[256];

addMissionToURL(L"www.experts-exchange.com","Mission", url, 256);

You of course can also use 'countof()' to automate the sizing, e.g.

#define countof(array) (sizeof(array)/sizeof(array[0]))

wchar_t url[256];

addMissionToURL(L"www.experts-exchange.com","Mission", url, countof(url));
0
 
LVL 86

Expert Comment

by:jkr
ID: 16953772
I just see that you're using 'sizeof()' in the above - not a good idea with 'wchar_t', since you'll get twice the size ('wchar_t' is 2 bytes), so you better use 'countof()' there, too. BTW, why not using STL's strings, e.g

#include <string>
using namespace std;

void addMissionToURL(wstring& ourUrl, wstring& Mission, wstring& urlOutput) {

      wchar_t ourDomain[256];
      wchar_t ourPath[1024];

      GetDomain(ourUrl, ourDomain, sizeof(ourDomain));
      GetPath(ourUrl, ourPath, sizeof(ourPath));
      urlOutput = TEXT("http://");
      urlOutput += ourDomain;
      urlOutput += ourPath;
      urlOutput += Mission;
      urlOutput += TEXT(".upd");

   return;
}

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Author Comment

by:GENTP
ID: 16953791
okay, I'll try count of.  As for why I'm not using stl strings, probably because I don't know about them.  I still haven't quite mastered strings in C++ yet (among other things).
0
 
LVL 5

Author Comment

by:GENTP
ID: 16953806
error C3861: 'countof': identifier not found, even with argument-dependent lookup

Is STL strings windows specific or is it cross platform?
0
 
LVL 86

Expert Comment

by:jkr
ID: 16953849
>>error C3861: 'countof': identifier not found, even with argument-dependent lookup

You'll need to add

#define countof(array) (sizeof(array)/sizeof(array[0]))

>>Is STL strings windows specific or is it cross platform?

It's more than cross platform ;o)

See http://www.sgi.com/tech/stl/

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question