NAudio Strong Name Issue - naudio

I'm using NAudio (1.7.3.0) and NAudio.lame ( 1.0.3.3048 ) to convert Wav to Mp3 audio format.
My code(calling assembly) is strongly named , VS2015 complains that Naudio/NAudioLame dlls should be strongly named too, so I did singed Naudio dlls with strong name. now Unfortunatley I'm build error as
Note : I have strongly named both(Naudio) dlls.
Here is the code.
try
{
 
string filePath = #"D:\Lame\Wav\25mb.wav";
string outputPath = #"D:\Lame\mp3\25mb.mp3";
using (WaveFileReader wavReader = new WaveFileReader(filePath))
using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(wavReader))
using (LameMP3FileWriter fileWriter = new LameMP3FileWriter(outputPath, pcm.WaveFormat, LAMEPreset.VBR_90))
{
pcm.CopyTo(fileWriter);
}
MessageBox.Show("Converted !");
}
catch (Exception ex)
{
 
MessageBox.Show(ex.Message);
}
Build error on : LameMP3FileWriter(outputPath, pcm.WaveFormat, LAMEPreset.VBR_90)
Error : The type 'WaveFormat' is defined in an assembly that is not referenced. You must add a reference to assembly 'NAudio, Version=1.7.3.0, Culture=neutral, PublicKeyToken=null.
Any help is appreciate !

You need to build your strongly named NAudio first, and then when you build NAudio.Lame, make sure it references your strongly named NAudio dll

Related

How do I modify this code to use another project in the solution

