473,788 Members | 3,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 4382
weaknessforcats
9,208 Recognized Expert Moderator Expert
There are a lot of C++/C# interoperabilit y 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
3969
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
58
10181
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 code... TCHAR myArray; DoStuff(myArray);
8
2558
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 INT_CONSTANT 4852 #endif
17
2274
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
2284
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 structore and one member element is a char array. A function is to be created that passes the structure as a reference, along with the 'pointer-to-char'. The function is to assign the passed-in 'pointer-to-char' to the referenced (passed-in)...
1
6436
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 the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
4
3451
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 and produces errors such as "class 'User' has an illegal zero-sized array" and "'=' : cannot convert from 'char' to char'". /* User.h */
1
9315
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 on the filesystem. This part is working perfectly. ((MimeBodyPart)p).saveFile(new File(p.getFileName())); However, I would like to pass this file as a byte array to a c++ library
40
2371
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 libary do I create a function where user passes this callback function? How would I define the function?
18
3274
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
9655
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
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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
10110
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
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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
5398
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...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.