![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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-- |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Search order for DLLImport? Duncan Smith wrote:
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/ | ||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: Search order for DLLImport? "Duncan Smith" <DSmith1974@xxxxxx> wrote in message news:1189261840.762727.230780@xxxxxx
Temporarily add the local dir to the path like so: $env:Path += ";$pwd" then try to call your .net assembly. -- Keith | ||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Search order for DLLImport?
| ||||||||||||||||||||||||
| |
| |
![]() |
| 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 |