Click here to Skip to main content
13,056,397 members (111,458 online)
Click here to Skip to main content

Stats

314K views
3.8K downloads
75 bookmarked
Posted 23 Jul 2002

NT Security Classes for .NET

, 19 Feb 2004
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)

Share

About the Author

David Hall
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.

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170728.5 | Last Updated 20 Feb 2004
Article Copyright 2002 by David Hall
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid