-
Notifications
You must be signed in to change notification settings - Fork 4
/
readcred.cpp
55 lines (46 loc) · 1.71 KB
/
readcred.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// readcred.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <windows.h>
#include <wincred.h>
int main(int argc, char *argv[])
{
if (2 > argc) {
std::cerr << "Usage: " << argv[0] << " <credential-name>\n";
return -1; // RETURN
}
int utf8Len = std::char_traits<char>::length(argv[1]);
int utf16Len = ::MultiByteToWideChar(CP_UTF8,
MB_ERR_INVALID_CHARS,
argv[1],
utf8Len,
0,
0);
if (0 == utf16Len) {
std::cerr << "Failed to encode credential name\n";
return -1; // RETURN
}
std::wstring utf16Target;
utf16Target.resize(utf16Len + 1);
::MultiByteToWideChar(CP_UTF8,
MB_ERR_INVALID_CHARS,
argv[1],
utf8Len,
&utf16Target[0],
utf16Target.length());
CREDENTIALW *credential;
if (FALSE == ::CredReadW(utf16Target.c_str(),
CRED_TYPE_GENERIC,
0,
&credential)) {
std::cerr << "Could not find credential\n";
return -1; // RETURN
}
std::copy_n(credential->CredentialBlob,
credential->CredentialBlobSize,
std::ostreambuf_iterator<char>(std::cout.rdbuf()));
::CredFree(credential);
return 0;
}