return string

  • Thread starter Thread starter AA2e72E
  • Start date Start date
A

AA2e72E

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.
 
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
 
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.
 
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
 
I figured out the signature: I need to use the [out] attribute for the buf
and buflen arguments.

AA2e72E said:
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 said:
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.
 
Dave, thanks for the additional guidance - much appreciated.

David Lowndes said:
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
.
 
AA2e72E said:
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).
 
Back
Top