473,803 Members | 2,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dumb String Conversion Problem?

I have a function that requires a LPCTSTR parameter. I have the value I
want to pass to it in a TextBox->Text field. Is there any way to do it in
a single assignment.

e.g.
DWORD Xyz::TheFunc(LP CTSTR lpVal)
....
TheFunc(Junk->Text->Something()) ;

Nov 17 '05 #1
8 1612
Fred Hebert wrote:
I have a function that requires a LPCTSTR parameter. I have the value I
want to pass to it in a TextBox->Text field. Is there any way to do it in
a single assignment.
No.
e.g.
DWORD Xyz::TheFunc(LP CTSTR lpVal)
...
TheFunc(Junk->Text->Something()) ;


See: Convert from System::String* to TCHAR*/CString
http://blog.kalmbachnet.de/?postid=18

<code>
#include <vcclr.h>

System::String *mstr = Junk->Text->Something();
TCHAR *ustr;
#ifdef _UNICODE
ustr = new TCHAR[mstr->get_Length() +1];
const __wchar_t __pin * umstring = PtrToStringChar s(mstr);
#else
const char* umstring = (const
char*)Marshal:: StringToHGlobal Ansi(mstr).ToPo inter();
ustr = new TCHAR[strlen(umstring )+1];
#endif
_tcscpy(ustr, umstring);
#ifndef _UNICODE
Marshal::FreeHG lobal(IntPtr((v oid*)umstring)) ;
#endif

// do something with the unmanaged string
TheFunc(ustr);

delete [] ustr;

</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2
Fred Hebert wrote:
I have a function that requires a LPCTSTR parameter. I have the value I
want to pass to it in a TextBox->Text field. Is there any way to do it in
a single assignment.

e.g.
DWORD Xyz::TheFunc(LP CTSTR lpVal)
...
TheFunc(Junk->Text->Something()) ;


The easiest way is to use the CString-Class:

<code>
#include <afx.h>

TheFunc(CString (Junk->Text->Something()) ;
</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #3
This seems to be the source of the many people's frustration (including my
own at one point). I would recommend Microsoft think seriously about putting
in some String* to char[] 'standard' .NET function (realizing they have no
control over the C++ standard itself). Or, better yet, overload assignment
(=) for 'char[]' to allow code like:

String* string_thing = "test" ;
char[33] char_array = string_thing ;

'char_array' would then have "test" as a null-terminated old-style string...

I realize the problem is 'char' came before 'String*', so 'char' knows
nothing about 'String*' (although the reverse is not true, which is why the
'string_thing' definition above is valid). But eqully, since 'char' came
first, much legacy stuff depends on 'char' still, while modern day usage of
'String*' is prominent (ala Control text fields). Conversion I think is
essential, and shouldn't look complex or complicated, and should be doable
in one line of C++ code...

My 2 cents...

[==Peteroid==]
"Fred Hebert" <fh*****@hotmai l.com> wrote in message
news:Xn******** *************** *******@207.46. 248.16...
I have a function that requires a LPCTSTR parameter. I have the value I
want to pass to it in a TextBox->Text field. Is there any way to do it in
a single assignment.

e.g.
DWORD Xyz::TheFunc(LP CTSTR lpVal)
...
TheFunc(Junk->Text->Something()) ;

Nov 17 '05 #4
Hi Peteroid!
I realize the problem is 'char' came before 'String*', so 'char' knows
nothing about 'String*'
The OP was about TCHAR, not char. But in general you are right.
Conversion I think is
essential, and shouldn't look complex or complicated, and should be doable
in one line of C++ code...


You are right. Therefor MS provided the "CString"-class which handles
the conversion icely:

System::String *mstring = S"Hello world";
// this works:
CString s1(mstring);
// and even the following works:
CString s2;
s2 = mstring;

This also handles ANSI/UNICODE builds.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #5
Jochen Kalmbach <no************ ********@holzma .de> wrote in
news:#9******** ******@TK2MSFTN GP10.phx.gbl:
Fred Hebert wrote:
I have a function that requires a LPCTSTR parameter. I have the
value I want to pass to it in a TextBox->Text field. Is there any
way to do it in a single assignment.

e.g.
DWORD Xyz::TheFunc(LP CTSTR lpVal)
...
TheFunc(Junk->Text->Something()) ;


The easiest way is to use the CString-Class:

<code>
#include <afx.h>

TheFunc(CString (Junk->Text->Something()) ;
</code>


This is what is driving me nuts.

You can't "#include <afx.h>" if you also "#include <windows.h>". ..

This is what I have to do:
char* junk = (char*)(void*)M arshal::StringT oHGlobalAnsi(Ju nk->Text);
TheFunc(junk);
Marshal::FreeHG lobal(junk);

To me this seems like an abomination.

In other "environmen ts" (non MS) all I have to do is:
TheFunc(Junk->Text.c_str() );
or
TheFunc(PChar(J unk.Text));

If MS wants people to migrate they need to make the transition easier.
At this point my evaluation of our company switching to Visual Studio is
that it is going to be very expensive to migrate our stuff and insane to
throw away 16 years of code and start over.

Am I missing something?
Nov 17 '05 #6
Hi Fred!
I have a function that requires a LPCTSTR parameter.
This is what I have to do:
char* junk = (char*)(void*)M arshal::StringT oHGlobalAnsi(Ju nk->Text);
TheFunc(junk);
Marshal::FreeHG lobal(junk);


Remember: This *only* works if you make an ANSI-Build of your App! If
you (later) switch to UNICODE, it does not work anymore!

You can't "#include <afx.h>" if you also "#include <windows.h>". ..
What is the problem? The following works perfectly:

#include <afx.h>
#include <windows.h>
#include <stdio.h>

In other "environmen ts" (non MS) all I have to do is:
TheFunc(PChar(J unk.Text));
This is exactly what I suggested:
TheFunc(CString (Junk.Text));

Am I missing something?


Maybe; I have not looked at your source...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #7
Jochen Kalmbach <no************ ********@holzma .de> wrote in
news:O#******** ******@TK2MSFTN GP14.phx.gbl:
Hi Fred!
I have a function that requires a LPCTSTR parameter.

This is what I have to do:
char* junk =
(char*)(void*)M arshal::StringT oHGlobalAnsi(Ju nk->Text);
TheFunc(junk); Marshal::FreeHG lobal(junk);


Remember: This *only* works if you make an ANSI-Build of your App! If
you (later) switch to UNICODE, it does not work anymore!

You can't "#include <afx.h>" if you also "#include <windows.h>". ..


What is the problem? The following works perfectly:

#include <afx.h>
#include <windows.h>
#include <stdio.h>

In other "environmen ts" (non MS) all I have to do is:
TheFunc(PChar(J unk.Text));


This is exactly what I suggested:
TheFunc(CString (Junk.Text));

Am I missing something?


Maybe; I have not looked at your source...


On my Visual Studio 2003 Enterprise running on XP PRO SP2, putting
"#include <afx.h>" before "#include <windows.h>" results in about dozen
warnings and errors. Reversing the order results in only one error in
afxv_w32.h which is generated by the following lines of code:

#ifdef _WINDOWS_
#error WINDOWS.H already included. MFC apps must not #include
<windows.h>
#endif

Since this is a .net app I don't fully understand this error.

Also you mentioned ANSI/UNICODE builds, where is this set?

I am an experienced programmer, but new to .net. I am probably doing
something wrong, but I find the documentation is poor and most of the
examples are to simplistic especially when it comes to mixed mode
programming.

Most of our applications are evolutions of existing apps rather than new
application from scratch. I suspect most companies who have a large
investment in code are not going to switch to .net unless there is a
reasonable upgrade path. So far I don't see it. It has been an up hill
battle just to convert these little demo programs that I originally
wrote in an hour or 2. I can't imagine what it would be like to convert
one of our multi-threaded DLLs that took a week to write. I could
probably retire converting one of our major apps, some of which took
more than a year to write.

I had fewer problems converting these program to Linux using a free
compiler! It's just a char* so why all the grief.
Nov 17 '05 #8
Hi Fred!
On my Visual Studio 2003 Enterprise running on XP PRO SP2, putting
"#include <afx.h>" before "#include <windows.h>" results in about dozen
warnings and errors. Reversing the order results in only one error in
afxv_w32.h which is generated by the following lines of code:
It is also enoght to only include "afx.h". You do not need to include
"windows.h" , because it is already included by "afx.h".
Also you mentioned ANSI/UNICODE builds, where is this set?
Right-Click on the project, select "Properties ".
In the "General" section you see an entry called "Character Set". Here
you can select "Not Set", "Multi-Byte", "Unicode"
I am an experienced programmer, but new to .net.
Unicode is not new... this option was available at least since VC5 (or
NT 3.1).
Most of our applications are evolutions of existing apps rather than new
application from scratch. I suspect most companies who have a large
investment in code are not going to switch to .net unless there is a
reasonable upgrade path. So far I don't see it. It has been an up hill
battle just to convert these little demo programs that I originally
wrote in an hour or 2. I can't imagine what it would be like to convert
one of our multi-threaded DLLs that took a week to write. I could
probably retire converting one of our major apps, some of which took
more than a year to write.
From my expirience, a reasonable upgrade is only if you use your
existing code via P/Invoke / COM-Interop and (re)write new/old parts in
C# (for example UI).

I had fewer problems converting these program to Linux using a free
compiler! It's just a char* so why all the grief.


That might be true. But you do not have all the advantages of .NET (like
reflection, which is really a bit plus!).
And you also need to reqrite your UI. Therefor I recommend to rewrite
the UI in C#.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1847
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
7
505
by: john | last post by:
On my form i have a message box called txtItemDesc that displays the french phrase qualité Père Noël. Now then when i run this code on that text box: Dim chrArr() As Char chrArr = txtItemDesc.Text.ToCharArray Dim pos As Integer While pos < chrArr.Length Dim c As Char MsgBox(Asc(chrArr(pos)) & " " & chrArr(pos)) pos = pos + 1
2
1306
by: TGF | last post by:
How do you copy a String type into a native char buffer? Dumb question, but not sure how to do it :( TGF
6
13360
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that this formatter writes into a stream. And I am not used enough to C# to convert this to a string. I tried the following code: MemoryStream stream= new MemoryStream() ; IFormatter formatter = new SoapFormatter();
11
17655
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
17
1594
by: vashwath | last post by:
#include <stdio.h> int main() { FILE *fp; char s; fopen("file.txt","w+"); fprintf(fp,"HI\n");
6
2219
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
5
5974
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For compression string ->(Unicode Conversion) byte -(Compression + Unicode Conversion) string
10
9089
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is Unicode, UCS-2LE(BMP) in Win32, and UCS4 in Linux?). I have 2 ways to do the job:
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.