Solved

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

Posted on 2006-06-21
6
372 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now