all groups > dotnet clr > july 2005 >
You're in the

dotnet clr

group:

How Do You Get the Fully Qualified Name of an Assembly from Unmanaged Code?


Re: How Do You Get the Fully Qualified Name of an Assembly from Unmanaged Code? Matthijs van der Vleuten
7/17/2005 12:00:00 AM
dotnet clr:
[quoted text, click to view]

If loading that assembly is no problem, you can try:

Assembly a = System.Reflection.Assembly.LoadFile(pathToAssembly);
byte[] publicKeyToken = a.GetName().GetPublicKeyToken();

[quoted text, click to view]
How Do You Get the Fully Qualified Name of an Assembly from Unmanaged Code? Rod da Silva
7/17/2005 9:16:56 AM
Hi,

I have some unmanaged code and am looking for a way to determine the fully
qualified name of a given assembly DLL. In particular the PublicKeyToken
part of the fully qualified name is proving difficult to determine. I have
found a work around using the SN.EXE tool to list the PublicKeyToken given
an assembly name. My code currently "runs" SN with the -Tp switch and then
captures the output and parses it for the PublicKeyToken. Not very elegant
but it works.

Problem is, however, this solution requires the .Net SDK to be installed and
I am looking for a generic way to do this using only the .Net runtime.

Anybody have any ideas?

Thanks,
Rod

Re: How Do You Get the Fully Qualified Name of an Assembly from Unmanaged Code? Rod da Silva
7/17/2005 10:22:26 PM
Matthijs

Getting the value from "managed" code is no problem as you have shown. I
need to be able to do this from unmanaged code.

I was hoping for a solutoin using the IMetaDataAssemblyImport unmanaged COM
interface. This call looks like:

STDMETHOD(GetAssemblyProps)( // S_OK or error.
mdAssembly mda, // [IN] The Assembly for which
to get the properties.
const VOID **ppbPublicKey, // [OUT] Pointer to the public
key.
ULONG *pcbPublicKey, // [OUT] Count of bytes in the
public key.
ULONG *pulHashAlgId, // [OUT] Hash Algorithm.
LPWSTR szName, // [OUT] Buffer to fill with
name.
ULONG cchName, // [IN] Size of buffer in wide
chars.
ULONG *pchName, // [OUT] Actual # of wide chars
in name.
ASSEMBLYMETADATA *pMetaData, // [OUT] Assembly MetaData.
DWORD *pdwAssemblyFlags) PURE; // [OUT] Flags.

Clearly it returns a pointer to a PublicKey.

But does anybody know how to get the PublicKeyToken from a PublicKey??

Rod

[quoted text, click to view]

Re: How Do You Get the Fully Qualified Name of an Assembly from Unmanaged Code? Matthijs van der Vleuten
7/18/2005 7:28:22 AM
[quoted text, click to view]

Unfortunately, I've got no experience with unmanaged code.

[quoted text, click to view]

According to the documentation [1], the public key token is the last 8
bytes of the SHA-1 hash of the public key.

[quoted text, click to view]

Re: How Do You Get the Fully Qualified Name of an Assembly from Unmanaged Code? Mattias Sjögren
7/18/2005 9:47:46 AM
Rod,

[quoted text, click to view]

There's an API for this - StrongNameTokenFromAssembly exported from
Mscorsn.dll and declared in StrongName.h.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: How Do You Get the Fully Qualified Name of an Assembly from Unmanaged Code? Gael Fraiteur
7/19/2005 11:36:06 AM
Rod,

I think IMetaDataAssemblyImport is the right way.

About public key token, Common Language Infrastructure (CLI) Specifications,
Partition II writes:

"This declaration is used to store the low 8 bytes of the SHA1 hash of the
originator's public key in the assembly reference, rather than the full
public key."

Gael Fraiteur

http://gael.fraiteur.net

[quoted text, click to view]

AddThis Social Bookmark Button