[system.reflection.Assembly]::LoadFrom("dll所在绝对路径");
但是传统的非.NET生成的Dll库如何使用呢?
比较复杂,基本可以按照下列语句来使用:
$dir="C:\\curl-7.16.4\\FTCTRLPRO3.dll"
$ctor = [Runtime.InteropServices.DllImportAttribute].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor, $dir
这只能简单载入Dll,如果想使用Dll的具体的函数,可以参看如下文章:
http://www.leeholmes.com/blog/ManagingINIFilesWithPowerShell.aspx
##############################
##
## Invoke-WindowsApi.ps1
##
## From PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
## Invoke a native Windows API
##
## ie:
##
## ## Prepare the parameter
## CreateHardLink function
## $parameterTypes = [string],
## $parameters = [string]
##
## ## Call the CreateHardLink
## $result = Invoke-WindowsApi
## $parameterTypes
##
##############################
param(
[string] $dllName,
[Type] $returnType,
[string] $methodName,
[ Type[]] $parameterTypes,
[Object[]] $parameters
)
## Begin to build the dynamic
$domain = [AppDomain]::CurrentDomain
$name = New-Object Reflection.AssemblyName 'PInvokeAssembly'
$assembly = $domain.DefineDynamicAssembly( $name, 'Run')
- Show quoted text -
$module = $assembly.DefineDynamicModule( 'PInvokeModule')$type = $module.DefineType('PInvokeType', "Public,BeforeFieldInit")
## Go through all of the
## we clone the user's inputs
## the P/Invoke call.
$inputParameters = @()
$refParameters = @()
for($counter = 1; $counter -le $parameterTypes.Length; $counter++)
{
## If an item is a PSReference
## wants an [out] parameter.
if($parameterTypes[$counter - 1] -eq [Ref])
{
## Remember which parameters
$refParameters += $counter
## On the cloned array, we
## .Net reference type that
## and the value with the
$parameterTypes[ $counter - 1] =
$parameters[$counter - 1 ].Value.GetType().MakeByRefType
$inputParameters += $parameters[$counter - 1].Value
}
else
{
## Otherwise, just add their
## input array.
$inputParameters += $parameters[$counter - 1]
}
}
## Define the actual P/Invoke
## attribute for any parameters
## parameters.
$method = $type.DefineMethod( $methodName, 'Public,HideBySig,Static
$returnType, $parameterTypes )
foreach($refParameter in $refParameters)
{
}
## Apply the P/Invoke construct
$ctor = [Runtime.InteropServices.DllImportAttribute ].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttribute
$method.SetCustomAttribute($attr)
## Create the temporary type,
$realType = $type.CreateType()
$inputParameters)
## Finally, go through all of
## values of the PSReference
foreach($refParameter in $refParameters)
{
$parameters[$refParameter - 1].Value = $inputParameters[$refParameter - 1]
}