Solved

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

Posted on 2006-06-21
6
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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