Why the assembly Mono.Cecil wrote is smaller than original? - mono.cecil

Here is my test code
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(#"E:\vd\fishrun_apk\assets\bin\Data\Managed\System.dll");
assembly.Write(#"E:\vd\fishrun_apk\assets\bin\Data\Managed\System.dll.new");
System.dll.new and System.dll have different size? Could somebody tell me why?

Related

RoslynPad: could not load file or assembly exception

I am using RoslynPad project to test how external plugins (Class libraries) could be used in the script.
I have created Class Library with a single class and method.
In roslynPad project i have added "plugin_demo" into RoslynHostReferences NamespaceDefault structure.
I have also added reference to plugin dll:
MetadataReference newref = MetadataReference.CreateFromFile("G:\\projects\\demos\\plugin_demo\\plugin_demo\\bin\\Debug\\plugin_demo.dll");
MetadataReferences = MetadataReferences.Add(newref);
Now i see that my new class is seen by diagnostics and autocomplition also works.
The code with a method call compiles fine.
But when i run it i get exception:
Could not load assembly or file. File not found.
I have copied plugin_demo.dll into directory where compiled roslynPad dll resides. Still no result.
I have checked that class library and roslynpad use the same .Code version.
What can be the problem?
What is correct way of adding assemblies to roslyn project?
Thanks
I have solved the problem after debugging. The roslynPad project generates file nams_deps.json when editor is launched. It must be deleted and after that the assembly is found.

SVG.DLL Bitmap to unmanaged code

I have acquired the .NET SVG.DLL and written a test harness to see if this DLL will solve an SVG problem I have, and in fact it has. Now ... for this BIG hurdle, I need to marshall or return the Bitmap, Image, or what ever is easiest method back to the main C++ code that needs the Bitmap.
Can anyone please help.
Al Harper
Well I seemed have have finally figure out how to get bitmap from managed to unmanaged cpp code. In short I used the HBITMAP handle retrieved in the managed code, then re hydrated a bitmap in the CPP code from it.
Let me know if anyone wants a code snippet.
Al Harper

LoadLibrary() error code 1008

I want to load a C++ dll in C# (VS2012, C++ dll also created in VS2012 C++) that in C++ dll I've used a shared library generated by MATLAB with using of deploytool.(My configuration in both C# and C++ programs is x64 and shared library also generated in 64-bit version of MATLAB).
I've used following codes to load library and then I've recieved error code 1008 :
[DllImport("kernel32.dll",CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr LoadLibrary(string libname);
...
IntPtr handle = LoadLibrary("libName.dll");
if(handle == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
//at this point error code will be 1008.
}
Help me to resolve that, please!
Thanks in advance.
Good chance the failure isn't directly in libName.dll but rather in one of its dependencies. There are quite a few tools that diagnose such dll loading problems:
Process monitor file system activity, can show you if a dll is sought but not found.
Dependency walker's 'profile' option can show you which dependent dll failed to load.
The most powerful tool AFAIK: 'Show Snaps' checkbox in gflags (bundled with debugging tools for windows). It activates a windows-loader-built-in tracing facility, that shows you not only who failed to load but also why exactly. Some more details and screenshots here.

Not able to find AssemblyFactory class using Mono.Cecil

I am using the Mono.Cecil DLL file and writing this code:
AssemblyDefinition sourceAssembly = AssemblyFactory.GetAssembly(assemblyPath);
My project is not getting compiled, because it is not able to find the class "AssemblyFactory". As if this class is not present in the DLL file at all. I have added the mono.cecil.dll file as a reference to my project. Is this class present somewhere outside the DLL file, maybe in some other DLL file at the .NET level?
This simply means that you're using an up to date version of Mono.Cecil where this deprecated type has been removed.
Please have a look at the migration page on Cecil's wiki to know how to convert code for Cecil 0.9. You'll see that you just have to change this line to read:
var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);

Error while compiling a library in C#

I am trying to compile a class(sqlAccess) declared as public with few methods related to database connection in it. I am getting the following error ...
Error 1 Friend access was granted to 'SqlAccess, PublicKey=00c8', but the output assembly is named 'SQLAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Try adding a reference to 'SqlAccess, PublicKey=00c8' or changing the output assembly name to match. c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll SQLAccess
What could be the reason? is there already a method of that name? I am new to programming so am unable to understand this clearly. Thank you.
This worked for me:
Open the Properties|Signing Tab. Ensure that you have "Sign the Assembly" checked and the strong name key file referenced. Save and compile the Project/Solution.
(cited from MSDN)
.NET seems to get grumpy if you give your assembly/project a name that isn't unique. In your case, SqlAccess must already exist in .NET or a referenced assembly.
The fix is to rename your assembly.
Similar issue:
Weird error in C#
That's because SqlAccess assembly has a reference which granted internal access to SqlAccess. It must be something like this [you will find it in AssemblyInfo.cs] :
[assembly: InternalsVisibleTo("Name of assembly goes here, PublicKey=")]
During compile time when compiler can not find assembly with specific PublicKey, you will get the error such as 'Friend access was granted to...'.
In order to resolve this problem one solution is to remove above attribute from source assembly, or add new public key and change it in source assembly.
Reason behind this should be either you have reinstall/update that particular dll within your solution and but old dll was not deleted properly from your solution and system.
That's why, it got worked when you change the Assembly name (from sqlAccess to sqlAccessXYZ)
I changed the Assembly name to sqlAccessXYZ and now its working, the problem is with the name. Not sure what exactly the problem, for now the issue is resolved. Thanks.

Resources