Example #1
0
    public static void Main( string[] args )
    {
        StringBuilder buffer =
         new StringBuilder( "Hello, how are you?" );

          // use Length and Capacity properties
          Console.WriteLine( "buffer = " + buffer +
         "\nLength = " + buffer.Length +
         "\nCapacity = " + buffer.Capacity );

          buffer.EnsureCapacity( 75 ); // ensure a capacity of at least 75
          Console.WriteLine( "\nNew capacity = " +
         buffer.Capacity );

          // truncate StringBuilder by setting Length property
          buffer.Length = 10;
          Console.Write( "\nNew length = " +
         buffer.Length + "\nbuffer = " );

          // use StringBuilder indexer
          for ( int i = 0; i < buffer.Length; i++ )
         Console.Write( buffer[ i ] );

          Console.WriteLine( "\n" );
    }
	static string[] LoadRenderModelNames()
	{
		var results = new List<string>();
		results.Add("None");

		using (var holder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
		{
			var renderModels = holder.instance;
			if (renderModels != null)
			{
				uint count = renderModels.GetRenderModelCount();
				for (uint i = 0; i < count; i++)
				{
					var buffer = new StringBuilder();
					var requiredSize = renderModels.GetRenderModelName(i, buffer, 0);
					if (requiredSize == 0)
						continue;

					buffer.EnsureCapacity((int)requiredSize);
					renderModels.GetRenderModelName(i, buffer, requiredSize);
					results.Add(buffer.ToString());
				}
			}
		}

		return results.ToArray();
	}
Example #3
0
        internal static string GetOidValue(IntPtr asn1ObjectPtr)
        {
            // OBJ_obj2txt returns the number of bytes that should have been in the answer, but it does not accept
            // a NULL buffer.  The documentation says "A buffer length of 80 should be more than enough to handle
            // any OID encountered in practice", so start with a buffer of size 80, and try again if required.
            StringBuilder buf = new StringBuilder(80);

            int bytesNeeded = ObjObj2Txt(buf, buf.Capacity, asn1ObjectPtr);

            if (bytesNeeded < 0)
            {
                throw CreateOpenSslCryptographicException();
            }

            Debug.Assert(bytesNeeded != 0, "OBJ_obj2txt reported a zero-length response");

            if (bytesNeeded >= buf.Capacity)
            {
                int initialBytesNeeded = bytesNeeded;

                // bytesNeeded does not count the \0 which will be written on the end (based on OpenSSL 1.0.1f),
                // so make sure to leave room for it.
                buf.EnsureCapacity(bytesNeeded + 1);

                bytesNeeded = ObjObj2Txt(buf, buf.Capacity, asn1ObjectPtr);

                if (bytesNeeded < 0)
                {
                    throw CreateOpenSslCryptographicException();
                }

                Debug.Assert(
                    bytesNeeded == initialBytesNeeded,
                    "OBJ_obj2txt changed the required number of bytes for the realloc call");

                if (bytesNeeded >= buf.Capacity)
                {
                    // OBJ_obj2txt is demanding yet more memory
                    throw new CryptographicException();
                }
            }

            return buf.ToString();
        }
    public static string GetSid(string username, string domain)
    {
        byte [] Sid = null;
            uint cbSid = 0;
            StringBuilder referencedDomainName = new StringBuilder();
            uint cchReferencedDomainName = (uint)referencedDomainName.Capacity;
            SID_NAME_USE sidUse;

            int err = NO_ERROR;
            if (!LookupAccountName(null,username,Sid,ref cbSid,referencedDomainName,ref cchReferencedDomainName,out sidUse))
            {
                err = Marshal.GetLastWin32Error();
                if (err == ERROR_INSUFFICIENT_BUFFER)
                {
                    Sid = new byte[cbSid];
                    referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);
                    err = NO_ERROR;
                    if (!LookupAccountName(null,username,Sid,ref cbSid,referencedDomainName,ref cchReferencedDomainName,out sidUse))
                        err = Marshal.GetLastWin32Error();
                }
            }
            if (err == 0)
            {
                IntPtr ptrSid;
                if (!ConvertSidToStringSid(Sid,out ptrSid))
                {
                    err = Marshal.GetLastWin32Error();
                    return(null);
                }
                else
                {
                    string sidString = Marshal.PtrToStringAuto(ptrSid);
                    LocalFree(ptrSid);
                    return(sidString);
                }
            }
            else
                return(null);
    }
Example #5
0
 public static void TestEnsureCapacity_Invalid()
 {
     var builder = new StringBuilder("Hello", 10);
     Assert.Throws<ArgumentOutOfRangeException>("capacity", () => builder.EnsureCapacity(-1)); // Capacity < 0
     Assert.Throws<ArgumentOutOfRangeException>("capacity", () => builder.EnsureCapacity(builder.MaxCapacity + 1)); // Capacity > builder.MaxCapacity
 }
Example #6
0
    public static void TestEnsureCapacity()
    {
        var builder = new StringBuilder(40);

        builder.EnsureCapacity(20);
        Assert.True(builder.Capacity >= 20);

        builder.EnsureCapacity(20000);
        Assert.True(builder.Capacity >= 20000);

        // Ensuring a capacity less than the current capacity does not change anything
        int oldCapacity = builder.Capacity;
        builder.EnsureCapacity(10);
        Assert.Equal(oldCapacity, builder.Capacity);
    }
    static string[] LoadRenderModelNames()
    {
        var results = new List<string>();
        results.Add("None");

        var error = HmdError.None;
        if (!SteamVR.active)
        {
            OpenVR.Init(ref error);
            if (error != HmdError.None)
                return results.ToArray();
        }

        var pRenderModels = OpenVR.GetGenericInterface(OpenVR.IVRRenderModels_Version, ref error);
        if (pRenderModels == System.IntPtr.Zero || error != HmdError.None)
        {
            if (!SteamVR.active)
                OpenVR.Shutdown();
            return results.ToArray();
        }

        var renderModels = new CVRRenderModels(pRenderModels);

        uint count = renderModels.GetRenderModelCount();
        for (uint i = 0; i < count; i++)
        {
            var buffer = new StringBuilder();
            var requiredSize = renderModels.GetRenderModelName(i, buffer, 0);
            if (requiredSize == 0)
                continue;

            buffer.EnsureCapacity((int)requiredSize);
            renderModels.GetRenderModelName(i, buffer, requiredSize);
            results.Add(buffer.ToString());
        }

        if (!SteamVR.active)
            OpenVR.Shutdown();

        return results.ToArray();
    }