473,569 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C style string in managed class and <string.h>

I am trying to use a C style string in a managed class. When I try to call a function like strcmp I get an error. For example, this code

__gc MyClass
char str _nogc [16]

void foo()
strcpy(str, "Hello")

Would give the error
error C2664: 'strcpy' : cannot convert parameter 2 from 'char [16]' to 'const char *
Cannot convert a managed type to an unmanaged typ

I would use System::String, but I am porting some old code and I'd rather not rewrite every single string processing bit of it

Anyone know of a way to use C style string functions in a managed class?
Nov 17 '05 #1
2 2115
mccoyn wrote:
I am trying to use a C style string in a managed class. When I try to call a function like strcmp I get an error. For example, this code:

__gc MyClass {
char str _nogc [16];

void foo(){
strcpy(str, "Hello");
}
}

Would give the error:
error C2664: 'strcpy' : cannot convert parameter 2 from 'char [16]' to 'const char *'
Cannot convert a managed type to an unmanaged type
I would use System::String, but I am porting some old code and I'd rather not rewrite every single string processing bit of it.

Anyone know of a way to use C style string functions in a managed class?


The array str is physically part of a __gc object, and thus the
corresponding pointer type is an interior __gc pointer. To pass it to
strcpy, you'll have to convert it to a pinning pointer, e.g.

char __pin * p = str;
strcpy(p, "Hello");

Alternatively, you could make str a pointer and manage its memory
dynamically. Then the string data would live on the unmanaged heap, and you
could pass str to strcpy directly.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
mccoyn wrote:
Thanks, this seems to work well.

Is it possible to reuse a pinned variable for multiple strings, or will this create a memory leak? I want something like the following.

char __pin * ptr1;
char __pin * ptr2;

ptr1 = str1;
ptr2 = str2;
strcpy(str1, str2);

ptr1 = str3;
ptr2 = str4;
strcpy(str3, str4);


That's fine. In addition, you can unpin an object by assigning NULL to a
pinning pointer.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #3

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

Similar topics

6
8313
by: Christopher Benson-Manica | last post by:
I have a C-style string (null-terminated) that consists of items in one of the following formats: 14 characters 5 characters space 8 characters 6 characters colon 8 characters 5 characters colon 8 characters Items are delimited by semicolons or commas. I have to produce a string delimited only by semicolons and containing items in the...
1
4363
by: joye | last post by:
Hello, How to convert an unmanaged string with char array type to a managed string with char array type? Thanks. Regards, Tsung-Yu
2
1398
by: Xiaoshen Li | last post by:
Dear All, I hear a lot that cstring cannot be changed. But my code: /************************************************************* * Testing cstring(a pointer) * **********************************************************/ #include <iostream> using namespace std;
24
17447
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET...
1
1623
by: hrobinson4 | last post by:
Is there a quick and easy way to obtain the style string that the TableItemStyle properties builds without subclassing TableItemStyle and adding a new method that examins all the parent class's style properties to construct the string? - Harry
1
5576
by: TDK02 | last post by:
Hi, I need someones help to Convert C++ strings to a C-style string (char*) in VC++ 6.0. What I need to do is something as follows. There is a string as, string sh = “hello”;
10
6746
by: howa | last post by:
#include <iostream> using namespace std; const char* test(char *src) { string s(src); return s.c_str(); }
3
3357
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above...
16
3647
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a systematic way?
0
7697
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...
0
7612
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...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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...
1
7672
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...
0
7968
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
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...
1
2113
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
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.