dll function
-
hi, i want to add a method to dll which could return a value.hw can i do that?
-
hi, i want to add a method to dll which could return a value.hw can i do that?
-
Do you have the source code to the dll? Is it a .NET assembly? I think we all need more info from your side to be able to help.. :confused:
yes i have the source code nw i want to add a function which should return a string.Also i want to call the function outside dll.hw to do that?
-
hi, i want to add a method to dll which could return a value.hw can i do that?
Member 590310 wrote:
i want to add a method to dll which could return a value.hw can i do that?
In DLL:-
namespace my
{class ABC
{
private int _intValue=10;
public int ReturnValue()
{
return _intValue;
}
}In your Executable:- First add reference of DLL , then following code will suffix
my.ABC oABC = new my.ABC();
Console.WriteLine(oABC.ReturnValue());"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Member 590310 wrote:
i want to add a method to dll which could return a value.hw can i do that?
In DLL:-
namespace my
{class ABC
{
private int _intValue=10;
public int ReturnValue()
{
return _intValue;
}
}In your Executable:- First add reference of DLL , then following code will suffix
my.ABC oABC = new my.ABC();
Console.WriteLine(oABC.ReturnValue());"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
0k thats fine but i dnt want to add dll reference instead i want to load it on runtime like that. Assembly a = Assembly.LoadFrom(Application.StartupPath + @"\"+strLangName+".dll"); then hw to call its function?
-
yes i have the source code nw i want to add a function which should return a string.Also i want to call the function outside dll.hw to do that?
If you have the source code, you edit one of the classes (or add a new one) and create a method that does this. This is pretty basic stuff.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
0k thats fine but i dnt want to add dll reference instead i want to load it on runtime like that. Assembly a = Assembly.LoadFrom(Application.StartupPath + @"\"+strLangName+".dll"); then hw to call its function?
-
0k thats fine but i dnt want to add dll reference instead i want to load it on runtime like that. Assembly a = Assembly.LoadFrom(Application.StartupPath + @"\"+strLangName+".dll"); then hw to call its function?
as calla suggested, bellow is the simple way to do it.
string asseblyId = @"NewAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=531174506d670b8c";
ObjectHandle Handle=Activator.CreateInstance(asseblyId, "NewAssembly.ClassName");
object obj = Handle.Unwrap();
object[] parm = new object[] { 2, 4 };
object[] parm1 = new object[] { "Hello" };
Type t = obj.GetType();
//Invoketion of the Method...........
t.InvokeMember("DoIt", BindingFlags.Public | BindingFlags.NonPublic|BindingFlags.InvokeMethod|BindingFlags.Instance ,null , obj ,null);
int i=(int)t.InvokeMember("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj,parm);
Console.WriteLine(i);
//Set And Get the property
t.InvokeMember("Name", BindingFlags.Public | BindingFlags.SetProperty|BindingFlags.Instance , null, obj, parm1);
string strname=(string)t.InvokeMember("Name", BindingFlags.Public|BindingFlags.GetProperty| BindingFlags.Instance, null, obj, null);
Console.WriteLine(strname);and bellow is the methods for class in dll
public class Employee
{
public string _name;
public int _age;
public string _address;
public string _id;
public Employee()
{
}
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
public int Age
{
get
{
return this._age;
}
set
{
this._age = value;
}
}
public string ID
{
get
{
return this._id;
}
set {
this._id = value;
}
}
public int Add(int i, int j)
{
return i + j;
}
public void DoIt()
{
Console.WriteLine("Welcome from DO it");
}
public override string ToString()
{
return String.Format("ID :{0} Name:={1} Age{2}", this._id, this._name, this._age);
}
}hope this helps!
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
as calla suggested, bellow is the simple way to do it.
string asseblyId = @"NewAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=531174506d670b8c";
ObjectHandle Handle=Activator.CreateInstance(asseblyId, "NewAssembly.ClassName");
object obj = Handle.Unwrap();
object[] parm = new object[] { 2, 4 };
object[] parm1 = new object[] { "Hello" };
Type t = obj.GetType();
//Invoketion of the Method...........
t.InvokeMember("DoIt", BindingFlags.Public | BindingFlags.NonPublic|BindingFlags.InvokeMethod|BindingFlags.Instance ,null , obj ,null);
int i=(int)t.InvokeMember("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj,parm);
Console.WriteLine(i);
//Set And Get the property
t.InvokeMember("Name", BindingFlags.Public | BindingFlags.SetProperty|BindingFlags.Instance , null, obj, parm1);
string strname=(string)t.InvokeMember("Name", BindingFlags.Public|BindingFlags.GetProperty| BindingFlags.Instance, null, obj, null);
Console.WriteLine(strname);and bellow is the methods for class in dll
public class Employee
{
public string _name;
public int _age;
public string _address;
public string _id;
public Employee()
{
}
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
public int Age
{
get
{
return this._age;
}
set
{
this._age = value;
}
}
public string ID
{
get
{
return this._id;
}
set {
this._id = value;
}
}
public int Add(int i, int j)
{
return i + j;
}
public void DoIt()
{
Console.WriteLine("Welcome from DO it");
}
public override string ToString()
{
return String.Format("ID :{0} Name:={1} Age{2}", this._id, this._name, this._age);
}
}hope this helps!
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan