Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-21
6
Medium Priority
?
429 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

876 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