Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 376181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:34:27+00:00 2026-05-12T14:34:27+00:00

I’m trying to translate this c++/cli code to c# #pragma once #define WIN32_LEAN_AND_MEAN #include

  • 0

I’m trying to translate this c++/cli code to c#

#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

   using namespace System;
   using namespace System::Runtime::InteropServices;

    struct CREDENTIAL    
    {
        DWORD Flags;
        DWORD Type;
        LPSTR TargetName;
        LPSTR Comment;
        Runtime::InteropServices::ComTypes::FILETIME LastWritten;        
        DWORD CredentialBlobSize;
        LPBYTE CredentialBlob;
        DWORD Persist;
        DWORD AttributeCount;
        LPBYTE Attributes;
        LPSTR TargetAlias;
        LPWSTR UserName;
    };

    [DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet::Unicode)]
    extern BOOL CredEnumerate(LPCTSTR Filter, DWORD Flags, DWORD* count, CREDENTIAL*** Credentials);

    [DllImport("advapi32.dll")]
    extern VOID CredFree(LPVOID);

        int main( array<System::String^>^argv )
        {
            String^ credentialList = "";
            Int32 count = 0;
            CREDENTIAL** credentialCollection = 0;            

            if( CredEnumerate( _T("WindowsLive:name=*"), 0, (DWORD*)&count, &credentialCollection) )                        
            {
                for( Int32 n = 0; n < count; n++ )
                {
                    credentialList += "\n";
                    credentialList += "Username " + gcnew System::String( credentialCollection[n]->UserName ) + "\n";
                    credentialList += "Password: " + gcnew System::String( (LPWSTR)credentialCollection[n]->CredentialBlob ) + "\n";
                } 

                CredFree( &credentialCollection );
        Console::WriteLine(credentialList);

            }
        Console::ReadLine();
        }

Here’s my c# code .. (I am not sure how CredEnumerate does work)

using System;
using System.Runtime.InteropServices;     // DLL support
using System.Collections.Generic;

public struct CREDENTIAL
{
    public UInt32 flags;
    public UInt32 type;
    public string targetName;
    public string comment;
    public System.Runtime.InteropServices.ComTypes.FILETIME lastWritten;
    public UInt32 credentialBlobSize;
    public IntPtr credentialBlob;
    public UInt32 persist;
    public UInt32 attributeCount;
    public IntPtr credAttribute;
    public string targetAlias;
    public string userName;
}

class Test
{        
    [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern bool CredEnumerate(string filter, int flag, out int count, out IntPtr
    pCredentials);

    static void Main()
    {
        try
        {
            int count = 0;
            IntPtr pCredentials = IntPtr.Zero;
            IntPtr[] credentials = null;
            bool ret = CredEnumerate("WindowsLive:name=*", 0, out count, out pCredentials);
            if (ret != false)
            {

                credentials = new IntPtr[count];
                IntPtr p = pCredentials;
                for (int n = 0; n < count; n++)
                {
                    if (Marshal.SizeOf(p) == 4) //32 bit CLR?
                        p = new IntPtr(p.ToInt32() + n);
                    else
                        p = new IntPtr(p.ToInt64() + n);
                    credentials[n]  = Marshal.ReadIntPtr(p);
                }

                List<CREDENTIAL> creds = new List<CREDENTIAL>(credentials.Length);
                foreach (var ptr in credentials)
                {
                    creds.Add((CREDENTIAL)Marshal.PtrToStructure(ptr, typeof(CREDENTIAL)));
                }

            }
        }
        catch (Exception x)
        {
            Console.WriteLine(x.ToString());
        }

        Console.ReadLine();
    }
}

I hope someone can help me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:34:27+00:00Added an answer on May 12, 2026 at 2:34 pm

    It’s pretty scary code, I have my doubts about the CLI/C++ being right. You should look up the definitions of your structure e.g.

    http://msdn.microsoft.com/en-us/library/aa374788(VS.85).aspx

    Then clean-up the code properly before posting.

    I would be tempted to use the code from this article on credential management

    Still, that involves keeping the CLI/C++ which you seem to want to get rid of so I gave fixing it a go.

    Now this works on my machine, that I’ve hacked it together… (note: I’ve removed your filter so I could test it)

    [StructLayout(LayoutKind.Sequential)]
    internal struct CREDENTIAL {
        public int Flags;
        public int Type;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string TargetName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string Comment;
        public long LastWritten;
        public int CredentialBlobSize;
        public IntPtr CredentialBlob;
        public int Persist;
        public int AttributeCount;
        public IntPtr Attributes;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string TargetAlias;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string UserName;
    }
    

    Then the reading

    try
    {
        uint count;
        IntPtr pCredentials = IntPtr.Zero;
        IntPtr[] credentials;
        bool ret = CredEnumerateW("*", 0, out count, out pCredentials);
        if (ret)
        {
            credentials = new IntPtr[count];
            List<CREDENTIAL> creds = new List<CREDENTIAL>(credentials.Length);
            for(int i = 0; i < count; i++) {
                IntPtr structs= Marshal.ReadIntPtr(pCredentials, IntPtr.Size * i);
                CREDENTIAL c = (CREDENTIAL)Marshal.PtrToStructure(structs, typeof(CREDENTIAL));
                creds.Add(c);
            }
        }
    }
    catch (Exception x)
    {
        Console.WriteLine(x.ToString());
    }
    

    and

    [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern bool CredEnumerateW(string filter, uint flag, out uint count, out IntPtr pCredentials);
    

    It doesn’t free the structure etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 177k
  • Answers 177k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer my $output = qx(powercfg -l); ## You've got your output… May 12, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer In general, empty classes are a code smell. I agree… May 12, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer All existing Java APIs try to build the whole document… May 12, 2026 at 3:25 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.