473,395 Members | 1,556 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,395 software developers and data experts.

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 4680
"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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.