2

I have a lib that returns a CFString, and I'm trying to get that string value in C#.. The problem is that I don't know how to do this in C#..

Making the external function return a CFString won't work as it throws an exception "Type MonoMac.CoreFoundation.CFString which is passed to unmanaged code must have a StructLayout attribute."

Then I thought that I could get the string as a byte array and then convert it to a string in C#, but then, I have another problem, I don't know how to convert in C the CFString to a byte array :/

C# Dll Import stuff
[DllImport("lib")]
public static extern MonoMac.CoreFoundation.CFString  test();   

[DllImport("lib")]
public static extern byte[] test();     


C Library sample
CFStringRef test()
{
return CFSTR("test string");
}

If anyone knows a way to do this please help me out ;)

Thanks

BraCa
  • 351
  • 1
  • 5
  • 13
  • What is the default calling convention for Mono? You should declare it both in the C code and the C# and make sure they match. – Security Hound Aug 11 '11 at 15:06

2 Answers2

1

Try:

 [DllImport("lib")]
 public static extern IntPtr test();

Then use:

 new CFString (test ()).ToString ();
poupou
  • 43,413
  • 6
  • 77
  • 174
0
  1. using UnixMarshal.StringToHeap to convert string to IntPtr.
  2. using UnixMarshal.PtrToString to convert IntPtr to string.
Utmost
  • 51
  • 1