What prompted this question:
I’m trying to change the account under which a windows service runs. I decided to use the win32 api rather than WMI and started looking at ChangeServiceConfig.
I thought I could simply use the SecureString type in the unmanaged method signature and it would work fine. Hmmm, not quite. This made me wonder, does anyone know what native (win32) type the SecureString class is marshalled as (by default) to unmanaged code?
I never got an answer to the question in the title, so I still don’t know SecureString is marshalled to unmanaged code.
I did, however, get my code working by using the suggestions in some of the other answers. I have provided the code below.
Instead of Marshal.SecureStringToBSTR I used Marshal.SecureStringToGlobalAllocUnicode because thats’s what the unmanaged function expects, but other than that it works a treat.
Hopefully someone else finds this useful.
Kep.