Windows Credential Manager

Share your advanced PureBasic knowledge/code with the community.
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Windows Credential Manager

Post by normeus »

Currently working on C# but my Visual studio was slowing down and the only way to fix it is restarting the computer which for me takes 1/2 hour.
While it was restarting I had time to translate this routine to PB.

PB 5.31 to 5.60 32 bit ONLY

Code: Select all

;Unicode & ansi write to credential manager
;just change sPwrd$, sUname$ and sDomain$
; Variable translation from c# to PB: http://purebasic.fr/english/viewtopic.php?p=151705#p151705
;

EnableExplicit

; THESE ARE THE VARIABLES YOU WANT TO CHANGE 
Global sPwrd$="OrangeSenility"
Global sUname$="POTUS"
Global sDomain$="back2USSR"
Global sComent$=""


#dllAdvapi32=256 ; any number

;CRED_TYPE
#CRED_TYPE_GENERIC = 1
#CRED_TYPE_DOMAIN_PASSWORD = 2
#CRED_TYPE_DOMAIN_CERTIFICATE = 3
#CRED_TYPE_DOMAIN_VISIBLE_PASSWORD = 4
;CRED_PERSIST
#CRED_PERSIST_SESSION = 1
#CRED_PERSIST_LOCAL_MACHINE = 2
#CRED_PERSIST_ENTERPRISE = 3
;CRED_
#CRED_PRESERVE_CREDENTIAL_BLOB = 1

Structure _CREDENTIAL_ATTRIBUTE    ;not really needed 
  Keyword.l ;LPTSTR
  Flags.l
  ValueSize.l
  Value.l ;LPBYTE
EndStructure


Structure _credential
  flags.l
  type.l
  targetName.l  ;LPTSTR 
  comment.l
  LastWritten.FILETIME
  credentialSize.l
  credentialBlob.l  ; LPBYTE
  persist.l
  attributeCount.l
  attributes.l ;pointer to _CREDENTIAL_ATTRIBUTE if needed    
  targetAlias.l
  UserName.l
EndStructure


Prototype CredWriteW( credential.l, flags.l = 0)
Prototype CredReadW( TargetName$, Type.l, Flags.l, *Credential)
If OpenLibrary(#dllAdvapi32, "advapi32.dll") 

  CompilerIf  #PB_Compiler_Unicode
    Define  CredWriteW_.CredWriteW = GetFunction(#dllAdvapi32, "CredWriteW")
    Define  CredReadW_.CredreadW = GetFunction(#dllAdvapi32, "CredReadW")  ; didn't have time to add CredReadW sample
  CompilerElse
    Define  CredWriteW_.CredWriteW = GetFunction(#dllAdvapi32, "CredWriteA")
    Define  CredReadW_.CredreadW = GetFunction(#dllAdvapi32, "CredReadA")  
  CompilerEndIf

  Define cred._credential
  With cred
    \flags=0
    \type=#CRED_TYPE_DOMAIN_PASSWORD
    \targetName=@sDomain$
    \UserName=@sUname$
    \comment=@sComent$
    \attributeCount=0
    \attributes=0
    \targetAlias=0
    \persist=#CRED_PERSIST_LOCAL_MACHINE
    \credentialSize=StringByteLength(sPwrd$)
    \credentialBlob=@sPwrd$
  EndWith
  If CredWriteW_(@cred)
    Debug "Created credentials for: " + sDomain$
  Else
      Debug "look up error code:"+ Str(GetLastError_())+ " @ https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx"   
 EndIf
EndIf
I owe you guys the example for reading credentials because my computer finished restarting.
Hope some one finds a use for this.
Latest Symantec is killing my PB stuff so now I disable the antivirus for the period of time I am trying to compile something in PB.

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari