473,405 Members | 2,310 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,405 software developers and data experts.

Problem with calling a managed C++ function from managed C#

Hi,

I have a problem with calling a managed C++ function from C#.

The function in C++ looks like Class::Func(char* Text)

this function is compiled to an DLL and this DLL is referenced in my
C# project and I can see that I could call this function like
Func(sbyte* ).

I created a sbyte[] mytext = new sbyte[20];

So how can I pass this to the C++ function?

The compiler always give me an compileerror that it cannot convert
from sbyte* and sbyte[].

Thanks

André Betz
http://www.andrebetz.de
Nov 16 '05 #1
2 1737
Andre,

The problem is that you are delcaring the method in C++ as a pointer,
and not a managed array (the concepts are different in the managed world).

You want to declare your function like this:

Class::Func(char Text __gc[])

This syntax will change with the next version of .NET (at least, in
C++). Also, if you want to pass a string, why not use the string class,
like so:

Class::Func(String *Text)

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andre" <sk******@gmx.de> wrote in message
news:be**************************@posting.google.c om...
Hi,

I have a problem with calling a managed C++ function from C#.

The function in C++ looks like Class::Func(char* Text)

this function is compiled to an DLL and this DLL is referenced in my
C# project and I can see that I could call this function like
Func(sbyte* ).

I created a sbyte[] mytext = new sbyte[20];

So how can I pass this to the C++ function?

The compiler always give me an compileerror that it cannot convert
from sbyte* and sbyte[].

Thanks

André Betz
http://www.andrebetz.de

Nov 16 '05 #2
Not sure why you called this a "managed" function, but char* is not a
managed type.
A char* is a pointer to whatever 'char' may be (ANSI or UNICODE), so you
need to pass a pinned pointer to an array of bytes (or shorts).
byte[] mytext = new byte[20];fixed (byte* pmyText= mytext )
{
// Call your function here...// Note that the sbyte array will be pinned
for the duration of the call! Func(pmyText);
}Are you sure this is what you expected from your managed C++ function, I
guess not.Willy."Andre" <sk******@gmx.de> wrote in message
news:be**************************@posting.google.c om...
Hi,

I have a problem with calling a managed C++ function from C#.

The function in C++ looks like Class::Func(char* Text)

this function is compiled to an DLL and this DLL is referenced in my
C# project and I can see that I could call this function like
Func(sbyte* ).

I created a sbyte[] mytext = new sbyte[20];

So how can I pass this to the C++ function?

The compiler always give me an compileerror that it cannot convert
from sbyte* and sbyte[].

Thanks

André Betz
http://www.andrebetz.de

Nov 16 '05 #3

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

Similar topics

3
by: Stephen Engle | last post by:
I have an app that I am working on that is dependent on an unmanaged code library. There is a middle library of managed C++ that encapsulates the unmanaged libary functions calls into a class. ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
8
by: Shawn B. | last post by:
Greetings, Me again. I have (roughly) the following code: HANDLE hConsoleOutput; HANDLE hConsoleInput;
3
by: Mali Guven | last post by:
Hello, I have a native DLL (written in C) which is supposed to call a managed DLL (was written in C#, and an entry point was injected modifying the ildasm'd code). The exectuable calls the...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
1
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I want to give multiple native classes (in different mixed mode dlls) access to a managed output window (for error messages). Therefore I wrote a native singleton with __declspec...
3
by: aaron.m.johnson | last post by:
Help me understand this, please. I have an older VC++ COM DLL that I'm using in a C# project. One of the COM objects takes a callback object as a parameter so that it can spin off a thread and...
0
by: BornTOCode | last post by:
Hello, I am attempting to call a (Delphi) win32 DLL from a Delphi.Net webservice. I am using a slightly modified version of the hello world webservice that comes with Delphi 2006. The DLL...
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: 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?
0
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
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
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
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,...
0
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...

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.