0

I am facing a issue while unprotecting the VBA Project of the protected excel.

IntPtr hWnd = FindWindow(null, ES.oXL.VBE.MainWindow.Caption);//Find window
  if (hWnd != IntPtr.Zero)
  {
      bool ret = BringWindowToTop(hWnd); //Bring VBE to top.
  }

  SendKeys.SendWait("%{F11}%TE" + strPassword + "~~%{F11}");
  SendKeys.SendWait("{ENTER}");
  SendKeys.SendWait("xyz");
  SendKeys.SendWait("{ENTER}");

I Wrote a code like this to unprotect the VBA Project. It works fine when debugging application.

We use com object model. when i applied the same dll in the dll hive, call the function i am getting below error

System.ComponentModel.Win32Exception: Access is denied at System.Windows.Forms.SendKeys.SendInput(Byte[] oldKeyboardState, Queue previousEvents)

Any help on this why its working?

Community
  • 1
  • 1

1 Answers1

1

What kind of environment did you deploy into?

It seems like the assembly is not running in a full trust security context which is why certain API calls are not a allowed

From the documentation:

The call to the Shell function requires full trust (SecurityException class).

UPDATE

Based on the OP's comments it appears that indeed the assembly being hosted in a COM+ container is not granted FullTrust.

In order to work around this you might consider deploying a separate assembly into the GAC on the target machine with FullTrust and enable the AllowPartiallyTrustedCallers option on this assembly. This assembly would have to be the one making the calls to SendKeys on your other COM+ hosted assembly.

But in the end, as others have pointed out this may not be the right solution. In general SendKeys should be used very sparingly and there are probably other better ways to accomplish your goals..

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151