The Artima Developer Community
Sponsored Link

C# Answers Forum
loading a dll

15 replies on 2 pages. Most recent reply: Jun 5, 2008 4:23 AM by Wimpie Ratte

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 15 replies on 2 pages [ 1 2 | » ]
meital

Posts: 1
Nickname: metalika
Registered: Sep, 2003

loading a dll Posted: Sep 10, 2003 6:50 PM
Reply to this message Reply
Advertisement
Hi,

can someone please help me.
I want to load my dll and operate functions within the dll (which is written in C#).however, the loading is done at run time, so I used this lines:

[DllImport("dllLibrary.dll)]
public static extern void Hello();

for example, in order to operate the function Hello within my dllLibrary.dll.
The Exception which was thrown says somthing about not finding
an entrypoint named Hello in my dll.

can someone please explain to me what is an entrypoint, and what should
I specify as an entrypoint (the function name?, the name of the class within the dll?)


Thanks in advance


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: loading a dll Posted: Sep 11, 2003 8:33 AM
Reply to this message Reply
.Net dlls are not really dlls. I don't know why Microsoft decided to use that extension and confuse matters. Anyhow, you'll want to look into reflection, especially Assembly.Load(), Assembly.GetType() and Activator.CreateInstance() among other things.

dineshwaran

Posts: 3
Nickname: sdinesh
Registered: Oct, 2003

Re: loading a dll Posted: Oct 31, 2003 2:23 AM
Reply to this message Reply
I am new to this forum and also new C#.

Matt, I read about dll in .NET and now in a confusion what is dll,assemblies,metadata and libraries in .NET ??.Can u explain to me in short.

Thanx in advance.

Sam Leslie

Posts: 1
Nickname: samleslie
Registered: Nov, 2003

Re: loading a dll Posted: Nov 11, 2003 10:45 PM
Reply to this message Reply
You could load the assembly and execute the method using the reflection API link the following example

Assembly a = Assembly.LoadFile("dllLibrary.dll");
HelloClass hc = (HelloClass)a.CreateInstance("HelloClass") hc.Hello();

sdinesh79

Posts: 7
Nickname: sdinesh79
Registered: Sep, 2003

Re: loading a dll Posted: Nov 11, 2003 11:57 PM
Reply to this message Reply
Hi sam, thank u for the reply.

I am basically a java programmer turning towards .NET.So can u please explain the relationship in terms of java. I mean is .NET namespace and java package the same?. Likewise can u give me the equivalent of .NET assemblies,.dll in java?

Are .NET assemblies the same as .class files in java??

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: loading a dll Posted: Nov 21, 2003 6:23 PM
Reply to this message Reply
.NET assemblies are more like Java ar files; they can contain many classes, namespaces, resources and metadata. The .NET namespace is like the Java package conceptually, but has differences in practice. For example, the jar file is a zip file that has a heirarchy reflecting the package organization, whereas assemblies don't have that directory hierarchy and are are not zip files. You can also spread namespaces across assemblies, but I'm not sure if you can spread packages across jar files (I haven't tried that...).

On the other hand, an assembly with a single class in it is more like a class file than a jar file.

Anyway, they are different in many ways. If I really wanted to research the differences, I'd read up on class files (in Bill Venners's Inside the Java Virtual Machine book and/or at http://java.sun.com) and then read up on assemblies in .NET (in books like Inside Microsoft .NET IL Assembler, or at http://msdn.microsoft.com). There are many more books and web sites that will go into great detail on all of this, as well.

Lukas

Posts: 1
Nickname: odie
Registered: Mar, 2004

Loading a dll Posted: Mar 23, 2004 10:23 PM
Reply to this message Reply
Matt

I compiled an assembly in Delphi 8 .NET, containing TClass1, which in turn contains method (procedure in Delphi, void in C#) PleaseWork.

How do I load this assembly in C#? I would prefer to be able to use it in designtime. I have tried Reflection, but it doesn't want to work. Could you help me?

Regards
Odie

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Loading a dll Posted: Mar 25, 2004 10:35 AM
Reply to this message Reply
If you buy me a copy of Delphi.net, I'll look into this for you! ;-)

Seriously though, since I haven't used Delphi.net, I can only assume that the intent is that it should work just like all the other .net languages. With that assumption, I would guess that your your class, or the properties or methods you are trying to access are perhaps not declared public. You should be able to examine your Delphi assembly with ildasm (which you can run from the command line of the shell you get from "Visual Studio.net Tools/Visual Studio.net Command Prompt"); have a look and see if you see what you expect.

By the way, you probably should have posted this as a new topic -- I almost didn't notice it.

Tex

Posts: 1
Nickname: tex
Registered: Jun, 2004

Re: loading a dll Posted: Jun 19, 2004 8:43 PM
Reply to this message Reply
Well ... hello at first ...

DLL load from file is exactly what I need but how does it work? I mean you can't declare an object of any type because it's defined inside the assembly. I tried it like this and the compiler doesn't like it - I actually understand why but how do I get it working?

Assembly MyAssembly = Assembly.LoadFrom("ClassLibTest.dll");
HelloClass myClass = (HelloClass)MyAssembly.CreateInstance("HelloClass");

F:\Visual Studio Projects\ConsoleApplication1\Class1.cs(12): The type or namespace name 'HelloClass' could not be found (are you missing a using directive or an assembly reference?)

Would be nice if you could explain how you did this ... Thx ...

Tex

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: loading a dll Posted: Jun 30, 2004 10:41 PM
Reply to this message Reply
Well Tex, I think you need to provide a bit more detail, about your exact problem, like the source to both projects, etc.

However, here is a simple example that will call a static method called foo() if it finds it in the specified assembly:

static void CallFoo(string filename)
{
   try
   {
      string fullFileName = new System.IO.FileInfo(filename).FullName;
      Assembly assembly = Assembly.LoadFile( filename );
      foreach( Type type in assembly.GetTypes() )
      {
         MethodInfo foo = type.GetMethod( "foo" );
         foo.Invoke(type, new object [] {} );
      }
   }
   catch( Exception e )
   {
      System.Console.WriteLine("{0}",e);
   }
}

Joseph Thoppil

Posts: 1
Nickname: jthoppil
Registered: Jun, 2006

Re: loading a dll Posted: Jun 12, 2006 9:32 AM
Reply to this message Reply
Matt:

I have a situation that requires for an unmanaged DLL to call a managed DLL (or for accuracy, assembly) written in C# within a .NET env. The problem I am facing is the fact that the invoking program is a COTS package, which actually does a LoadLibrary. So I give the LoadLibrary the name of the C# DLL (basically I went into my projects directory and found the name of the DLL). But after that I get stuck as I have no clue how to invoke a function within a class, or even how to instantiate the class from the invoking DLL. Also, the COTS vendor says that we should get rid of any name mangling that might occur within the C# environment.

Any help will be greatly appreciated. An example will kind of make it easier, as I am pretty much new to this stuff.

--JoJo

Charles Haws

Posts: 24
Nickname: hawscs
Registered: Nov, 2003

Re: Loading a dll Posted: Jun 13, 2006 4:40 PM
Reply to this message Reply
> Matt
>
> I compiled an assembly in Delphi 8 .NET, containing
> TClass1, which in turn contains method (procedure in
> Delphi, void in C#) PleaseWork.
>
> How do I load this assembly in C#? I would prefer to be
> able to use it in designtime. I have tried Reflection, but
> it doesn't want to work. Could you help me?
>
> Regards
> Odie

What you want to do is add a Reference. Reflection is a good thing to have, but not needed just to reference a dll. I'll assume you're in Visual C# or Visual Studio of some sort.

So, given a C# project that needs to reference some other .NET dll, all you need to do is right-click on the project and choose Add Reference. By default that shows the DLLs that are installed in the global assembly cache on that machine; instead you want to browse to the specific DLL you want to use.

Then you can just start writing code anywhere in this project that calls any public classes in the DLL.

jasmin halevi

Posts: 3
Nickname: jasminh
Registered: Jun, 2007

Re: loading a dll Posted: Jun 12, 2007 9:07 AM
Reply to this message Reply
hello,
can you write me which #include file i need to add so the line "Assembly a = Assembly.LoadFile("dllLibrary.dll");" will be compiled with out error?
"Assembly" is undeclared identifier in my code.

please help me,
jj

Wojciech Lukoszek

Posts: 1
Nickname: laki32
Registered: Jun, 2007

Re: loading a dll Posted: Jun 12, 2007 2:36 PM
Reply to this message Reply
Jasmin, first thing: this topic is about C#. In C#:

using System.Reflection;

but I think you're using C++\CLI, so you should use:

using namespace System::Reflection;

jasmin halevi

Posts: 3
Nickname: jasminh
Registered: Jun, 2007

Re: loading a dll Posted: Jun 13, 2007 12:43 AM
Reply to this message Reply
Fisrt,
Thank you for your response.

secondly,
I post new topic: load c# dll from c++ because I still have problem with the code: using namespace System;

please help me,
jj

Flat View: This topic has 15 replies on 2 pages [ 1  2 | » ]
Topic: J2ee .Net Interopearbility Previous Topic   Next Topic Topic: what are generic types used for?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use