none
Interop class using a different user RRS feed

  • Question

  • Hi,

    I have a question.. Here is the code i am currently working on...

    Line 1: MessageBox.Show(System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString()); 
    Line 2: using ( new Impersonator( "testAdmin", "", "testAdmin" ) )
    Line 3: {
    Line 4: word.Application wd = null; wd = new word.Application();
    Line 5: MessageBox.Show(System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString());
    Line 6: }

    The impersonator class works perfectly fine.. when i do a GetCurrent(), i get a different values at Line 1 and Line 5. However, when i try to create a winword.exe process at Line 4, the winword.exe was not created using the impersonated user.. Is there any other way to accomplish this task ?

    -Rupesh

    Thursday, April 21, 2011 10:59 PM

Answers

All replies

  • Hi Rupesh,

    After reading your post, I am not very clear about the Line2 code.

    Could you tell me where does the Impersonator come from? Is it the in namespace:Microsoft.Imm.Sdk.SharePoint under the SharePoint SDK  http://msdn.microsoft.com/en-us/library/microsoft.imm.sdk.sharepoint.impersonator.aspx or you write yourself? 

    If it's your own class, could you show me the code snippet? I will do further research with your detailed informaiton.

    Best Regards, 


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 22, 2011 9:35 AM
  • Hi Rupesh,

    With your code, it uses the current user to start the word application. I think we need to use Process class to start the word application, below is the code snippet:

     

                ProcessStartInfo psinfo = new ProcessStartInfo("notepad");

                psinfo.UseShellExecute = false;

                psinfo.UserName = "testAdmin";

                char[] cs = "testAdmin".ToCharArray(); 

                SecureString ss = new SecureString();

                foreach(char c in cs)

                {

                    ss.AppendChar(c);

                }

                psinfo.Password = ss;

                Process proc = new Process();

                proc.StartInfo = psinfo;

                proc.Start();


    It can start the word process of testAdmin account. I hope this can help you and just feel free to follow up.

    Best Regards,

     


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 25, 2011 10:09 AM
  • Thanks Bruce!

    I don't think I am able to arrive at a suitable solution using the code above. Is there any better way of Impersonating Word.application in c# ??

     

    -Rupesh

    Monday, April 25, 2011 5:29 PM
  • Bruce,

    I came across this post. I am able to create a new winword.exe process using new user. (not the current user). 
    How do I associate this object to "wd" @ Line 4.

    Post: http://stackoverflow.com/questions/480358/run-new-process-as-different-user-in-vb-net

    I've tried the following:

     

    ImpersonationDemo.RunProgram("testAdmin", "testAdmin", "", "C:\\Program Files (x86)\\Microsoft Office\\Office12\\winword.exe", "");
       using ( new Impersonator( "testBench", "", "hasdfgh" ) )
       {
        word.Application wd = null;
        wd = (word.Application)GetObj("Word.Application");
        wd.DocumentBeforeClose += new word.ApplicationEvents4_DocumentBeforeCloseEventHandler(wd_DocumentBeforeClose);
       }

     

    ImpersonationDemo class creates a new winword.exe and I am able to get the PID. I am unable to associate this to "wd".

     

    -Rupesh


    Monday, April 25, 2011 8:53 PM
  • Hi Rupesh,
    I think it not possible to get the word application and associate with wd. I have tried using the following code snippet:
          word.Application wd = null;
          wd = (word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
          MessageBox.Show(wd.UserName);
    
    However, it throws the COM exception: Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) while the code can work well if the process is not the impersonated user. I think the reason may be this:
    In addition,Please take a look at this KB article about automating a running instance of Office program: http://support.microsoft.com/kb/316126 . You can try the methods the article metioned to see whether it works well.
     
    Hope this helps.
    Best Regards,

    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Bruce Song Thursday, May 5, 2011 9:48 AM
    • Marked as answer by Bruce Song Monday, May 9, 2011 9:01 AM
    Tuesday, April 26, 2011 11:37 AM
  • Hi Rupesh,

    Have you figured out your problem yet, and does the suggestion help you? If you still have any concern on the thread, feel free to follow up. 

    Best Regards,



    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 29, 2011 6:04 AM