I need to 'friend' a dll library that I didn't author.
I can see in the properties that it has a strong name, but how can I find out what the strong name is, so I can use it in System.Runtime.CompilerServices.InternalsVisibleTo?
To get the public key of a strong-named assembly, use the sn tool:
sn -Tp assembly.dll
This will show you the public key that you need to put in the InternalsVisibleTo attribute. If you open a Visual Studio command prompt, the sn.exe tool will already be in the path.
However, I would question what you are trying to actually achieve. If you have a compiled assembly that you did not write, adding the InternalsVisibleTo attribute to your code will let it access the internals of your code, but it wouldn't have compiled without already having friend access. If you are trying to access the internals of the other assembly, then the InternalsVisibleTo attribute will need adding to the other assembly - something which you cannot do without recompiling it..
You have to specify fully qualified name and public key token in AssemblyInfo.cs file of assembly you "need a friend":
[assembly: InternalsVisibleTo("FullAssemblyName, PublicKey=....", )]
If you have Reflector.NET or ildasm in hand you can use it to see this information
Related
I have a tlb files that contains some function declaration that I need to use.
If I use
#import "type_library.tlb"
I can correclty reference the function from my code:
tlb_namespace::required_function();
But when I compile the project the linker says that tlb_namespace::required_function is an unresolved external symbol.
How can I succesfully build this kind of project?
EDIT:
I have used the same type library in a Dummy VBA access project. I have added the reference to the type library and I have noticed that some of the function contained in the type library are correctly called. But some of them are not. VBA says It can't locate their entry point in the related dll.
Can this explain the unresolved external symbol when building c++ app?
I have also noticed that the failing function are declared in the tlb like this:
UPPER_function_name
but in the dll are declared like this:
Upper_function_name
Can this be the issue?
Is it possible to solve this kind of error directly modifying the binary tlb file or dll?
Use IDE to view TLB information.
Use this help : How to: View Type Library Information
At IDE : View-> Object Browser, click "..." Edit Custom Component Set, browse your TLB file and Add to view information.
Confirm namespace used for.
use the namespace to resolve the linker error:
example:
#import "<>" raw_interfaces_only
using namespace <>
this will resolve the problem
HOW I CAME ACROSS THIS
I wrote code for a simple stopwatch which can also double up as a Rubik's cube timer. The source code and the executable are here:
Cube timer
Anyway my doubt is not regarding this code(It works fine).
I downloaded the executable that I had uploaded to check if it worked fine and at that time I was greeted with this screen:
Open file - security warning
And under this dialogue box there was a field that said:
Publisher : Unknown Publisher
SCREEN SHOT:
DOUBT
Is there some way programatically or otherwise by which I can change the publisher field?
SPECS
I have compiled the code with Microsoft Visual C++ 2010 Express.
You can easily change the publisher, either when linking/compiling by setting the appropriate resources for your project (e.g. CompanyName), or modifying the resources with a resource editor.
Your problem is really that there is no signature, so even if a publisher field is present it cannot be trusted.
You can find an example resource rc file near the end of http://msdn.microsoft.com/en-us/library/windows/desktop/aa381058%28v=vs.85%29.aspx.
To add resources to your VC project check:
How do I embed version information into a windows binary?
VC++ 2012: How to include version info from version.inc (maintained separately) into the .rc file
The .rc file(s) will be compiled to binary (.res) and linked into your final executable.
To add or modify an existing executable, you should be able to use this tool (login required, this will cause the signature to be invalid in an already signed binary of course).
The Microsoft Authenticode documentation includes tutorials.
CAcert.org will sign a certificate you can use, and have instructions for getting started with Authenticode.
Sorry I can't be more helpful with VC, I don't use it, I usually using mingw and make, from some time ago targetting win32:
given a VERSIONINFO in a text version.rc file use mingw32-windres to compile it to a .o file (I actually had a bunch of .rc files, they were each #include-d in a single resources.rc so I only needed to run windres on that single file, and link a single extra object file)
include that version.o (or combined resources.o) in the final CC command, assuming compile and link to executable in one step
I also included -lversion when linking, AFAIR this was just because I used GetFileVersionInfo() for the code to check and display its own version in the 'About' dialog.
Make your program in a batch file, then using Advanced BAT to EXE Converter, convert it to EXE & fill out all of the fields. This sure helped me! :)
I am creating an eclipse rcp application in which I am using SAXParser to parse an XML document. The "EventsDefinition.xsd" which I am using to validate the XML document has following import:
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="xml.xsd"/>
I keep the "EventsDefinition.xsd" & "xml.xsd" in the eclipse folder of the exported rcp product.
For accessing the "EventsDefinition.xsd", I use the following code which works.
URL fileURL = new URL(Platform.getInstallLocation().getURL() + "EventsDefinition.xsd");
File eventsDefinitionFile = new File(fileURL.getPath());
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", eventsDefinitionFile);
With this, the parser is able to access "EventsDefinition.xsd" but not the "xml.xsd" referenced by it, because it tries to find the xml.xsd relative to the directory from which the rcp application is executed.
Is there a similar way to tell the parser to find the "xml.xsd" at eclipse folder rather than in the present working directory?
I tried specifying schemaLocation="http://www.w3.org/2001/xml.xsd" in EventsDefinition.xsd, but it fails to read the schema. So I have to use the local copy of "xml.xsd" present at the exported product's eclipse folder.
Any suggestions will be extremely helpful.
I think that the problem is with the import declaration. First, although permitted, is not recommended to use "namespace" as a namespace prefix. Second, the problem arises form the fact that you use "http://www.w3.org/XML/1998/namespace" as namespace name, which is prohibited. Take a look here: http://www.w3.org/TR/REC-xml-names/#dt-prefix , precisely here:
The prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace. It MAY, but need not, be declared, and MUST NOT be bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace.
Try to rename the namespace name to something else (and the namespace prefix too). Hope it helps.
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);
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.