By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,682 Members | 1,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,682 IT Pros & Developers. It's quick & easy.

StringBuilder and LPWSTR

P: n/a
I need to pass a StringBuilder as a parameter that expects LPWSTR
(SetConsoleTitle).

How do I do this in MC++?
Thanks,
Shawn
Dec 11 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
>I need to pass a StringBuilder as a parameter that expects LPWSTR
(SetConsoleTitle).

How do I do this in MC++?


Sorry, I got it wrong. I need to pass a StringBuilder to GetConsoleTitle,
and a String __gc* to GetConsoleTitle.

For the GetConsoleTitle API, I'm doing

void Console::set_Caption(String *caption)
{
if (caption->Length < CAPTION_LENGTH)
{
SetConsoleTitle((LPCWSTR)&caption);
}
}

But I'm getting strange unicode character in the Title before it throws an
exception.

For the GetConsoleTitle, I'm doing

String *Console::get_Caption()
{
StringBuilder *caption = new StringBuilder(CAPTION_LENGTH);
return caption->ToString(
0,
(DWORD)GetConsoleTitle((LPWSTR)&caption, CAPTION_LENGTH)
);
}

and nothing happens.

I'm converting code from C#.

So, in effect, I want to know what the proper way to pass the StringBuilder
and the String is to the API calls. I've looked up the platform SDK,
googled for hours, and even got the "Essential Guide to Managed Extensions
for C++" Book and nothing has helped me one iota. I'm terribly rusty with
C++ so that probly isn't helping.

Thanks for you help.
Thanks,
Shawn
Dec 11 '05 #2

P: n/a
Hi Shawn!
Sorry, I got it wrong. I need to pass a StringBuilder to GetConsoleTitle,
and a String __gc* to GetConsoleTitle.


http://blog.kalmbachnet.de/?postid=18

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 11 '05 #3

P: n/a
>> Sorry, I got it wrong. I need to pass a StringBuilder to
GetConsoleTitle, and a String __gc* to GetConsoleTitle.


http://blog.kalmbachnet.de/?postid=18


Thanks for this post, but I'm still left trying to figure out how to pass
the string into the GetConsoleTitle. It won't accept a wchar_t or a String.
Thanks,
Shawn
Dec 11 '05 #4

P: n/a
Hi Shawn!
Sorry, I got it wrong. I need to pass a StringBuilder to
GetConsoleTitle, and a String __gc* to GetConsoleTitle.


http://blog.kalmbachnet.de/?postid=18


Thanks for this post, but I'm still left trying to figure out how to pass
the string into the GetConsoleTitle. It won't accept a wchar_t or a String.


Upps... sorry I misread your first post...

I recommend:
<code>
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;

[DllImportAttribute("kernel32.dll")]
extern UInt32 GetConsoleTitle(StringBuilder *str, UInt32 nSize);

int _tmain()
{
StringBuilder *sb = new StringBuilder(1000);
GetConsoleTitle(sb, sb->get_Capacity());
Console::WriteLine(sb->ToString());
return 0;
}
</code>

But you can also use:
<code>
#include <windows.h>
#using <mscorlib.dll>
using namespace System;

int _tmain()
{
wchar_t szBuffer[1000];
GetConsoleTitleW(szBuffer, 1000);
String *str = new String(szBuffer);
Console::WriteLine(str);
return 0;
}
</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 11 '05 #5

P: n/a
> Hi Shawn!
Sorry, I got it wrong. I need to pass a StringBuilder to
GetConsoleTitle, and a String __gc* to GetConsoleTitle.

http://blog.kalmbachnet.de/?postid=18


Thanks for this post, but I'm still left trying to figure out how to pass
the string into the GetConsoleTitle. It won't accept a wchar_t or a
String.


Upps... sorry I misread your first post...

I recommend:
<code>
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;

[DllImportAttribute("kernel32.dll")]
extern UInt32 GetConsoleTitle(StringBuilder *str, UInt32 nSize);

int _tmain()
{
StringBuilder *sb = new StringBuilder(1000);
GetConsoleTitle(sb, sb->get_Capacity());
Console::WriteLine(sb->ToString());
return 0;
}
</code>

But you can also use:
<code>
#include <windows.h>
#using <mscorlib.dll>
using namespace System;

int _tmain()
{
wchar_t szBuffer[1000];
GetConsoleTitleW(szBuffer, 1000);
String *str = new String(szBuffer);
Console::WriteLine(str);
return 0;
}
</code>


I was just getting ready to post my message when I saw this response from
you. I finally put

[DllImport("kernel32", EntryPoint="GetConsoleTitle", CharSet=CharSet::Auto)]
extern "C" int GetConsoleTitle2(StringBuilder *title, int size);

at the top (I couldn't redfine GetConsoleTitle since it was already
#included in "wincon.h" and it works just fine.

Of course, do know what I've been struggling with over the last 3 days on
this? Where you say

wchar_t szBuffer[1000]

I was saying

wchar_t *szBuffer[1000]

And that is where my frustration lies, because that could not be passed into
the API call. Doh... I am just way too rusty at C++ for this.

In any case, thanks for your help.
Thanks,
Shawn
Dec 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.