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

Passing C++ const char* (String from JAVA) to C#

I'm actually a Java programmer, and I'm messing with JNI a bit,

so I've managed to pass an int from java to c++ and to c# and back.
no problems cause it gets serialized automatically.

but when dealing with string - the object JNI passes is jstring which
casts to char* but how to pass it to c#?

Do I need to Marshal it on the c# side??? can someone post a bit of
code example????

my C++ code is like so :
Expand|Select|Wrap|Line Numbers
  1. ...
  2. JNIEXPORT jint JNICALL Java_jni_ExecuteString_executeString (JNIEnv *env, jobject, jstring strinput) {
  3.        ExecuteStringC* myObj = new ExecuteStringC();
  4.         const char *str;
  5.     int result;
  6.     str = env->GetStringUTFChars(strinput, 0);
  7.         result = myObj->callExecuteString(str);
  8.     env->ReleaseStringUTFChars(strinput, str);  
  9.     return result;
The ExecuteStringC class :
Expand|Select|Wrap|Line Numbers
  1. public __gc class ExecuteStringC
  2. {
  3. public:
  4.     CSharpExecuteString __gc *execObj; 
  5.     ExecuteStringC() {
  6.         execObj = new CSharpExecuteString();
  7.     }
  8.     int callExecuteString(const char* str) {
  9.         return execObj->executeString(str); 
  10.       }
  11. };
and finally the C# part :
Expand|Select|Wrap|Line Numbers
  1.   class CSharpExecuteString
  2.     {
  3.         public int executeString(string  stringToExec)
  4.         {
  5.             Console.WriteLine("C# Says : " + stringToExec);
  6.             return 0;
  7.         }
  8.     }
Since I'm executing this code from Java it fails but I can't tell where, is it in the first or 2nd classes (C++) ??? or maybe in the C# side.
I do believe it's the lather, but if anyone can shed some info on the subject (posting some code will be nice...)


10x in advance to all contributors :)
Mar 3 '10 #1
1 4316
weaknessforcats
9,208 Expert Mod 8TB
There are a lot of C++/C# interoperability topics in MSDN complete with examples. Microsoft calls this Managed C++.
Mar 4 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
17
by: BlindHorse | last post by:
Help!!! I need someone to tell me why I am getting the err msg error C2440: '=' : cannot convert from 'char *' to 'char' //==================== #include <iostream>
3
by: Steven Taylor | last post by:
Hope someone can assist. I'm trying to answer a book question. I'm going around in circles in relation to 'pointer-to-char'. Object : A short program is to be created, which involves a...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
4
by: James | last post by:
How do you pass a string into a function and set the contents of an internal string field to be the contents of the string passed into the Set method? My attempt at doing this below doesn't work...
1
by: | last post by:
Hi all, I am writing a sendmail milter application in Java. The incoming mails will usually have image file as attachments. My application is currently able to extract the ImageFile and save it...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
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
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...

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.