473,508 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert managed char array to unmanaged char*?

Dear All,

Many old C standard library is useful for string operation such as strcpy.
But I meet some problem, a managed char array which declared in a structure
and this structure has a instance declared in a__gc class.
Sample code as following,

namespace Configure
{
using namespace System ;

typedef struct _SW{
char szVersion[64] ;
} SW,*PSW ;

public __gc class Config{
private :
SW m_sw ;
public :
Config() { strcpy(m_sw.szVersion,"1.0.0.0") ; }
} ;
}

There is a complier error at strcpy() function: Can't conver char[64] to
char *.

My question is how to conver this managed char array to char* , thus, I can
use the standard C lib such as strcpy(), and strcat().
Does any can tell me the solution? Thank you very much.

Regards,
Tsung-yu

Nov 17 '05 #1
5 4688
"joye" <jo**@askey.com.tw> wrote in message news:<#9**************@TK2MSFTNGP09.phx.gbl>...
Dear All,

Many old C standard library is useful for string operation such as strcpy.
But I meet some problem, a managed char array which declared in a structure
and this structure has a instance declared in a__gc class.
Sample code as following,

namespace Configure
{
using namespace System ;

typedef struct _SW{
char szVersion[64] ;
} SW,*PSW ;

public __gc class Config{
private :
SW m_sw ;
public :
Config() { strcpy(m_sw.szVersion,"1.0.0.0") ; }
} ;
}

There is a complier error at strcpy() function: Can't conver char[64] to
char *.

My question is how to conver this managed char array to char* , thus, I can
use the standard C lib such as strcpy(), and strcat().
Does any can tell me the solution? Thank you very much.

Regards,
Tsung-yu

Hi

This will do the magic:
Config()
{
Config __pin *This = this;
strcpy(This->m_sw.szVersion,"1.0.0.0") ;
}

To copy the native array you have to pin the gc class that hold it
before referring to a native function such as strcpy()

Alon Fliess
[VC++ MVP]
Nov 17 '05 #2

Hi Alon,

It's work. Thank you very much.

By the way, why I can't use this way as following,

strcpy((__pin char*)m_sw.szVersion,"1.0.0.0");

Regards,
Joye

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #3
Hello Alon,

I change the code a bit as following,

namespace Configure
{
using namespace System ;

typedef struct _SW{
char szVersion[64] ;
} SW,*PSW ;

public __gc class Config{
public :
SW m_sw ;
public :
Config(char *szVersion)
{
Config __pin *This = this ;
strcpy(this->m_sw.szVersion,szVersion) ;
} ;
}

Assume the calling code as following,

Config *pCfg1 = new Config("1.0.0.1") ;
Config *pCfg2 = new Config(pCfg1->m_sw.szVersion) ;

-->Here still get an error : can't conver char[64] to char *

How to resolved this?
Thank.

Regards,
Joye

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #4
Hello Alon,

It's work, thank you very much.
Now, I change the code a bit as following,

namespace Configure
{
using namespace System ;

typedef struct _SW{
char szVersion[64] ;
} SW,*PSW ;

public __gc class Config{
public :
SW m_sw ;
public :
Config(char *szVersion)
{
Config __pin *This = this ;
strcpy(this->m_sw.szVersion,szVersion) ;
} ;
}

Assume the calling code as following,

Config *pCfg1 = new Config("1.0.0.1") ;
Config *pCfg2 = new Config(pCfg1->m_sw.szVersion) ;
-->Here still get an error : can't conver char[64] to char *

How to resolved this?
Thanks.

Regards,
Joye

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #5
Hi

The problem in the code is the usage of the "this" keyword instead of
the "This" pinned pointer. If you find it confusing you may want to use
other name for the pinned pointer.
Just change the line to:
strcpy(This->m_sw.szVersion,sz*Version) ;

Alon Fliess
[VC++ MVP]

Nov 17 '05 #6

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

Similar topics

2
2272
by: Weston Fryatt | last post by:
(Sorry for spamming multiple groups, But I need a solution to this problem) I think this should be a simple question on Memory Allocation in a managed DLL and passing a memory pointer over to an...
16
14072
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
6
2535
by: Omid Hodjati | last post by:
Hi All, I implemented an encryption algorithm using C#, native C++ and managed C++. Then I measured the CPU time used for executing this algorithm in all implementation. The managed version of...
1
4353
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
0
264
by: joye | last post by:
Hello , I meet some data convertion problem, the sample code as following, namespace Configure { using namespace System ; typedef struct _SW{ char szVersion ;
1
2131
by: joye | last post by:
Hello , I meet some data convertion problem, the sample code as following, namespace Configure { using namespace System ; typedef struct _SW{ char szVersion ;
8
5894
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
6
5883
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
0
1372
by: DavidT | last post by:
Hello, at first, exuse if the following question is simple to solve, but i normaly coding with C# and now have to use C++/CLI for one project. My Problem is that i have to use a native c++ sdk...
8
2259
by: DavidT | last post by:
Hello, at first, exuse if the following question is simple to solve, but i normaly coding with C# and now have to use C++/CLI for one project. My Problem is that i have to use a native c++ sdk...
0
7224
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
7118
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
7323
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,...
0
7379
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
7493
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
5625
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,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
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 ...

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.