Hi,
It is my understanding that dns_sd library uses zero-terminated UTF8
strings
in its public interface. Correct me if I am wrong (BTW, it is not
obvious
either from online docs or from the header docs. The mDNS/DNSSD draft
is the
doc that mentions this).
The .NET wrapper assembly (DLL.NET) treats strings as Ansi and uses
incorrect/default marshalling. This can be easily fixed. Which I did
in my
local copy of the source. I have a "diff -ur ..." patch, if anyone's
interested. PString.h is a logical place to add the following
marshalling
code (MS MC++).
Everywhere where you go from managed System::String to UTF8 C-string,
you
gotta say something like this:
using namespace System::Text;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;
// turn the managed string s to 0-terminated utf8 byte array
Byte bytes[]=__gc new Byte[Encoding::UTF8->GetByteCount(s)+1];
int n=Encoding::UTF8->GetBytes(s,0,s->Length,bytes,0);
Debug::Assert(n==bytes->Length-1);
bytes[n]=0; // don't forget the 0 terminator
// marshal to unamanged memory and call a dns_sd function
IntPtr unmanaged = Marshal::AllocHGlobal(bytes->Length);
for(int i=0;i<bytes->Length;i++)
Marshal::WriteByte(unmanaged, i, bytes[i]);
call_a_dns_sd_function(..., (const char*) unmanaged.ToPointer(), ...);
Marshal::FreeHGlobal(unmanaged);
When you get a dns_sd callback reply, you will have to turn UTF8
C-strings
in unmanaged memory to System::String like so:
using namespace System::Text;
using namespace System::Runtime::InteropServices;
// c_string is the unmanaged UTF8 string.
// marshal c_string into managed byte array:
Byte utf8bytes[] = __gc new Byte[strlen(c_string)];
IntPtr ptr((void*) c_string);
for(int i=0;i<utf8bytes->Length;i++)
utf8bytes[i]=Marshal::ReadByte(ptr, i);
// decode from UTF8 into a .NET unicode string:
String managed_string = Encoding::UTF8->GetString(utf8bytes);
Caveat emptor. There is no error checking code in these examples.
Hope this helps.
PS: There may be a one-liner way of doing this in .NET, but I haven't
found
anything like that yet during my limited exposure to this framework.
--
Pavel Repin
_______________________________________________
rendezvous mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/rendezvous
Do not post admin requests to the list. They will be ignored.