473,770 Members | 3,912 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.szV ersion,"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 4719
"joye" <jo**@askey.com .tw> wrote in message news:<#9******* *******@TK2MSFT NGP09.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.szV ersion,"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.szVe rsion,"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
2298
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 unmanaged DLL. I have a "Unmanaged" Client DLL that I'm creating a Managed "wrapper" to be used in VB.Net and/or C#.. In the my Client DLL (unmanaged), There are several functions where I need to allocate a "client side" memory buffer to...
16
14138
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 byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
6
2556
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 C++ was the worst ! It is 20 times slower that the native version and 6 times slower that the C# versio !! Because i wanted to use automatic memory management offered by .Net framework, i used the managaed classes available. e.g. i have used...
1
4383
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
2157
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
5911
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 vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How can I convert this Byte() in this IntPtr ?
6
5900
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 will prove useful. structure MyStruct { // this is a complicated struct declaration in the sense
0
1401
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 to read pictures from ip cameras. I get the native c++ decoder worked so far and now try to send callbacks from unmanaged to managed code, to signal wenn a picture is fully received. To get a first view on that area i implemented a little test...
8
2286
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 to read pictures from ip cameras. I get the native c++ decoder worked so far and now try to send callbacks from unmanaged to managed code, to signal wenn a picture is fully received.
0
9602
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
9439
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
10071
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
10017
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
8905
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...
1
7431
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
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
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.