Managed and unmanaged strings 
Author Message
 Managed and unmanaged strings

In his article in the April 2002 Windows Developer, David Union discusses
passing a string from C# to the Win32 MessageBox function (not the .NET
MessageBox function).  He remarks:

  The sample also indicates that the C# String class is sufficient to
  handle LPCTSTR.  Technically, we could define an explicit unmanaged
  string and pass that into the Win32 API function, but that should seldom
  be necessary.

Can somebody elaborate on (1) the difference between a managed string and
an unmanaged string (beyond the fact that the former is garbage collected
and the latter is not), (2) why there is typically no need to create an
unmanaged string to pass to Win32 from .NET, and (3) the conditions under
which creating an unmanaged string to pass to Win32 WOULD be necessary?

Thanks,

Scott



Sat, 18 Sep 2004 13:46:19 GMT  
 Managed and unmanaged strings
Hi Scott,

Quote:
> In his article in the April 2002 Windows Developer, David Union discusses
> passing a string from C# to the Win32 MessageBox function (not the .NET
> MessageBox function).  He remarks:

>   The sample also indicates that the C# String class is sufficient to
>   handle LPCTSTR.  Technically, we could define an explicit unmanaged
>   string and pass that into the Win32 API function, but that should seldom
>   be necessary.

> Can somebody elaborate on (1) the difference between a managed string and
> an unmanaged string (beyond the fact that the former is garbage collected
> and the latter is not),

Actually, I have to admit I have no idea what the hell he is talking about
here. However, I imagin he is talking about more "complex" types of string
types, other than your basic C-like char array. Obviously, there's BSTRs,
but those are already handled by the runtime.

One that comes to mind that might fall under what he seems to say would be
some fairly low-level structures in the Win32 API coming out of the kernel
definitions, particularly the UNICODE_STRING structure used by the LSA api.
It is declared like this:
typedef struct _LSA_UNICODE_STRING {
  USHORT Length;
  USHORT MaximumLength;
  PWSTR  Buffer;

Quote:
} LSA_UNICODE_STRING, *PLSA_UNICODE_STRING;

Obviously, that's one not directly supported by the .NET marshaller, so
you'd need to define such a struct in managed code and add some extra
marshalling hints to your P/Invoke declarations.

OTOH, I admit this is an educated guess :)

--
Tomas Restrepo



Sat, 18 Sep 2004 19:17:37 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. conversion from managed System::String to unmanaged UUID?

2. better way to use strings in managed and unmanaged code

3. Managed String to unmanaged

4. Converting managed String to unmanaged char*

5. Exception across managed/unmanaged

6. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

7. Managed vs. Unmanaged

8. Debugging into managed c++ DLL that is being used by unmanaged code

9. managed and unmanaged object pointers in same dll

10. LINK : error LNK2020: unresolved token (0A000055) (managed to unmanaged)

11. Managed C++ call unmanaged DLL error

12. Performance of unmanaged C++ in a managed C++ app

 

 
Powered by phpBB® Forum Software