public void Launch()
{
if (LinkedInstance != null) return;
LinkedInstance = new OrderInstance(ServiceLocation, Algorithm, MaxPrice, Limit, PoolData, ID, StartingPrice, StartingAmount);
if (HandlerDLL.Length > 0)
{
try
{
Assembly ASS = Assembly.LoadFrom(HandlerDLL);
Type T = ASS.GetType("HandlerClass");
HandlerMethod = T.GetMethod("HandleOrder");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
This code will open an assembly based on file name and assign HandlerMethod with T.GetMethod.
The thing is I do not want to create a dll file.
I want to do this with a project file in the solution.
The type of HandlerMethod is this
private MethodInfo HandlerMethod;
So How do I assign HandlerMethod to point to a method in a class I have.
It just happens that the name of the assembly of sample Handler is Handler Example
So I tried to change the code to this
Assembly ASS = Assembly.LoadFrom("HandlerExample");
Type T = ASS.GetType("HandlerClass");
HandlerMethod = T.GetMethod("HandleOrder");
It simply cause exception.
How do I arrange that ASS would refer to the dll file that is in another project in the solution. I want minimal code change.
If the method is present in some other project then you can either add a reference to that project and use the method directly, or if you want to use reflection, then Assembly ASS = Assembly.LoadFrom(HandlerDLL); will do the job that you are already using.
If the class is in the same project then you can use Assembly ASS = Assembly.GetExecutingAssembly(); and in this case you don't need to provide dll name.
Note: Though it is not mentioned by OP what language he is using, but the code looks like c# to me.

Loading XAML workflow with multiple activity assemblies

I have a XAML workflow which, which uses custom activities that are stored in more than one dlls. I’m trying to execute this workflow using WorkflowApplication. However I cannot figure out how to resolve multiple reference assemblies, while loading the XAML. I’m aware that the XamlXmlReaderSettings provides a LocalAssembly property, which allows us to provide the reference assembly. However, it only allows to provide a single assembly. How do I provide multiple reference assemblies to the reader, so that it is able to resolve the external types? Any help will be greatly appreciated. I’ve pasted the code I’m using for reference.
public void LoadWorkflowFromFileAsync(string workflowXaml, Assembly activityAssembly)
{
var xamlReaderSettings = new XamlXmlReaderSettings
{
LocalAssembly = activityAssembly
};
var xamlSettings = new ActivityXamlServicesSettings
{
CompileExpressions = true
};
using (var reader = new XamlXmlReader(workflowXaml, xamlReaderSettings))
{
_activity= ActivityXamlServices.Load(reader, xamlSettings);
}
}
Does your xmlns in the XAML include the assembly name (ex. xmlns:ede="clr-namespace:Sample.MyActivityLibrary;assembly=Sample.MyActivityLibrary")?
I'm not aware of anyway to reference multiple local assemblies in XamlXmlReaderSettings but if the assembly is referenced in the XAML it should resolve automatically.

Best way to extract .zip and put in .jar

I have been trying to find the best way to do this I have thought of extracting the contents of the .jar then moving the files into the directory then putting it back as a jar. Im not sure is the best solution or how I will do it. I have looked at DotNetZip & SharpZipLib but don't know what one to use.
If anyone can give me a link to the code on how to do this it would be appreciated.
For DotNetZip you can find very simple VB.NET examples of both creating a zip archive and extracting a zip archive into a directory here. Just remember to save the compressed file with extension .jar .
For SharpZipLib there are somewhat more comprehensive examples of archive creation and extraction here.
If none of these libraries manage to extract the full JAR archive, you could also consider accessing a more full-fledged compression software such as 7-zip, either starting it as a separate process using Process.Start or using its COM interface to access the relevant methods in the 7za.dll. More information on COM usage can be found here.
I think you are working with Minecraft 1.3.1 no? If you are, there is a file contained in the zip called aux.class, which unfortunately is a reserved filename in windows. I've been trying to automate the process of modding, while manipulating the jar file myself, and have had little success. The only option I have yet to explore is find a way to extract the contents of the jar file to a temporary location, while watching for that exception. When it occurs, rename the file to a temp name, extract and move on. Then while recreating the zip file, give the file the original name in the archive. From my own experience, SharZipLib doesnt do what you need it do nicely, or at least I couldnt figure out how. I suggest using Ionic Zip (Dot Net Zip) instead, and trying the rename route on the offending files. In addition, I also posted a question about this. You can see how far I got at Extract zip entries to another Zip file
Edit - I tested out .net zip more (available from http://dotnetzip.codeplex.com/), and heres what you need. I imagine it will work with any zip file that contains reserved file names. I know its in C#, but hey cant do all the work for ya :P
public static void CopyToZip(string inArchive, string outArchive, string tempPath)
{
ZipFile inZip = null;
ZipFile outZip = null;
try
{
inZip = new ZipFile(inArchive);
outZip = new ZipFile(outArchive);
List<string> tempNames = new List<string>();
List<string> originalNames = new List<string>();
int I = 0;
foreach (ZipEntry entry in inZip)
{
if (!entry.IsDirectory)
{
string tempName = Path.Combine(tempPath, "tmp.tmp");
string oldName = entry.FileName;
byte[] buffer = new byte[4026];
Stream inStream = null;
FileStream stream = null;
try
{
inStream = entry.OpenReader();
stream = new FileStream(tempName, FileMode.Create, FileAccess.ReadWrite);
int size = 0;
while ((size = inStream.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, size);
}
inStream.Close();
stream.Flush();
stream.Close();
inStream = new FileStream(tempName, FileMode.Open, FileAccess.Read);
outZip.AddEntry(oldName, inStream);
outZip.Save();
}
catch (Exception exe)
{
throw exe;
}
finally
{
try { inStream.Close(); }
catch (Exception ignore) { }
try { stream.Close(); }
catch (Exception ignore) { }
}
}
}
}
catch (Exception e)
{
throw e;
}
}

working with sqlce and ErikEJ.SqlCe library

i got this errors
Error 1 The type 'System.Data.SqlServerCe.SqlCeTransaction' is defined in an assembly is not referenced That. You Must add a reference to assembly 'System.Data.SqlServerCe, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 89845dcd8080cc91'.
WHERE DO I DOWNLOAD VERSION 4.0.0.0? I didn't find it.
Error 2 The type 'System.Data.SqlServerCe.SqlCeConnection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'.
same problem..
Error 3 The best overloaded method match for 'ErikEJ.SqlCe.SqlCeBulkCopy.WriteToServer (System.Data.DataTable)' Some invalid arguments have
overloaded? only i want to use it :s
Error 88 The best overloaded method match for 'ErikEJ.SqlCe.SqlCeBulkCopy.WriteToServer(System.Data.DataTable)' has some invalid arguments
?? it DOES allow datatable . i dont understand it..
Error 94 Argument '1': cannot convert from 'System.Data.DataTable [c:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\System.Data.dll]' to 'System.Data.DataTable []'
convert to datatable[]?? what?
well this is my method code.
private void DoBulkCopy(bool keepNulls, System.Data.DataTable tabla, string nombretabla)
{
if (tabla.Rows.Count > 0)
{
ErikEJ.SqlCe.SqlCeBulkCopyOptions options = new ErikEJ.SqlCe.SqlCeBulkCopyOptions();
if (keepNulls)
{
options = options |= ErikEJ.SqlCe.SqlCeBulkCopyOptions.KeepNulls;
}
//using (SqlCeBulkCopy bc = new SqlCeBulkCopy(connectionString, options))
using (SqlCeBulkCopy bc = new SqlCeBulkCopy(Resco.Data.Database.Instance.ConnectionString,options))
{
bc.DestinationTableName = nombretabla;
try
{
bc.WriteToServer(tabla);
}
catch(Exception ex) { }
}
}
}
Easy way -> install to project using Package Manager Console.
PM> Install-Package ErikEJ.SqlCeBulkCopy
See http://nuget.org/packages/ErikEJ.SqlCeBulkCopy

How to programmatically get DLL dependencies

How can I get the list of all DLL dependencies of a given DLL or EXE file?
In other words, I'd like to do the same as the "Dependency walker" tool, but programmatically.
What is the Windows (ideally .NET) API for that?
You can use EnumProcessModules function. Managed API like kaanbardak suggested won't give you a list of native modules.
For example see this page on MSDN
If you need to statically analyze your dll you have to dig into PE format and learn about import tables. See this excellent tutorial for details.
NOTE: Based on the comments from the post below, I suppose this might miss unmanaged dependencies as well because it relies on reflection.
Here is a small c# program written by Jon Skeet from bytes.com on a .NET Dependency Walker
using System;
using System.Reflection;
using System.Collections;
public class DependencyReporter
{
static void Main(string[] args)
{
//change this line if you only need to run the code one:
string dllToCheck = #"";
try
{
if (args.Length == 0)
{
if (!String.IsNullOrEmpty(dllToCheck))
{
args = new string[] { dllToCheck };
}
else
{
Console.WriteLine
("Usage: DependencyReporter <assembly1> [assembly2 ...]");
}
}
Hashtable alreadyLoaded = new Hashtable();
foreach (string name in args)
{
Assembly assm = Assembly.LoadFrom(name);
DumpAssembly(assm, alreadyLoaded, 0);
}
}
catch (Exception e)
{
DumpError(e);
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
static void DumpAssembly(Assembly assm, Hashtable alreadyLoaded, int indent)
{
Console.Write(new String(' ', indent));
AssemblyName fqn = assm.GetName();
if (alreadyLoaded.Contains(fqn.FullName))
{
Console.WriteLine("[{0}:{1}]", fqn.Name, fqn.Version);
return;
}
alreadyLoaded[fqn.FullName] = fqn.FullName;
Console.WriteLine(fqn.Name + ":" + fqn.Version);
foreach (AssemblyName name in assm.GetReferencedAssemblies())
{
try
{
Assembly referenced = Assembly.Load(name);
DumpAssembly(referenced, alreadyLoaded, indent + 2);
}
catch (Exception e)
{
DumpError(e);
}
}
}
static void DumpError(Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: {0}", e.Message);
Console.WriteLine();
Console.ResetColor();
}
}
To get native module dependencies, I believe it should be ok to get it from the PE file's import table, here are 2 links which explain that in-depth:
http://msdn.microsoft.com/en-us/magazine/bb985992.aspx
http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
To get .NET dependencies, we can use .NET's API, like Assembly.Load.
To get a .NET module's all dependencies, How about combine the 2 ways - .NET assemblies are just PE file with meta data.
While this question already has an accepted answer, the documentation referenced in the other answers, where not broken, is old. Rather than reading through all of it only to find it doesn't cover differences between Win32 and x64, or other differences, my approach was this:
C:\UnxUtils\usr\local\wbin>strings.exe E:\the-directory-I-wanted-the-info-from\*.dll > E:\TEMP\dll_strings.txt
This allowed me to use Notepad++ or gvim or whatever to search for dlls that were still depending on MS dlls with 120.dll at the end of the dll name so I could find the ones that needed updating.
This could easily be scripted in your favorite language.
Given that my search for this info was with VS 2015 in mind, and this question was the top result for a Google search, I supply this answer that it may perhaps be of use to someone else who comes along looking for the same thing.
To read the DLL's (modules) loaded by a running exe, use the ToolHelp32 functions
Tool help Documentation on MSDN.
Not sure what it will show for a .Net running exe (I've never tried it). But, it does show the full path from where the DLL's were loaded. Often, this was the information I needed when trying to sort out DLL problems. .Net is supposed to have removed the need to use these functions (look up DLL Hell for more information).
If you don't want to load the assembly in your program, you can use DnSpy (https://www.nuget.org/packages/dnSpyLibs):
var assemblyDef = dnlib.DotNet.AssemblyDef.Load("yourDllName.dll");
var dependencies = assemblyDef.ManifestModule.GetAssemblyRefs();
Notice that you have all the infos you can want in the "ManifestModule" property.