HighTechTalks DotNet Forums  

return string

VC++.net microsoft.public.dotnet.languages.vc


Discuss return string in the VC++.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AA2e72E
 
Posts: n/a

Default return string - 02-15-2010 , 05:58 AM






If I have a function such as this (in a Win32 project):

_declspec (dllexport) char* _stdcall view()
{
char* output;
// HOW DO I assign "THIS IS AN ARBITRARY STRING" to output
return output;
}

The objective is that a native (unmanaged code) client [that is, I cannot
use _clrcall] calling the function view in the Win32 DLL receives the string
"THIS IS AN ARBITRARY STRING"

Thanks for your help.

Reply With Quote
  #2  
Old   
David Lowndes
 
Posts: n/a

Default Re: return string - 02-15-2010 , 06:17 AM






Quote:
If I have a function such as this (in a Win32 project):

_declspec (dllexport) char* _stdcall view()
{
char* output;
// HOW DO I assign "THIS IS AN ARBITRARY STRING" to output
return output;
}

The objective is that a native (unmanaged code) client [that is, I cannot
use _clrcall] calling the function view in the Win32 DLL receives the string
"THIS IS AN ARBITRARY STRING"
The cleanest solution is to not do it that way. Instead do what the
Win32 APIs do - have the function accept a buffer pointer and a
parameter that specifies the length of the buffer, you then just copy
as much as will fit into the buffer area.

If you have to do it as you want then you either have to have both
side agree on a common memory management API or have the interface
abstract the memory manager away so that only your component allocates
and frees the memory.

Dave

Reply With Quote
  #3  
Old   
AA2e72E
 
Posts: n/a

Default Re: return string - 02-15-2010 , 01:34 PM



Dave, Thank you very much. I like your 'cleanest' solution and understand
that Win32 APIs (eg GetCurrentDirectory) work that way,

I need a little more help with the construction of the APIs signature. If
the API is GetNewestFile, which takes one argument, a path and now will take
a buffer and another argument that is the length of the bufffer.

What would be the fignature of GetNewestFile? I can think of this:

_declspec (dllexport) char* _stdcall GetNewestFile(char *path,char* buf,int
buflen)

Is this correct? [I have no experience of writing Win32 APIs].


Thanks for your help.


"David Lowndes" wrote:

Quote:
The cleanest solution is to not do it that way. Instead do what the
Win32 APIs do - have the function accept a buffer pointer and a
parameter that specifies the length of the buffer, you then just copy
as much as will fit into the buffer area.

Reply With Quote
  #4  
Old   
David Lowndes
 
Posts: n/a

Default Re: return string - 02-15-2010 , 05:32 PM



Quote:
Dave, Thank you very much. I like your 'cleanest' solution and understand
that Win32 APIs (eg GetCurrentDirectory) work that way,

I need a little more help with the construction of the APIs signature. If
the API is GetNewestFile, which takes one argument, a path and now will take
a buffer and another argument that is the length of the bufffer.

What would be the fignature of GetNewestFile? I can think of this:

_declspec (dllexport) char* _stdcall GetNewestFile(char *path,char* buf,int
buflen)
I'd do it like this (skipping the export & calling convention stuff
for clarity):

BOOL GetNewestFile( LPWSTR Path, UINT PathBufferLength );

It'd return TRUE (non-zero) if it succeeds, FALSE if something's
amiss.

Note that it'd be wide-character (Unicode) rather than ANSI - since
all supported Windows OS's are now natively Unicode - so ANSI doesn't
make much sense these days for file/path names.

A typical use would be like this:

WCHAR szPath[_MAX_PATH];

if ( GetNewestFile( szPath, _countof( szPath ) ) )
{
// szPath now contains whatever it should do...

Dave

Reply With Quote
  #5  
Old   
AA2e72E
 
Posts: n/a

Default Re: return string - 02-15-2010 , 05:53 PM



I figured out the signature: I need to use the [out] attribute for the buf
and buflen arguments.

"AA2e72E" wrote:

Quote:
Dave, Thank you very much. I like your 'cleanest' solution and understand
that Win32 APIs (eg GetCurrentDirectory) work that way,

I need a little more help with the construction of the APIs signature. If
the API is GetNewestFile, which takes one argument, a path and now will take
a buffer and another argument that is the length of the bufffer.

What would be the fignature of GetNewestFile? I can think of this:

_declspec (dllexport) char* _stdcall GetNewestFile(char *path,char* buf,int
buflen)

Is this correct? [I have no experience of writing Win32 APIs].


Thanks for your help.


"David Lowndes" wrote:

The cleanest solution is to not do it that way. Instead do what the
Win32 APIs do - have the function accept a buffer pointer and a
parameter that specifies the length of the buffer, you then just copy
as much as will fit into the buffer area.

Reply With Quote
  #6  
Old   
AA2e72E
 
Posts: n/a

Default Re: return string - 02-16-2010 , 02:05 AM



Dave, thanks for the additional guidance - much appreciated.

"David Lowndes" wrote:

Quote:
Dave, Thank you very much. I like your 'cleanest' solution and understand
that Win32 APIs (eg GetCurrentDirectory) work that way,

I need a little more help with the construction of the APIs signature. If
the API is GetNewestFile, which takes one argument, a path and now will take
a buffer and another argument that is the length of the bufffer.

What would be the fignature of GetNewestFile? I can think of this:

_declspec (dllexport) char* _stdcall GetNewestFile(char *path,char* buf,int
buflen)

I'd do it like this (skipping the export & calling convention stuff
for clarity):

BOOL GetNewestFile( LPWSTR Path, UINT PathBufferLength );

It'd return TRUE (non-zero) if it succeeds, FALSE if something's
amiss.

Note that it'd be wide-character (Unicode) rather than ANSI - since
all supported Windows OS's are now natively Unicode - so ANSI doesn't
make much sense these days for file/path names.

A typical use would be like this:

WCHAR szPath[_MAX_PATH];

if ( GetNewestFile( szPath, _countof( szPath ) ) )
{
// szPath now contains whatever it should do...

Dave
.

Reply With Quote
  #7  
Old   
David Wilkinson
 
Posts: n/a

Default Re: return string - 02-16-2010 , 06:03 AM



AA2e72E wrote:
Quote:
I figured out the signature: I need to use the [out] attribute for the buf
and buflen arguments.
AFAIK, [out] is not a part of standard C++, though I think it is a Microsoft
extension. In any case, buflen is not an out parameter.

The [out] concept is not needed in C++, because the notions of pointer,
reference and const can indicate the intent of the parameter.

For example, if you declare a function as

bool GetNewestFile(char* path, int buflen);

then it is *assumed* that the path buffer will be modified by the function;
otherwise it would have been passed as const char* (and the buflen would not
have been needed).

--
David Wilkinson
Visual C++ MVP

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.