Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing C++ classes from within C# .NET

Accessing C++ classes from within C# .NET

Scheduled Pinned Locked Moved C#
helpcsharpquestionc++debugging
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vini Deep
    wrote on last edited by
    #1

    Hi, 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=CallingConvention.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.. Vini

    L A 2 Replies Last reply
    0
    • V Vini Deep

      Hi, 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=CallingConvention.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.. Vini

      L Offline
      L Offline
      lajiyo
      wrote on last edited by
      #2

      I think you can modify the dll if you have the source code. you can modify the functions which you want to dllimport. for example you can do "export c mapImage()" lajiyo

      V 1 Reply Last reply
      0
      • L lajiyo

        I think you can modify the dll if you have the source code. you can modify the functions which you want to dllimport. for example you can do "export c mapImage()" lajiyo

        V Offline
        V Offline
        Vini Deep
        wrote on last edited by
        #3

        lajiyo wrote: I think you can modify the dll if you have the source code. you can modify the functions which you want to dllimport. for example you can do "export c mapImage()" The mapImage() function is a member function of a class and the class is already declared as '__declspec(dllexport)'. But to access the class in my Client, I have created a class derived from 'IDisposable' and declared a member 'private IntPtr inst' to get the access to the exported class. Then the calling convention i need to use is 'ThisCall', isn't? How to remove the exception? Hope you meant extern "C" or is it 'export c' ? :confused: Vini

        1 Reply Last reply
        0
        • V Vini Deep

          Hi, 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=CallingConvention.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.. Vini

          A Offline
          A Offline
          Andy Wieberneit
          wrote on last edited by
          #4

          If you want to use C++ classes that are exported from a DLL, you should write a simple wrapper class library in Managed C++. You can then use that wrapper class library from your other .NET projects. For more details and a simple sample project, look at: http://www.codeguru.com/Cpp/Cpp/cpp\_managed/interop/article.php/c6867/

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups