How to convert managed char array to unmanaged char*?

J

joye

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
 
A

Alon Fliess

joye said:
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]
 
J

Joye

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
 
J

Joye

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
 
J

Joye

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
 
A

Alon

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top