473,408 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

StringBuilder and LPWSTR

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
5 4405
>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
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
>> 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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
1
by: Jinhee Myoung | last post by:
I wish to call a COM Object which has a parameter of type LPWSTR ** on a method call from C# The COM Method :- STDMETHODIMP CClassType::GetDataInfo(LPWSTR ** ppData) { ...... return S_OK;
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
3
by: Morgan Cheng | last post by:
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use string or StringBuilder. However, there is one problem about StringBuilder. By default, its Capacity is 16. If the returned...
6
by: MattWilson.6185 | last post by:
Hello! I am trying to convert a char * to a LPWSTR, and I am going absolutly mad! I can't find anything besides typle L"string" Unfortunetaly I can't use that... basicaly the setup is ...
2
by: dehi | last post by:
Hello, My problem ist that I don't know how to convert a variable from LPWSTR to LPCWSTR. Anyone an idea? Regards, Denis
0
by: superdawgie | last post by:
Hi, Im have implemented the IWMCredentialCallback for a windows media pushsink in my application. My pushsink works fine for publishing to the windows media server when no authentication is...
3
by: Samant.Trupti | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++) //print each...
28
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.