Passing pointers 'round and 'round?
-
If I have a c++ app that gets a pointer to a c++ char, will I be able to read that value into a C# char from the address from the c++ app? Or are they incompatible?
AFAIK
IntPtr
is the closest you can get through managed code. If there is a method in the dll which returnschar*
, try getting the return value into anIntPtr
and then use it.50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
AFAIK
IntPtr
is the closest you can get through managed code. If there is a method in the dll which returnschar*
, try getting the return value into anIntPtr
and then use it.50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
I can't figure out what is wrong with my c#/c++ code trying to do this... c# Code
try
{
char c;
string s;
int i;
i = System.Convert.ToInt32(Environment.GetCommandLineArgs()[1]);
unsafe
{
char* charp;
charp = (char*)i;
c = *charp;
}
s = c.ToString();System.IO.File.WriteAllText("C:\\\\users\\\\name\\\\file.txt", System.IO.File.ReadAllText("C:\\\\users\\\\name\\\\file.txt") + Environment.NewLine + s); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException); }
C++ Code that launches the above and passes the pointer to a char.
int inn = &the_char;
ShellExecute(NULL,NULL,"C:\\csharpfile.exe",inn,NULL,0); -
I can't figure out what is wrong with my c#/c++ code trying to do this... c# Code
try
{
char c;
string s;
int i;
i = System.Convert.ToInt32(Environment.GetCommandLineArgs()[1]);
unsafe
{
char* charp;
charp = (char*)i;
c = *charp;
}
s = c.ToString();System.IO.File.WriteAllText("C:\\\\users\\\\name\\\\file.txt", System.IO.File.ReadAllText("C:\\\\users\\\\name\\\\file.txt") + Environment.NewLine + s); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException); }
C++ Code that launches the above and passes the pointer to a char.
int inn = &the_char;
ShellExecute(NULL,NULL,"C:\\csharpfile.exe",inn,NULL,0);I cannot figure out what your code is trying to do. Take the first command line argument and convert it to an integer. In an unsafe block cast the integer to a character pointer and take the first character of that array. Convert that character to a string. Use that string for some purpose. Did you try running this through the debugger to see what results you are getting?