/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" [MSH] keyboard status "," playing with MY-object from MSH "," MSH access NT provider "," Update-TypeData (Democracy to the types) "," MSH directory watcher with popup-balloon "," StartRDP script "," convert images in MSH "," Style Problems "," ColorCoding MSH scripts on blog "," GUI folder copy tool "," ")

Sunday, October 23, 2005

 


MSH Clipboard use workaround



while playing with the MY object in a former post,
I noticed that the MSH will exit if called from MSH.

I posted this on the newsgroup, and got the following aswer :

Sorry - this is a known problem. Execution threads in the monad engine are
created MTA and you can't create an object that requires STA from an MTA
thread. We have workaround code in the new-object cmdlet that let's you
create an STA COM object when you create the object explicitly. Unfortunately
this doesn't work for objects that are created implicitly by a .NET object.
We instantiate the .NET object and some time later it instantiates the COM
object. We can't use the same workaround because we don't get the exception
until the first time the object is used instead of when it's created. Anyway
- we're still working on this and will hopefully have a solution soon.
-bruce
--
Bruce Payette
Monad Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


I have found the following workaround for it :

gr /\/\o\/\/

PS, it's more a POC (proof-of-Concept), it will only write the line "Hello Clip" (hard-coded),
You need to make it a function that takes arguments, or better yet a CMDlet. to make it realy usefull ;-)
cls
$provider = new-object Microsoft.VisualBasic.VBCodeProvider
$params = new-object System.CodeDom.Compiler.CompilerParameters
$params.GenerateInMemory = $True
$refs = "System.dll","Microsoft.VisualBasic.dll","System.Data.DLL","System.management.dll","System.DirectoryServices.dll"
$params.ReferencedAssemblies.AddRange($refs)

# VB.NET EXAMPLE
$txtCode = @'

Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class mow

Sub Main()

Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
newThread.ApartmentState = ApartmentState.STA
newThread.Start()
End Sub

Shared Sub ThreadMethod()
dim Comp as new Microsoft.VisualBasic.Devices.computer
comp.clipboard.setText("hello Clip")
End Sub

End Class


'@

$results = $provider.CompileAssemblyFromSource($params, $txtCode)
$mAssembly = $results.CompiledAssembly
$i = $mAssembly.CreateInstance("mow")
$r = $i.main()



Comments:
Anonymous Anonymous
I am new here, just saying hello :)

<3 sagepowder
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?