Have you ever tried to pinvoke a function, that returns a pointer to an array of structs? There's you can easily marshale them using safe C# code but if the code was not written already and you can't simply copy-paste that :), I promise to you, that you'll have to do a lot more work to get it done. unsafe code might be a quicker solution.
You could use unsafe code in the following situations:
a) You are not writing code or utilities, that might or will run in restricted environments, such as with an ISP environment. And ISPs (eg.) should not allow .NET to run third-party code that needs full-trust policies.
b) You have knowledge about pointers using C++
c) Your program will not be completely 'type safe', this is what some evangelists say, however, I don't see a flawless windows or webfarm if only everything were typesafe :).
Therefore, I propagate, that the biggest part, if not all, of your code, is type-safe. If some tiny utilities, are tested well and in favor of speed and power and possibilities, need to be type-unsafe, just go on.
If conditions have been considered, make your decision. Eventually stop reading now and wait for other postings :).
Such a candidate to be marked as unsafe, would be DsCrackNames, for the COM/automation world, it would be clever to instantiate the NameTranslate class which has an IDispatch interface (good for scripting as well).
Since ít's my hobby, to avoid easy solutions :) I wrote a unique (for the moment, I did not find another on the internet) wrapper for DsCrackNames which run nearly identical its earlier automation friend.
Let's have a look at the MSDN definition of this function.:
Wow! this promises a lot of troubles, since PDS_NAME_RESULT is a pointer to a structure which contains an array of pointers to another sequential struct.
I do challence you, to write a 'safe' equivalent to it, I tried it, and believe me, the .NET framework does its stinking best to tell you that your attributes are not valid.
Of course, we understand that it simply is not possible unless you jump to a MC++ solution or you use the definition from www.pinvoke.net which is requires a lot tricky code (no offense to anybody)... .
You cannot use attributes on the structure, and pass the stuff in one single call to the platform invoke and have the net framework do the actual marshaling for you. You should create some looping work using Marshal.ReadInt32 (..) if you go for the 'safe' code solution .
I want to stress the point that safe platform invokes are not necessarily safer! They can leak memory as hell sorry, as well.
I've tried to use the [MarshalAs] attribute, but a having interop fill an array of IntPtrs inside a struct is not supported by the IL environment. Yes, you can try something like
IntPtr firstItem; //will work only if you crack one item or if you use Marshal.ReadInt32 etc code seen at pinvoke.net
Or try this... But now you have a managed array of IntPtr and that won't work as well.
IntPtr pDS_NAME_RESULT_ITEM; //won't be marshaled since the framework cannot create and marshale this array on the fly
and DS_NAME_RESULT ITEM would be like:
public int status;
public string pDomain;
public string pName;
Let's see how easy you could write the solution set the 'unsafe' attribute in spite of how disastrous this might sound.
Unsafe only allows for easy interop with unmaged IL code, this is a facto default with MC++, where you easily can use all those .h files, (windows.h, winbase.h etc) without having to retype all your DllImport statements or without having to copy them from http://www.pinvoke.net which often has untested declares. I could have use MC++ as well, but if you want to expose the assembly for reusage, you'll start to redefine all those Win32 enums and constants anyway. We have to learn to live with the border between managed and unmanged code.
Our 'unsafe' CSharp code, looks very much like a C++ implementation. In addition, it offers some improvements over the IADsNameTranslate interface (that you derive from NameTranslate, with Guid("b1b272a3-3625-11d1-a3a4-00c04fb950dc").
Some remarks about my style of programming: I really don't like ansi or Win9x compatible code. Screw it! :), as you can see, my declares favor Windows 2000 and higher. Also take in account, that this is not full proof & tested code.
Have fun using this code.
/* Copyright, Nierop Webconsultancy 2005 www.nieropwebconsult.nl
* Use of this code, in your projects, is for your own risk.
* If you modify the code, you send improvements back
* If you copy the code, you won't remove the credits for the code
public enum ADS_NAME_INITTYPE_ENUM
/// Initializes a NameTranslate object by setting the domain that the object binds to.
ADS_NAME_INITTYPE_DOMAIN = 1,
/// Initializes a NameTranslate object by setting the server that the object binds to.
ADS_NAME_INITTYPE_SERVER = 2,
/// Initializes a NameTranslate object by locating the global catalog that the object binds to
ADS_NAME_INITTYPE_GC = 3
public enum DS_NAME_FORMAT
DS_UNKNOWN_NAME = 0,
DS_FQDN_1779_NAME = 1,
DS_NT4_ACCOUNT_NAME = 2,
DS_DISPLAY_NAME = 3,
DS_UNIQUE_ID_NAME = 6,
DS_CANONICAL_NAME = 7,
DS_USER_PRINCIPAL_NAME = 8,
DS_CANONICAL_NAME_EX = 9,
DS_SERVICE_PRINCIPAL_NAME = 10,
DS_SID_OR_SID_HISTORY_NAME = 11,
DS_DNS_DOMAIN_NAME = 12
/// Indicates that there are no associated flags
DS_NAME_NO_FLAGS = 0x0,
/// Performs a syntactical mapping at the client without transferring over the network.
/// The only syntactic mapping supported is from DS_FQDN_1779_NAME to DS_CANONICAL_NAME or DS_CANONICAL_NAME_EX.
/// DsCrackNames returns the DS_NAME_ERROR_NO_SYNTACTICAL_MAPPING flag if a syntactical mapping is not possible.
DS_NAME_FLAG_SYNTACTICAL = 0x1,
/// Forces a trip to the domain controller for evaluation, even if the syntax could be cracked locally
DS_NAME_FLAG_EVAL_AT_DC = 0x2,
/// The call fails if the domain controller is not a global catalog server.
DS_NAME_FLAG_GCVERIFY = 0x4,
/// Enables cross forest trust referral.
DS_NAME_FLAG_TRUST_REFERRAL = 0x8
public enum DS_NAME_ERROR
/// The conversion was successful.
DS_NAME_NO_ERROR = 0,
/// A generic processing error occurred.
DS_NAME_ERROR_RESOLVING = 1,