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. C++ DLL within C# application

C++ DLL within C# application

Scheduled Pinned Locked Moved C#
helpcsharpsqlitec++php
9 Posts 4 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.
  • M Offline
    M Offline
    my Nick
    wrote on last edited by
    #1

    Hello, dear forum. I have a problem programming on C#. My recent activities with C++ are back over 20 years. I downloaded from the internet a server/client-solution for SQLite and am stuck in my C#-application using this dll.

    HRESULT hr;
    HMODULE hMod = NULL;
    PDLLGETCLASSOBJECT proc = NULL;
    IClassFactory\* pFactory = NULL;
    
    //Get the module from list or load it
    hMod = GetModule( szDllPath );
    if( !hMod ) return( false );
    
    //Get Proc address
    proc = (PDLLGETCLASSOBJECT) GetProcAddress( hMod, "DllGetClassObject" );
    if( !proc ) return( false );
    
    hr = proc( IInterface , IID\_IClassFactory , (void\*\*)&Factory );
    if( hr || !pFactory ) return( false );
    
    hr = pFactory->CreateInstance( NULL, IInterface , pInterface );
    if( hr || !pInterface ) return( false );
    
    pFactory->Release();
    

    What I've done so far (not much):

    \[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode\]
    private static extern IntPtr LoadLibrary(String lpFileName);
    
    \[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true)\]
    private static extern IntPtr GetProcAddress(IntPtr hModule, String procName);
    
    \[DllImport("kernel32.dll", SetLastError = true)\]
    \[return: MarshalAs(UnmanagedType.Bool)\]
    private static extern Boolean FreeLibrary(IntPtr hModule);
    
    ...
    
    private static String DLLFile = string.Empty;
    private static IntPtr handleDLL = IntPtr.Zero;
    private static IntPtr handleProc = IntPtr.Zero;
    
    ...
    
    DLLFile = MyPath + @"Lib\\SQLQueryAS.dll";
    handleDLL = LoadLibrary(DLLFile);
    if (handleDLL == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
    
    handleProc = GetProcAddress(handleDLL, "DllGetClassObject");
    if (handleProc == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
    
    ???
    and now
    ???
    

    Then (unless I have understood correctly) it should invite a class, so you can responsive in this class located methods (procs/functions). Please help me. What should happen next? Thank you in advance.

    L M 2 Replies Last reply
    0
    • M my Nick

      Hello, dear forum. I have a problem programming on C#. My recent activities with C++ are back over 20 years. I downloaded from the internet a server/client-solution for SQLite and am stuck in my C#-application using this dll.

      HRESULT hr;
      HMODULE hMod = NULL;
      PDLLGETCLASSOBJECT proc = NULL;
      IClassFactory\* pFactory = NULL;
      
      //Get the module from list or load it
      hMod = GetModule( szDllPath );
      if( !hMod ) return( false );
      
      //Get Proc address
      proc = (PDLLGETCLASSOBJECT) GetProcAddress( hMod, "DllGetClassObject" );
      if( !proc ) return( false );
      
      hr = proc( IInterface , IID\_IClassFactory , (void\*\*)&Factory );
      if( hr || !pFactory ) return( false );
      
      hr = pFactory->CreateInstance( NULL, IInterface , pInterface );
      if( hr || !pInterface ) return( false );
      
      pFactory->Release();
      

      What I've done so far (not much):

      \[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode\]
      private static extern IntPtr LoadLibrary(String lpFileName);
      
      \[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true)\]
      private static extern IntPtr GetProcAddress(IntPtr hModule, String procName);
      
      \[DllImport("kernel32.dll", SetLastError = true)\]
      \[return: MarshalAs(UnmanagedType.Bool)\]
      private static extern Boolean FreeLibrary(IntPtr hModule);
      
      ...
      
      private static String DLLFile = string.Empty;
      private static IntPtr handleDLL = IntPtr.Zero;
      private static IntPtr handleProc = IntPtr.Zero;
      
      ...
      
      DLLFile = MyPath + @"Lib\\SQLQueryAS.dll";
      handleDLL = LoadLibrary(DLLFile);
      if (handleDLL == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
      
      handleProc = GetProcAddress(handleDLL, "DllGetClassObject");
      if (handleProc == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
      
      ???
      and now
      ???
      

      Then (unless I have understood correctly) it should invite a class, so you can responsive in this class located methods (procs/functions). Please help me. What should happen next? Thank you in advance.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      There is a .NET specific assembly available for download[^], which means you don't need C++. You open a SQLiteConnection and issue a SQLiteCommand :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

      M 1 Reply Last reply
      0
      • L Lost User

        There is a .NET specific assembly available for download[^], which means you don't need C++. You open a SQLiteConnection and issue a SQLiteCommand :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

        M Offline
        M Offline
        my Nick
        wrote on last edited by
        #3

        Thank you for answering. My intention is a Server/Client solution, where the Server is on another remote machine than the Client. IMHO the System.Data.SQLite does not support this. Or am I totaly wrong with the mentioned dll?

        J M 2 Replies Last reply
        0
        • M my Nick

          Thank you for answering. My intention is a Server/Client solution, where the Server is on another remote machine than the Client. IMHO the System.Data.SQLite does not support this. Or am I totaly wrong with the mentioned dll?

          J Offline
          J Offline
          John Torjo
          wrote on last edited by
          #4

          This sounds a bit weird. If the server is on another machine, the client should "protect" you from the fact that you're dealing with a database. If you want to use Sqlite (and connect to such a db) on a remote machine, I have never tried such a scenario, but seems to defeat the purpose of sqlite. In other words, the whole purpose of sqlite is to keep everything into a file which it can access really fast at any time. If that file is remote, that guarantee is gone. Best, John

          -- Log Wizard - a Log Viewer that is easy and fun to use!

          1 Reply Last reply
          0
          • M my Nick

            Thank you for answering. My intention is a Server/Client solution, where the Server is on another remote machine than the Client. IMHO the System.Data.SQLite does not support this. Or am I totaly wrong with the mentioned dll?

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            John is correct, you are using the wrong tool for the job. Trying to use a single user, local database as a server database. Use a server database!

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            • M my Nick

              Hello, dear forum. I have a problem programming on C#. My recent activities with C++ are back over 20 years. I downloaded from the internet a server/client-solution for SQLite and am stuck in my C#-application using this dll.

              HRESULT hr;
              HMODULE hMod = NULL;
              PDLLGETCLASSOBJECT proc = NULL;
              IClassFactory\* pFactory = NULL;
              
              //Get the module from list or load it
              hMod = GetModule( szDllPath );
              if( !hMod ) return( false );
              
              //Get Proc address
              proc = (PDLLGETCLASSOBJECT) GetProcAddress( hMod, "DllGetClassObject" );
              if( !proc ) return( false );
              
              hr = proc( IInterface , IID\_IClassFactory , (void\*\*)&Factory );
              if( hr || !pFactory ) return( false );
              
              hr = pFactory->CreateInstance( NULL, IInterface , pInterface );
              if( hr || !pInterface ) return( false );
              
              pFactory->Release();
              

              What I've done so far (not much):

              \[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode\]
              private static extern IntPtr LoadLibrary(String lpFileName);
              
              \[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true)\]
              private static extern IntPtr GetProcAddress(IntPtr hModule, String procName);
              
              \[DllImport("kernel32.dll", SetLastError = true)\]
              \[return: MarshalAs(UnmanagedType.Bool)\]
              private static extern Boolean FreeLibrary(IntPtr hModule);
              
              ...
              
              private static String DLLFile = string.Empty;
              private static IntPtr handleDLL = IntPtr.Zero;
              private static IntPtr handleProc = IntPtr.Zero;
              
              ...
              
              DLLFile = MyPath + @"Lib\\SQLQueryAS.dll";
              handleDLL = LoadLibrary(DLLFile);
              if (handleDLL == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
              
              handleProc = GetProcAddress(handleDLL, "DllGetClassObject");
              if (handleProc == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
              
              ???
              and now
              ???
              

              Then (unless I have understood correctly) it should invite a class, so you can responsive in this class located methods (procs/functions). Please help me. What should happen next? Thank you in advance.

              M Offline
              M Offline
              my Nick
              wrote on last edited by
              #6

              Thank you for participating in my question. Of course, you ALL are right with the (ab)use of SQLite for such a task. But there are in the market some providers of such scenarios, unfortunately, to pay what separates for my hobby. Therefore I've decided for this supplier, which is for free. Please, let's go away from the theme SQLite, at all from the theme database. My original question was how to reconstruct the functionality of a language (C++) in another language (C#). To use a DLL with embedded class ... Is there even any further help? Thank you.

              L J 2 Replies Last reply
              0
              • M my Nick

                Thank you for participating in my question. Of course, you ALL are right with the (ab)use of SQLite for such a task. But there are in the market some providers of such scenarios, unfortunately, to pay what separates for my hobby. Therefore I've decided for this supplier, which is for free. Please, let's go away from the theme SQLite, at all from the theme database. My original question was how to reconstruct the functionality of a language (C++) in another language (C#). To use a DLL with embedded class ... Is there even any further help? Thank you.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You should go back and read Eddy Vluggen's response. You do not need to reproduce anything from C++, you just need to use the .NET assembly that supports SQLite. If you then want to separate your database from your client application, you can use a socket connection between the two C# programs.

                1 Reply Last reply
                0
                • M my Nick

                  Thank you for participating in my question. Of course, you ALL are right with the (ab)use of SQLite for such a task. But there are in the market some providers of such scenarios, unfortunately, to pay what separates for my hobby. Therefore I've decided for this supplier, which is for free. Please, let's go away from the theme SQLite, at all from the theme database. My original question was how to reconstruct the functionality of a language (C++) in another language (C#). To use a DLL with embedded class ... Is there even any further help? Thank you.

                  J Offline
                  J Offline
                  John Torjo
                  wrote on last edited by
                  #8

                  If you want to go that route, first off, it matters a lot if the functions you want to use are C functions, or C++ functions. If they are C functions, you're in luck. Otherwise, each (member-)function will be really weirdly mangled by the compiler into and it will be really really painful to use. Assuming we're in the C-functions area, you don't really need to complicate yourself with loading the library and such. What I suggest is to bring the dll you need into a path that is auto-recognized by the OS - simplest one - your application path, where your executable is. Then, you can import C-functions the pinvoke[^] way. More info here[^] and here[^] If you're lucky, you can use this tool[^] to convert all your dll functions into C# function calls. More tools available here[^].

                  -- Log Wizard - a Log Viewer that is easy and fun to use!

                  M 1 Reply Last reply
                  0
                  • J John Torjo

                    If you want to go that route, first off, it matters a lot if the functions you want to use are C functions, or C++ functions. If they are C functions, you're in luck. Otherwise, each (member-)function will be really weirdly mangled by the compiler into and it will be really really painful to use. Assuming we're in the C-functions area, you don't really need to complicate yourself with loading the library and such. What I suggest is to bring the dll you need into a path that is auto-recognized by the OS - simplest one - your application path, where your executable is. Then, you can import C-functions the pinvoke[^] way. More info here[^] and here[^] If you're lucky, you can use this tool[^] to convert all your dll functions into C# function calls. More tools available here[^].

                    -- Log Wizard - a Log Viewer that is easy and fun to use!

                    M Offline
                    M Offline
                    my Nick
                    wrote on last edited by
                    #9

                    Thank you very much, John. I will see and try ...

                    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