(Type your message here)
How can I dynamically load a unmanaged dll in C#. Is it using DllImport? As in VC, can I use the LoadLibrary() directly?
I have an unmanaged C++ DLL which exports certain classes. I want to access these classes from the C# client. I was able to do so by modifying the DLL source to export class creation and destruction functions. Is this the only option for the problem? If I don't have the source code for the DLL, but just the .dll file, how can i access the exported methods from the dll, in C# client?
Also, I have read somewhere about writing a Wrapper class for the DLL, in C#. Is it possible to write the wrapper in C#? Is there any link which will help me in this?
In my c# client , for DllImport, i have given the callingConvention as 'CallingConvention.ThisCall'. One of my exported function has a callBackFunction as argument. I declared a delegate to take care of this. But when executed, the application gives an error of "different calling convention used". If I change the CallingConvention to 'StdCall' or 'cdecl', there is no error, but then the call back function is not called.
[DllImport("ediImgReader - Debug.dll", EntryPoint ="?mapImage@Cedi9660Reader@@QAE_NGP6A_NIPAE_N@Z@Z",CallingConventionJllingConvention.ThisCall )]
private static extern System.Int32 mapImage(IntPtr inst,System.UInt16 SectorSize, ReadSectorDelegate CallbackFunctionAddress);
public delegate System.Int32 ReadSectorDelegate(System.UInt32 SectorToBeRead,System.Byte SetToSectorDataIsLiteral, System.Int32 IsLiteral);
public static System.Int32 ReadSector(System.UInt32 SectorToBeRead,System.Byte SetToSectorDataIsLiteral, System.Int32 IsLiteral)
{
MessageBox.Show("Inside Delegate");
return 1;
}
public static System.Int32 mapImage(System.UInt16 SectorSize, ReadSectorDelegate CallbackFunctionAddress)
{
mapImage(Instance.inst,SectorSize,CallbackFunctionAddress);
return 1;
}
Kindly help.
Thanks..
Maxim Kazitov - 16 Apr 2004 02:10 GMT
If you know DLL name but, don't know path on design-time you can use LoadLibrary
using System;
using System.Runtime.InteropServices;
namespace YogaGeneration.Win32
{
public class Kernel32
{
[DllImport("kernel32")]
public extern static IntPtr LoadLibrary(string lpLibFileName);
[DllImport("kernel32")]
public extern static Int32 FreeLibrary(IntPtr hModule);
}
}
Now, you can use DLLImport without path, just call LoadLibrary before try to call any exported functions.
[DllImport("MYDLL.dll", CharSet=CharSet.Auto, SetLastError=true)]
protected static extern Int32 MyFun(IntPtr context );
(Type your message here)
How can I dynamically load a unmanaged dll in C#. Is it using DllImport? As in VC, can I use the LoadLibrary() directly?
I have an unmanaged C++ DLL which exports certain classes. I want to access these classes from the C# client. I was able to do so by modifying the DLL source to export class creation and destruction functions. Is this the only option for the problem? If I don't have the source code for the DLL, but just the .dll file, how can i access the exported methods from the dll, in C# client?
Also, I have read somewhere about writing a Wrapper class for the DLL, in C#. Is it possible to write the wrapper in C#? Is there any link which will help me in this?
In my c# client , for DllImport, i have given the callingConvention as 'CallingConvention.ThisCall'. One of my exported function has a callBackFunction as argument. I declared a delegate to take care of this. But when executed, the application gives an error of "different calling convention used". If I change the CallingConvention to 'StdCall' or 'cdecl', there is no error, but then the call back function is not called.
[DllImport("ediImgReader - Debug.dll", EntryPoint ="?mapImage@Cedi9660Reader@@QAE_NGP6A_NIPAE_N@Z@Z",CallingConventionÊllingConvention.ThisCall )]
private static extern System.Int32 mapImage(IntPtr inst,System.UInt16 SectorSize, ReadSectorDelegate CallbackFunctionAddress);
public delegate System.Int32 ReadSectorDelegate(System.UInt32 SectorToBeRead,System.Byte SetToSectorDataIsLiteral, System.Int32 IsLiteral);
public static System.Int32 ReadSector(System.UInt32 SectorToBeRead,System.Byte SetToSectorDataIsLiteral, System.Int32 IsLiteral)
{
MessageBox.Show("Inside Delegate");
return 1;
}
public static System.Int32 mapImage(System.UInt16 SectorSize, ReadSectorDelegate CallbackFunctionAddress)
{
mapImage(Instance.inst,SectorSize,CallbackFunctionAddress);
return 1;
}
Kindly help.
Thanks..