Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

System::String *sa and LPTSTR in new C++ managed (studio 7)

376 views
Skip to first unread message

Anton

unread,
Feb 28, 2002, 7:58:42 AM2/28/02
to
Let's say I have
System::String *sa;
and in sa I have some string... (let's say sa = S"c:\\").

And I want to use classical function
BOOL SetCurrentDirectory(
LPCTSTR lpPathName // new directory name
);

Now if I put variable sa as argument I get an error...

c:\Besedila\Visual Studio Projects\C\Razno\Pretvordbe besed\Pretvordbe
besed.cpp(42): error C2664: 'SetCurrentDirectoryW' : cannot convert
parameter 1 from 'System::String __gc *' to 'LPCWSTR'

So how I convert System::String to LPCWSTR?


Anton, ml.
P.S. Please don't ask why I need this and I am quite sure that micro
and soft created a function and there "must" be a simple normal way to
convert it to "normal" string in C/C++, but I can't find it in help
files...

David V. Corbin

unread,
Feb 28, 2002, 9:38:55 AM2/28/02
to
Most implementations of String class do NOT have a
conversion to "char *". This is becuase direct access
would allow corruption of the internal (probably reference
counted) data.

In many implementations there is a "char const *" wtich
wil work for a LPCSTR. It looks like your code wants a
wide (unicode) char pointer, depending on the
implementation of the string class, this may not be
available.

Sorry that I don't jhave a magic bullet, but this IS one
of the issues ofr going from a raw to manager environment,
be it either C to C++ or C++ to .NET.

David.

>.
>

Craig Powers

unread,
Feb 28, 2002, 9:51:42 AM2/28/02
to
"David V. Corbin" wrote:
>
> Most implementations of String class do NOT have a
> conversion to "char *". This is becuase direct access
> would allow corruption of the internal (probably reference
> counted) data.

In this particular case, it's not a char*, it's a char const *, so it's
not allowed to be modified. Hence, the safety issue should not be
a problem.

The more likely issue is conversion from ANSI to UNICODE. I would
expect (and might very well be wrong) that System::String has either
a default conversion or accessor function for LPCSTR, but a conversion
to unicode will still be required.

Alternatively, the OP could just call SetCurrentDirectoryA instead of
SetCurrentDirectoryW.

John

unread,
Feb 28, 2002, 2:48:06 PM2/28/02
to

"Anton" <vah...@email.si> wrote in message
news:8ebfff67.02022...@posting.google.com...

I'm curious why LPCTSTR is resolving to wchar_t, so you have #defined
_UNICODE, but String is constructing a char buffer. You didn't show the code
that allocates the System::String object. That's about all I can say since I
know nothing about.NET, maybe try the .NET newsgroup. Did you try the
ToCharArray() function? check the documentation on System::String in MSDN
John


Igor Tandetnik

unread,
Mar 1, 2002, 6:25:32 PM3/1/02
to
See "HOW TO: Convert from System::String* to Char* in Visual C++ .NET
(Q311259)" at

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q311259

--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"Anton" <vah...@email.si> wrote in message
news:8ebfff67.02022...@posting.google.com...

0 new messages