Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Does MSH support P/Invoke?

45 views
Skip to first unread message

hayate

unread,
Oct 8, 2005, 5:57:50 AM10/8/05
to
As Msh is based on dot net,does it support platform invoke?
Or maybe there is some way special to attend that.

hah...:P

Jouko Kynsijärvi

unread,
Oct 8, 2005, 10:12:39 AM10/8/05
to
hayate wrote:
> As Msh is based on dot net,does it support platform invoke?
> Or maybe there is some way special to attend that.

MSH doesn't directly support P/Invoke. The easiest way to use P/Invoke from
MSH is probably to create a DLL in C#/VB/etc and call that from MSH.

But if you really want to use P/Invoke from MSH, it is possible with
Reflection.Emit. But note that the dynamic assembly created can't be
unloaded (unless you create it in a new AppDomain and unload that).

Here is an example that calls the user32.dll MessageBox API:

$domain = [AppDomain]::CurrentDomain
$name = New-Object Reflection.AssemblyName 'TestAssembly'
$assembly = $domain.DefineDynamicAssembly($name, 'Run')
$module = $assembly.DefineDynamicModule('TestModule')
$type = $module.DefineType('TestType')

[Type[]]$parameterTypes = [IntPtr], [string], [string], [int]
$method = $type.DefineMethod("MessageBox", 'Public,Static,PinvokeImpl',
[int], $parameterTypes)

$ctor =
[Runtime.InteropServices.DllImportAttribute].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor, 'user32'
$method.SetCustomAttribute($attr)

$realType = $type.CreateType()
[object[]]$args = [IntPtr]0, [string]'Bar', [string]'Foo', [int]0
$realType.InvokeMember('MessageBox', 'Public,Static,InvokeMethod', $null,
$null, $args)

(A less verbose way would probably be to generate C#/VB code, compile that
with csc.exe/vbc.exe and load the assembly.)


hayate

unread,
Oct 9, 2005, 12:50:52 AM10/9/05
to
thanks It's helpful

hayate

unread,
Oct 11, 2005, 10:28:54 PM10/11/05
to
I'm sorry I have a new problem
I have tried to create a new AppDomain,but it can't Define
DynamicAssembly in the new AppDomain directly.I have also tried
delegate the method,but it seems like no delegate in MSH.

Jeff Jones [MSFT]

unread,
Oct 12, 2005, 11:48:28 AM10/12/05
to
Can you give us an example of the code you are trying that isn't working?

BTW, you can cast ScriptBlocks to delegates.

--
Jeff Jones [MSFT]
Microsoft Command Shell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"hayate" <haya...@gmail.com> wrote in message
news:1129084134.0...@g47g2000cwa.googlegroups.com...

hayate

unread,
Oct 12, 2005, 9:04:54 PM10/12/05
to
ok
$Script = {

$domain = [AppDomain]::CurrentDomain
$name = New-Object Reflection.AssemblyName 'TestAssembly'
$assembly = $domain.DefineDynamicAssembly($name, 'Run')
$module = $assembly.DefineDynamicModule('TestModule')
$type = $module.DefineType('TestType')
[Type[]]$parameterTypes = [IntPtr], [string], [string], [int]
$method = $type.DefineMethod("MessageBox", 'Public,Static,PinvokeImpl',
[int], $parameterTypes)
$ctor =
[Runtime.InteropServices.DllImportAttribute].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor,
'user32'
$method.SetCustomAttribute($attr)
$realType = $type.CreateType()
[object[]]$args = [IntPtr]0, [string]'Bar', [string]'Foo', [int]0
$realType.InvokeMember('MessageBox', 'Public,Static,InvokeMethod',
$null,
$null, $args)
} # this the copy of the above

$ad = [AppDomain]::CreateAppdomain('NewDomain')
$delegate = New-Object CrossAppDomainDelegate($Script) # maybe it's
wrong,it can't execute here
$ad.DoCallBack($delegate)
[AppDomain]::Unload($ad)

that's all. I don't how to cast a scriptblock to delegate,so I have
tried some ways.

0 new messages