Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

NT Security Classes for .NET

Rate me:
Please Sign up or sign in to vote.
4.78/5 (33 votes)
19 Feb 2004CPOL2 min read 377.7K   4K   75  
A collection of .NET classes written in Managed C++ that faciliate the manipulation of NT security rights
// =========================================================================
// mgdhelp.h - Helper classes for working with managed/unmanaged code
// =========================================================================
#pragma once

using namespace System;
using namespace System::Runtime::InteropServices;

namespace mmsseclib
{
	// =========================================================================
	// Buffer template - Allocates and frees unmanaged buffer on stack
	// =========================================================================
	template <typename T>
	class Buffer
	{
	private:
		T __nogc * ptr;
	public:
		Buffer(int size = 0) : ptr(0)
		{
			if (size) Allocate(size);
		}
		Buffer(T __nogc * p) : ptr(p) {}
		~Buffer()
		{
			Free();
		}
		T __nogc * Allocate(int size)
		{
			ptr = static_cast<T __nogc *>(::LocalAlloc(LPTR, size));
			return ptr;
		}
		T __nogc * ReAllocate(int size)
		{
			ptr = static_cast<T __nogc *>(::LocalReAlloc(ptr, size, LMEM_ZEROINIT));
			return ptr;
		}
		void Attach(T __nogc * p)
		{
			Free();
			ptr = p;
		}
		T __nogc * Detach()
		{
			T* p = ptr;
			ptr = NULL;
			return p;
		}
		void Free()
		{
			::LocalFree(ptr);
			ptr = NULL;
		}
		operator T *() const
		{
			return ptr;
		}
		operator void *() const
		{
			return static_cast<void __nogc *>(ptr);
		}
		T __nogc * operator->() const
		{
			return ptr;
		}
		T __nogc * operator =(T* p)
		{
			Attach(p);
			return ptr;
		}
		T __nogc ** operator&()
		{
			return &ptr;
		}
	};

	// =========================================================================
	// MString - Wrapper for converting System::String object to unmanaged LPTSTR
	// =========================================================================
	class MString
	{
	private:
		void* sbuf;
		void Free()
		{
			if (sbuf != NULL)
				Marshal::FreeHGlobal(static_cast<IntPtr>(sbuf));
		}
	public:
		MString() : sbuf(0)
		{
		}
		MString(String* s) : sbuf(0)
		{
			*this = s;
		}
		~MString()
		{
			Free();
		}
		operator LPCTSTR() const
		{
			return static_cast<LPCTSTR>(sbuf);
		}
		operator LPTSTR() const
		{
			return static_cast<LPTSTR>(sbuf);
		}
		MString& operator = (String* s)
		{
			Free();
			if (sizeof(TCHAR) == sizeof(char))
				sbuf = Marshal::StringToHGlobalAnsi(s).ToPointer();
			else
				sbuf = Marshal::StringToHGlobalUni(s).ToPointer();
			return *this;
		}
	};
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
United States United States
I have been a Windows software developer since 1991. Most of what I create fills the need for some aspect of bigger projects that I consult on.

Comments and Discussions