I am wondering if there is something like [DllImport(...)] equivalent
in MSH.
Not automatic. You can always drum up some C# solution that does on-the-fly
compilation and execution for such a thing.
ActiveX is--or was and will be--supported, though.
Jon
this MSH script one does it in Visual Basic.NET but you can switch the
compiler for a C# version :
$provider = new-object Microsoft.VisualBasic.VBCodeProvider
$params = new-object System.CodeDom.Compiler.CompilerParameters
$params.GenerateInMemory = $True
$refs = "System.dll","Microsoft.VisualBasic.dll"
$params.ReferencedAssemblies.AddRange($refs)
# VB.NET EXAMPLE
$txtCode = @'
Class mow
Declare Auto Function MyMessageBox Lib “user32.dll” Alias _
“MessageBox” (ByVal hWnd as Integer, ByVal msg as String, _
ByVal Caption as String, ByVal Tpe as Integer) As Integer
Sub Main()
MyMessageBox(0, "Hello World !!!", "Project Title", 0)
End Sub
end class
'@
$results = $provider.CompileAssemblyFromSource($params, $txtCode)
$mAssembly = $results.CompiledAssembly
$i = $mAssembly.CreateInstance("mow")
$r = $i.main()
gr /\/\o\/\/
#
# Build up a compiler params object...
#
${framework}\System.Data.dll,${framework}\System.dll,${framework}\system.xml.dll"
$framework = Combine-Path $env:windir
"Microsoft.NET\Framework\$FrameWorkVersion"
$refs = new Collections.ArrayList
$refs.AddRange( @("${framework}\System.dll",
"${mshhome}\System.Management.Automation.dll",
"${mshhome}\System.Management.Automation.ConsoleHost.dll",
"${framework}\system.windows.forms.dll",
"${framework}\System.data.dll",
"${framework}\System.Drawing.dll",
"${framework}\System.Xml.dll"))
if ($references.Count -ge 1)
{
$refs.AddRange($References)
}
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $false
$cpar.OutputAssembly = "custom"
$cpar.ReferencedAssemblies.AddRange($refs)
$cr = $cp.CompileAssemblyFromSource($cpar, $code)
if ( $cr.Errors.Count)
{
$codeLines = $code.Split("`n");
foreach ($ce in $cr.Errors)
{
write-host "Error: $($codeLines[$($ce.Line - 1)])"
$ce |out-default
}
Throw "INVALID DATA: Errors encountered while compiling code"
}
}
#########################################################
# Here I leverage one of my favorite features (here-strings) to define
# the C# code I want to run. Remember - if you use single quotes - the
# string is taken literally but if you use double-quotes, we'll do variable
# expansion. This can be VERY useful.
#########################################################
$code = @'
using System;
using System.Runtime.InteropServices;
namespace test
{
public class Testclass
{
[DllImport("msvcrt.dll")]
public static extern int puts(string c);
[DllImport("msvcrt.dll")]
internal static extern int _flushall();
public static void Run(string message)
{
puts(message);
_flushall();
}
}
}
'@
########################################################
# So now we compile the code and use .NET object access to run it.
########################################################
compile-CSharp $code
[Test.TestClass]::Run("Monad ROCKS!")
--
Jeffrey Snover [MSFT]
Monad Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
"DontBotherMeWithSpam" <DontBother...@gmail.com> wrote in message
news:1133720493.1...@f14g2000cwb.googlegroups.com...
that last line is to much,
and throw an error as $framework is not yet declared, it gets declared
below, and also there are these refs again.
if you delete it it works as expected, pasting error I presume.
thanks for the addition to my toolbox, it's good for the "glide-path"
gr /\/\o\/\/
PS if you change this line,
$cpar.GenerateInMemory = $False
you can use it to make CMDlets
make-shell -out mowSH -namespace mow -reference ShowFileDialog.dll
mowSH
This has been great of a help. I guess now this opens up a new
area(Win32API) I could explore with Msh :)
You might want to try VS 2005 C# express.
http://msdn.microsoft.com/vstudio/express/visualcsharp/download/
that will help a Lot ;-)
and its free !!
if you want to distribute, you can past it in the MSH wrapper.
Can you do?
file test.txt:
@"Hello $name"@
MSH >$name = "monad"
MSH >gc test.txt
Here-strings are only usable in MSH script files:
test.msh:
$name = "monad"
$msg1 = @"
Hello
$name
"@
$msg2 = @'
Hello
$name
'@
$msg1
$msg2
MSH> .\test.msh
Hello
monad
Hello
$name
you can also run any command by putting in $() e.g.
@"
Top 10 Handlecounts:
$(get-process |sort handlecount |select -last 10|out-string)
"@
Here strings are awesome tools for creating XML/HTML/any files.
--
Jeffrey Snover [MSFT]
Monad Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
<anonymous> wrote in message
news:%23Y0wo3a%23FHA...@TK2MSFTNGP14.phx.gbl...
> Here strings are awesome tools for creating XML/HTML/any files.
I agree. These are very handy. Although, just out of curiousity, what's
with the name "here string"? From a "glidescope to C#" point of view,
aren't these very similar to C# verbatim strings sans the issue of optional
variable expansion? Which also begs the question, why the funky syntax?
What not just use:
MSH > $s1 = @"Some string with an expanded var $PID"
MSH > $s2 = @'Some string with an unexpanded var $PID and a
>> newline with some more text'
>>
MSH >
If I want a simple string just to prevent escaping, I have to do this over
three lines of text with MSH:
MSH $s1 = @"
>> Some string with an expanded var $PID
>> "@
>>
BTW, why aren't the escapes `r`n etc escaped in "here" strings? I guess
this might explain why "here" strings are not the same as C# verbatim
strings?
--
Keith
I think Jouko posted an Emit example some time ago
(was to much for me, so I went the Compile way ;-) )
> Anyway, I needed to do this for one of my scripts so I came up with the
> attached script. It creates a scriptblock that calls a win32 api. For
> example:
>
WOW cool.
thx.
now I
can type anything
until I encounter an EOF
by itself
$
--
Jeffrey Snover [MSFT]
Monad Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
"Keith Hill" <r_keit...@no.spam.thank.u.hotmail.com> wrote in message
news:%23XjgI2b%23FHA...@TK2MSFTNGP15.phx.gbl...