如何使用Go检索本地计算机Windows证书存储的列表?

Go allows you to pretty easily retrieve the Personal Windows cert store handle for the Current User using the following command:

store, err := syscall.CertOpenSystemStore(0, syscall.StringToUTF16Ptr("MY")) // Personal Certificates

However, when I try to retrieve the Local Machine Personal cert store I always end up with either an empty store handle, and exception or error that the store was not found, CRYPT_E_NOT_FOUND.

Alternatively, you can use this method:

    store, err := syscall.CertOpenStore(
       windows.CERT_STORE_PROV_SYSTEM, 
       0,
       0,
       windows.CERT_SYSTEM_STORE_CURRENT_USER,
       uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("MY")))) 

But when I swap out the values for the Local Machine store I start seeing the errors referenced above.

How do you successfully retrieve the Local Machine cert store in Windows using Go?

You do use the following code snippet as mentioned in the comments above to retrieve the certs:

    store, err := syscall.CertOpenStore(
       windows.CERT_STORE_PROV_SYSTEM,
       0,
       0,
       windows.CERT_SYSTEM_STORE_LOCAL_MACHINE, 
       uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("MY"))))

But there were a few things going on here as to why specifically it wasn't working initially:

  1. You must run the program as Administrator otherwise the store will be returned as 0.
  2. I was not running VS Code as Administrator: I thought I was, but you have to kill all instances in order to do so!
  3. The code to retrieve the last error didn't seem to work in this situation in Go but when I switched over to working in C++ I did see the access denied error.