Solved

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

Posted on 2006-06-21
6
367 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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…
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.

747 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

10 Experts available now in Live!

Get 1:1 Help Now