locked
call Ienumerable present in interface RRS feed

  • Question

  • User512724938 posted

    Hi I want to known how to call Ienumerable present in interface.

    Interface -

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace testapp
    {
        public interface IAgentProtocol
        {
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
             IEnumerable<IdentityReference> GetIdentities();
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="identity"></param>
            /// <param name="data"></param>
            /// <returns></returns>
            byte[] SignData(IdentityReference identity, byte[] data);
        }
    }
    

     Program.cs

    using Renci.SshNet;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.IO.MemoryMappedFiles;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace testapp
    {
    
       public class Program:IAgentProtocol
        {
            #region  Constants
    
            private const int WM_COPYDATA = 0x004A;
    
            private const int AGENT_COPYDATA_ID = unchecked((int)0x804e50ba);
    
            private const int AGENT_MAX_MSGLEN = 8192;
    
            /// <summary>
            /// 
            /// </summary>
            public const byte SSH2_AGENTC_REQUEST_IDENTITIES = 11;
    
            /// <summary>
            /// 
            /// </summary>
            public const byte SSH2_AGENT_IDENTITIES_ANSWER = 12;
    
            /// <summary>
            /// 
            /// </summary>
            public const byte SSH2_AGENTC_SIGN_REQUEST = 13;
    
            /// <summary>
            /// 
            /// </summary>
            public const byte SSH2_AGENT_SIGN_RESPONSE = 14;
    
            #endregion
    
            
    
             
    
            static void Main(string[] args)
            {
                  
             }
            }
    
            
    
            IEnumerable<IdentityReference> IAgentProtocol.GetIdentities()
            {
                 var hWnd = NativeMethods.FindWindow("Pageant", "Pageant");
    
                if (hWnd == IntPtr.Zero)
                {
                    //yield break;
                }
                string mmFileName = Path.GetRandomFileName();
    
                using (var mmFile = MemoryMappedFile.CreateNew(mmFileName, AGENT_MAX_MSGLEN))
                {
                    using (var accessor = mmFile.CreateViewAccessor())
                    {
                        var security = mmFile.GetAccessControl();
                        security.SetOwner(System.Security.Principal.WindowsIdentity.GetCurrent().User);
                        mmFile.SetAccessControl(security);
    
                        accessor.Write(0, IPAddress.NetworkToHostOrder(AGENT_MAX_MSGLEN - 4));
                        accessor.Write(4, SSH2_AGENTC_REQUEST_IDENTITIES);
    
                        var copy = new COPYDATASTRUCT(AGENT_COPYDATA_ID, mmFileName);
    
                        if (NativeMethods.SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, ref copy) == IntPtr.Zero)
                        {
                              yield break;
                        }
    
                        if (accessor.ReadByte(4) != SSH2_AGENT_IDENTITIES_ANSWER)
                        {
                              yield break;
                        }
    
                        int numberOfIdentities = IPAddress.HostToNetworkOrder(accessor.ReadInt32(5));
                        int position = 9;
                         
                        for (int i = 0; i < numberOfIdentities; i++)
                        {
                            int blobSize = IPAddress.HostToNetworkOrder(accessor.ReadInt32(position));
                            position += 4;
    
                            var blob = new byte[blobSize];
    
                            accessor.ReadArray(position, blob, 0, blobSize);
                            position += blobSize;
                            int commnetLenght = IPAddress.HostToNetworkOrder(accessor.ReadInt32(position));
                            position += 4;
                            var commentChars = new byte[commnetLenght];
                            accessor.ReadArray(position, commentChars, 0, commnetLenght);
                            position += commnetLenght;
    
                            string comment = Encoding.ASCII.GetString(commentChars);
                            string type = Encoding.ASCII.GetString(blob, 4, 7);// needs more testing kind of hack
                            
                             
                            yield return new IdentityReference(type, blob, comment);
    
                        }
    
                    }
                }
            }
    
            byte[] IAgentProtocol.SignData(IdentityReference identity, byte[] data)
            {
                var hWnd = NativeMethods.FindWindow("Pageant", "Pageant");
    
                if (hWnd == IntPtr.Zero)
                {
                    return new byte[0];
                }
    
                string mmFileName = Path.GetRandomFileName();
    
                using (var mmFile = MemoryMappedFile.CreateNew(mmFileName, AGENT_MAX_MSGLEN))
                {
                    using (var accessor = mmFile.CreateViewAccessor())
                    {
                        var security = mmFile.GetAccessControl();
                        security.SetOwner(System.Security.Principal.WindowsIdentity.GetCurrent().User);
                        mmFile.SetAccessControl(security);
    
                        accessor.Write(0, IPAddress.NetworkToHostOrder(AGENT_MAX_MSGLEN - 4));
                        accessor.Write(4, SSH2_AGENTC_SIGN_REQUEST);
                        accessor.Write(5, IPAddress.NetworkToHostOrder(identity.Blob.Length));
                        accessor.WriteArray(9, identity.Blob, 0, identity.Blob.Length);
                        accessor.Write(9 + identity.Blob.Length, IPAddress.NetworkToHostOrder(data.Length));
                        accessor.WriteArray(13 + identity.Blob.Length, data, 0, data.Length);
    
    
    
                        var copy = new COPYDATASTRUCT(AGENT_COPYDATA_ID, mmFileName);
    
                        if (NativeMethods.SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, ref copy) == IntPtr.Zero)
                        {
                            return new byte[0];
                        }
    
                        if (accessor.ReadByte(4) != SSH2_AGENT_SIGN_RESPONSE)
                        {
                            return new byte[0];
                        }
    
                        int size = IPAddress.HostToNetworkOrder(accessor.ReadInt32(5));
                        var ret = new byte[size];
                        accessor.ReadArray(9, ret, 0, size);
                        return ret;
                    }
                }
            }
    
        }
    }

    How to call IEnumerable<IdentityReference> IAgentProtocol.GetIdentities() in static void Main(string[] args) ?

     
     
    Thursday, June 22, 2017 6:32 PM

All replies

  • User1068175894 posted

    my suggestion is to create a non static class "Application" that implements IAgentProtocol

    then in your Program instantiate that class, that way you will avoid all issues with the static program class and it will also make your application more portable.

            static void Main(string[] args)
            {
    var app = new Application(); }
    Thursday, June 22, 2017 7:07 PM
  • User512724938 posted

    No luck.

    I need help to call  IEnumerable<IdentityReference> IAgentProtocol.GetIdentities() in program.cs

    Thursday, June 22, 2017 7:14 PM
  • User1068175894 posted

    You have to cast app to IAgentProtocol, here is a working example:

        public interface IAgentProtocol
        {
            IEnumerable<IdentityReference> GetIdentities();
    
            byte[] SignData(IdentityReference identity, byte[] data);
        }
    
        public class Application : IAgentProtocol
        {
            #region  Constants
            //...
            #endregion
    
            IEnumerable<IdentityReference> IAgentProtocol.GetIdentities()
            {
                //...
                yield return new NTAccount("name");
            }
    
            byte[] IAgentProtocol.SignData(IdentityReference identity, byte[] data)
            {
                //...
                return new byte[]{};
            }
    
        }
    
        public class Program
        {
            static void Main(string[] args)
            {
                var app = new Application();
                ((IAgentProtocol)app).GetIdentities();
            }
        }

    Thursday, June 22, 2017 9:11 PM
  • User303363814 posted

    IAgentProtocol x = new Program();

    x.GetIdentities();

    Friday, June 23, 2017 7:32 AM