Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading an assembly located in the GAC from unmanaged code

Status
Not open for further replies.

MKuiper

Programmer
Jan 29, 2002
364
NL
I have written in C# some code which needs to be called from several unmanaged applications (located in different directories).

I have written in unmanaged C++ a dll which is used by every application to load and execute the C# code. It looks like this:

Code:
ICLRRuntimeHost * IRuntime = NULL;
LPCWSTR DotNetVersie = L"v2.0.50727";
LPCWSTR MyManagedDll = L"<Name of my dll>"; //PROBLEM !!
LPCWSTR MyTypename   = L"MyNamespace.MyClass";
LPCWSTR MyMethod     = L"MyMethod";
LPCWSTR MyArgument   = L"<doesnt matter>";
DWORD   ReturnValue  = 0;

HRESULT DotNetStatus =
        CorBindToRuntimeEx ( DotNetVersie, NULL, 0,
                             CLSID_CLRRuntimeHost,
                             IID_ICLRRuntimeHost,
                             (LPVOID *)&IRuntime );

if ( FAILED ( DotNetStatus ))
   { <handle error, no failures here>; }

DotNetStatus = IRuntime->Start ( );

if ( FAILED ( DotNetStatus ))
   { <handle error, no failures here, too>; }

DotNetStatus = IRuntime->ExecuteInDefaultAppDomain
       ( MyManagedDll, MyTypename, MyMethod,
         MyArgument, &ReturnValue );

if ( FAILED ( DotNetStatus ))
   { <handle error, this is where things go wrong> }

... <etc> ...

Problem is with the name of the assembly.

When I install the assembly in the same directory as the calling application and the field MyManagedDll is filled with the physical assemblyname (like C:\xxx\xxx.dll) everything is fine.

But I want MyManagedDll to be called from several applications and therefore put it into the GAC.

If I do this and fill the field MyManagedDll with a string like "MyManagedDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3154aff06323403", the call to ExecuteInDefaultAppDomain fails. The HRESULT returned is 80070002 (COR_E_FILENOTFOUND)

Being very thankful for any advice,


Marcel
 
it's my understanding that when working with the GAC assemblies must be fully named and be strongly typed (or signed, or something like that.)

so the assembly will need a public/private key signing. and you will need to refernece the assembly by it's token, and full name.

I haven't done any work like this directly, so I could be way off, but this is my understanding.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yeah, if the assembly is not strong named, it is not even possible to put it into the GAC. But I have succeeded putting it into the GAC. So that part should be ok (at least i hope).


Marcel
 
found this post
maybe ther is a problem with referencing the correct GAC version?

wait this makes sense. in your client project you need to reference the version in the GAC, not a related project in the solution. could this be it?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you for answering, Jason.

Unfortunately the link you supply is a question from somebody with maybe a similar issue, but no answer.

If you say "problem with referencing the correct version in the GAC", do you mean there is an error in the string "MyManagedDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3154aff06323403"?
Could be, but I've checked it a thousand times and collegues of me, too. (The public key token has been copy/pasted, the version matches too).

What do you mean with your last sentence ("Wait this ...")?

I can not make a reference to the GAC version, because we are talking about unmanaged code. I need to call managed code from applications which are unable themself to call it, but they are able to call an unmanaged dll function. So that's why I'm creating the unmanaged wrapper-dll.



Marcel
 
i'm stumped as this is outside my expertice.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
In the meantime, I have made one step forward.

Current situation:
MyManagedDll.dll is located in a separate directory, let's say C:\MyManagedDll\MyManagedDll.dll and in the GAC.

this is working (it loads MyManagedDll.dll from the GAC)

LPCWSTR MyManagedDll = L"C:\\MyManagedDll\\MyManagedDll.dll";

But, have also NGEN'ed C:\MyManagedDll.dll. Problem now is, the NGEN'ed version is not used.




Marcel
 
No, I want the NGEN'ed dll to be loaded and used.

(Sorry, I meant NGEN'ed C:\MyManagedDll\MyManagedDll.dll instead of NGEN'ed C:\MyManaged.dll - there is no C:\MyManagedDll.dll)

Marcel
 
But if I don't have it in the output folder, it fails to load at all

Marcel
 
Problem solved, assembly has been made com-visible and is executed using CCW (Com Callable Wrapper)

Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top