Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

NT Security Classes for .NET

, 19 Feb 2004 CPOL
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
Architect
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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 20 Feb 2004
Article Copyright 2002 by David Hall
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid