1

I am running into a wall with the whole p/invoke issue. I need to do this programmatically in C#.

mzachen
  • 89
  • 1
  • 1
  • 4
  • Ive tried using the LSA wrapper floating around out there, can be seen in the following 2 links: http://stackoverflow.com/questions/1286795/c-how-to-programmatically-grant-user-log-on-as-a-service http://www.codeproject.com/Articles/4863/LSA-Functions-Privileges-and-Impersonation I have also tried the method listed in the next link. It will show users who belong to each group policy, but this doesn't help me if the user is a member of a group with LSA, because the user wouldnt be listed. http://david-homer.blogspot.com/2012/03/audit-and-document-security-user-rights.html – mzachen May 10 '12 at 18:18
  • Sorry, let me know if the last comment helps... – mzachen May 10 '12 at 18:21

1 Answers1

0

I did the p/invoke stuff (with the LSA wrapper) a couple of weeks ago. I got an exception the first time I run the wrapper, but it succeeded the second time on Win2k8 server 64 bit.

Something like this worked for me

try{
   DoThePInvokeMagic();
}
catch(Exception){
    // Sometimes the first invocation fails, but the second time it seems to work       
    DoThePInvokeMagic();
}

Unfortunately we did not have time/prio to investigate further why this happened and why it worked the second time, since we got it working on all our servers.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • Wow. You're right, it fails on first try and passes on second. I am grateful for that tip. I will spend some time trying to figure out why and hopefully get back to you. – mzachen May 10 '12 at 19:46
  • One thing I did different is avoided the recursion and added an error counter. Im thinking that if there was some other error in the LSA/PInvoke code you would hit an endless loop. Check this out: `catch(Exception ex) { errorCount = errorCount + 1; if (errorCount > 1) { MessageBox.Show(ex.Message); } else { goto restart; } } ` – mzachen May 10 '12 at 19:52