for some reasons I must have dll in not in same folder where main executable lives. I use the method described here https://stackoverflow.com/a/2864714/5492345. My code looks like this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
namespace testnetwork
{
class Program
{
[DllImport("SomeLibrary.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int SomeMethod([MarshalAs(UnmanagedType.AnsiBStr)] string someInput);
static void Main(string[] args)
{
string CurrentPath = Uri.UnescapeDataString(new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);
var paths = new List<string>
{
CurrentPath + "/Folder1",
//CurrentPath + "/Folder2",
};
AddEnvironmentPaths(paths);
SomeMethod("myinput");
}
static void AddEnvironmentPaths(IEnumerable<string> paths)
{
var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(paths));
Environment.SetEnvironmentVariable("PATH", newPath);
}
}
}
Everything is working fine until I have internet connection. When there is no network connection I get an error
An unhandled exception of type 'System.DllNotFoundException' occurred in testnetwork.exe Additional information: Unable to load DLL 'SomeLibrary.dll': The specified network name is no longer available. (Exception from HRESULT: 0x80070040)
Seems like DllImport works differently when PC is disconnected from network? In my %PATH% variable there is no any UNC paths.
User contributions licensed under CC BY-SA 3.0