Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Search order for DLLImport?

Closed Thread
 
Thread Tools Display Modes
Old 09-08-2007   #1 (permalink)
Duncan Smith
Guest


 

Search order for DLLImport?

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--

Old 09-08-2007   #2 (permalink)
Hal Rottenberg
Guest


 

Re: Search order for DLLImport?

Duncan Smith wrote:
Quote:

> 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?
I don't know the answer, but here are some pointers that may help:

Dynamic-Link Library Search Order
http://msdn2.microsoft.com/en-US/library/ms682586.aspx

SetDllDirectory
http://msdn2.microsoft.com/en-us/library/ms686203.aspx

The first article has several different cases where the search order is
different, but all of them do contain the current directory. Maybe if you
explicitly set-location to the directory in question that would help?

--

Hal Rottenberg
blog: http://halr9000.com
powershell category:
http://halr9000.com/article/category...ng/powershell/
Old 09-08-2007   #3 (permalink)
Keith Hill [MVP]
Guest


 

Re: Search order for DLLImport?

"Duncan Smith" <DSmith1974@xxxxxx> wrote in message
news:1189261840.762727.230780@xxxxxx
Quote:

> 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?
Yes the local folder of the PowerShell executable with is $PSHome.
Temporarily add the local dir to the path like so:

$env:Path += ";$pwd"

then try to call your .net assembly.

--
Keith

Old 09-09-2007   #4 (permalink)
Duncan Smith
Guest


 

Re: Search order for DLLImport?

Quote:

>
Quote:

> > I thought the search order for things like this always try the local
> > folder first, and then search through the path?
>
> Yes the local folder of the PowerShell executable with is $PSHome.
> Temporarily add the local dir to the path like so:
>
> $env:Path += ";$pwd"
>
> then try to call your .net assembly.
>
Good answer! Thanks Keith.

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
DllImport doesn't work in ASP.NET for old C-style dlls Chris Bordeman .NET General 0 04-10-2008 05:31 PM
Driver search fails everytime - and have to set the search manually (each time) to System32.. help... William Dowell Vista General 30 08-08-2007 07:40 PM
vista desktop search and windows mail search times invader@nospamforme.com Vista General 1 05-01-2007 01:03 AM
DNS Search Order Question Paul Wain Vista networking & sharing 1 03-23-2007 09:44 PM
Changing Start Menu Search default selection from "Search the Com. =?Utf-8?B?R3JhbnQ=?= Vista file management 0 07-13-2006 11:38 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50