Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mDNSResponder DLL.NET doesn't marshal strings correctly



Pavel,

.NET provides an attribute called CharSet to indicate marshalling behaviour. Default behaviour for C++ is CharSet.None, equivalent to CharSet.ANSI :-(. Specifying CharSet.Unicode should get the Unicode strings without having to code the marshalling yourself. CharSet applies to the name of the called DLL functions and string parameters passed to those functions.

Here's some follow-up info:

Specifying a character set to P/Invoke - this has links to examples including a call from C++:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ cpguide/html/cpconCharSetObjectField.asp


Calling unmanaged code: using CharSet
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ vccore/html/vcwlkSysimportAttributeTutorial.asp



Specifics about CharSet
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/ html/ frlrfSystemRuntimeInteropServicesDllImportAttributeClassCharSetTopic.asp




Rob


On Jul 19, 2004, at 6:07 AM, Pavel Repin wrote:

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.
_______________________________________________
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.


References: 
 >mDNSResponder DLL.NET doesn't marshal strings correctly (From: Pavel Repin <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.