예제 #1
0
        public object MarshalNativeToManaged(IntPtr pNativeData)
        {
            if (pNativeData == IntPtr.Zero)
            {
                return(null);
            }

            CREDENTIALW nativeCredential = (CREDENTIALW)Marshal.PtrToStructure(pNativeData, typeof(CREDENTIALW));

            Credential credential = new Credential
            {
                UserName       = nativeCredential.UserName,
                TargetName     = nativeCredential.TargetName,
                TargetAlias    = nativeCredential.TargetAlias,
                Persist        = nativeCredential.Persist,
                Comment        = nativeCredential.Comment,
                Flags          = nativeCredential.Flags,
                LastWritten    = nativeCredential.LastWritten,
                Type           = nativeCredential.Type,
                CredentialBlob = new byte[nativeCredential.CredentialBlobSize],
                Attributes     = new CredentialAttribute[nativeCredential.AttributeCount]
            };

            Marshal.Copy(nativeCredential.CredentialBlob, credential.CredentialBlob, 0, (int)nativeCredential.CredentialBlobSize);

            for (int i = 0; i < nativeCredential.AttributeCount; ++i)
            {
                IntPtr ptrAttribute = IntPtr.Add(nativeCredential.Attributes, Marshal.SizeOf <CredentialAttribute>());
                Marshal.PtrToStructure(ptrAttribute, credential.Attributes[i]);
            }

            return(credential);
        }
예제 #2
0
        public IntPtr MarshalManagedToNative(object obj)
        {
            if (obj == null)
            {
                return(IntPtr.Zero);
            }

            if (obj is Credential credential)
            {
                int sizeofCredentialStruct     = Marshal.SizeOf <CREDENTIALW>();
                int sizeofCredentialBlob       = (credential.CredentialBlob?.Length ?? 0) * sizeof(byte);
                int sizeofCredentialAttributes = (credential.Attributes?.Length ?? 0) * Marshal.SizeOf <CredentialAttribute>();

                ptrAllocated = Marshal.AllocHGlobal(sizeofCredentialStruct + sizeofCredentialBlob + sizeofCredentialAttributes);

                IntPtr ptrCredentialStruct     = ptrAllocated;
                IntPtr ptrCredentialBlob       = IntPtr.Add(ptrCredentialStruct, sizeofCredentialStruct);
                IntPtr ptrCredentialAttributes = IntPtr.Add(ptrCredentialBlob, sizeofCredentialBlob);

                CREDENTIALW nativeCredential = new CREDENTIALW
                {
                    UserName           = credential.UserName,
                    TargetName         = credential.TargetName,
                    TargetAlias        = credential.TargetAlias,
                    Persist            = credential.Persist,
                    Comment            = credential.Comment,
                    Flags              = credential.Flags,
                    LastWritten        = credential.LastWritten,
                    Type               = credential.Type,
                    CredentialBlobSize = (uint)(credential.CredentialBlob?.Length ?? 0),
                    CredentialBlob     = ptrCredentialBlob,
                    AttributeCount     = (uint)(credential.Attributes?.Length ?? 0),
                    Attributes         = ptrCredentialAttributes
                };

                Marshal.StructureToPtr(nativeCredential, ptrCredentialStruct, false);
                Marshal.Copy(credential.CredentialBlob, 0, ptrCredentialBlob, sizeofCredentialBlob);

                for (int i = 0; i < nativeCredential.AttributeCount; ++i)
                {
                    IntPtr ptrAttribute = IntPtr.Add(nativeCredential.Attributes, Marshal.SizeOf <CredentialAttribute>());
                    Marshal.StructureToPtr(credential.Attributes[i], ptrAttribute, false);
                }

                return(ptrCredentialStruct);
            }

            throw new ArgumentException("Argument is not valid Credential object.");
        }