Windows Vista Forums

Search order for DLLImport?
  1. #1


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


      My System SpecsSystem Spec

  2. #2


    Hal Rottenberg Guest

    Re: Search order for DLLImport?

    Duncan Smith wrote:

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

      My System SpecsSystem Spec

  3. #3


    Keith Hill [MVP] Guest

    Re: Search order for DLLImport?

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

    > 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


      My System SpecsSystem Spec

  4. #4


    Duncan Smith Guest

    Re: Search order for DLLImport?


    >

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


      My System SpecsSystem Spec

Search order for DLLImport? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Vista ultimate x64 Search engine issues - Search fails to index cprTodd Vista General 2 18 Jun 2009
DllImport doesn't work in ASP.NET for old C-style dlls Chris Bordeman .NET General 0 10 Apr 2008
Driver search fails everytime - and have to set the search manually (each time) to System32.. help... William Dowell Vista General 30 08 Aug 2007
DNS Search Order Question Paul Wain Vista networking & sharing 1 23 Mar 2007
Changing Start Menu Search default selection from "Search the Com. =?Utf-8?B?R3JhbnQ=?= Vista file management 0 14 Jul 2006