Calling unmanaged libraries

S

Stephen Walch

When my managed C++ application calls standard C functions like strncmp and
malloc, am I making an expensive transition between managed and unmanaged
code? If so, are there alternate "pure managed" versions of these libraries
that I can link to?

Also, is doing "new char[1024]" in managed code invoking an unmanaged (and
therefore expensive) heap allocator? What is the most performant way to
allocate memory in managed C++?

Thanks, Steve
 
S

Stephen Walch

Of course I use String whenever possible, but I have some cases where I need
to deal with ASCII data. I think converting to Strings just to a string
compare would be ineffecient.

Check the String class.


Stephen Walch said:
When my managed C++ application calls standard C functions like strncmp and
malloc, am I making an expensive transition between managed and unmanaged
code? If so, are there alternate "pure managed" versions of these libraries
that I can link to?

Also, is doing "new char[1024]" in managed code invoking an unmanaged (and
therefore expensive) heap allocator? What is the most performant way to
allocate memory in managed C++?

Thanks, Steve
 
T

Tian Min Huang

Hello Stephen,

Thanks for your post. Please kindly note that the Char value type in .NET
represents a Unicode character, while String class .NET represents a series
of Unicode characters. If you want to call unmanaged APIs with ANSI string,
you should convert System::String to unmanaged Char* as discussed in the
following KB article:

HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?id=311259

In addition, Managed Extensions for C++ supports mixing unmanaged and
managed code and introduces the __nogc and __gc keywords to specify that an
object is allocated on the standard C++ heap or .NET managed heap. I
believe the following Spec is helpful:

4.5.3 __gc and __nogc Keywords and Arrays
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/ht
ml/vcManagedExtensionsSpec_4_5_3.asp

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tian Min Huang

Hi Stephen,

For the question regarding malloc, I suggest to create mixed-mode C++ app
which can call unmanaged code directly. Please refer to the following MSDN
article:

Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/
vcconconvertingmanagedextensionsforcprojectsfrompureintermediatelanguagetomi
xedmode.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Stephen Walch

Yes, I did this long ago, but you are not answering any of my questions!!!
Again:

When my managed C++ application calls standard C functions like strncmp and
malloc, am I making an expensive transition between managed and unmanaged
code?

Do I incur pInvoke overhead when I call malloc?
 
R

Ronald Laeremans [MSFT]

Yes, you are making a p/invoke transition. Compared to the cost of malloc or
strncmp, in general that is a cheap and not an expensive transition.
Compared to something like the cost of CharNext, it would be. The cost is on
the order of a few dozen X86 instructions.

Ronald Laeremans
Visual C++ team
 

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