Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2004

Tip: Looking for answers? Try searching our database.

Accessing unmanaged DLL from C# client

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vini Deep - 15 Apr 2004 17:37 GMT
(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..

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.