Sunday, April 18, 2010

Is Silverlight 4 able to call Win32 API functions when running Out of Browser (OOB)?

With the new relaxed security model of Silverlight 4, I was curious if  would finally be possible for Silverlight to directly call Win32 API functions, particularly when running out of browser.  You could argue that it shouldn’t, as an application with such capabilities could certainly invoke unwanted behavior.  But regardless of whether it would be a good thing or not, I was still curious if it could.

After much testing it appears the answer is no. I get an error like the following when attempting to access various Win32 API functions out of browser, even after signing the assembly.

“Attempt by security transparent method 'Button1Click' to call native code through method 'Win32APIMethod' failed. Methods must be security critical or security safe-critical to call native code.”

It may be possible to use the new COM Interop capability to access these functions indirectly, but I have not tested that.

If you have any more information on this, or can show it working by providing an example, I would greatly appreciate your comments.

Hope this helps.

Joe Kunk
Microsoft MVP Visual Basic
Okemos, MI USA

1 comment:

levenshe said...

I found there is a win32 declare for visual basic, during I development visual basic Application, I always refer that, You can also refer that too. the address is: