I'm trying to call a function from a plain old DLL (no COM or .NET)
which I can do by compiling some in-line c# code in a 'here string'
and then instantaiting the c# code. Works well so long as the dll I'm
trying to call is in the path, for e.g. c:\windows\system32, but
doesn't work when the dll is just in the same folder as the script
(which is what I need)
I thought the search order for things like this always try the local
folder first, and then search through the path?
Below is the function that calls the dll. The problem is when I try
and call it I get:
8# $helper::_openCommPort(1,2)
Exception calling "_openCommPort" with "2" argument(s): "Unable to
load DLL 'weatherlink.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)"
At line:1 char:23
+ $helper::_openCommPort( <<<< 1,2)
9#
The dll is in the same location as the ps1 script, maybe the c#
snippet is being executed in a different context - i.e. different
folder? I've tried changind the DLLImport string to specifiy the
local folder, i.e. ".\weatherlink.dll" too
Any pointers?
---function snippet---
function Process-WS {
$code = @'
using System;
using System.Runtime.InteropServices;
public class WSHelper
{
[DllImport("weatherlink.dll")]
private static extern Int32 OpenCommPort( Int32 port, Int32
baud );
public static Int32 _openCommPort( Int32 port, Int32 baud )
{
return OpenCommPort( port, baud );
}
}
'@
[WSHelper] > $null
$global:helper = [WSHelper]
trap {
$cp = new-object Microsoft.CSharp.CSharpCodeProvider
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $false
$cpar.OutputAssembly = "custom"
$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 "Compile failed..."
}
else
{
# don't report the exception
continue
}
}
[Int32]$port = 2;
[Int32]$baud = 2400;
[Int32]$retVal = $helper::_openCommPort( $port, $baud )
}
---end of function snippet--


