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