emcodem Posted December 27, 2024 Posted December 27, 2024 (edited) Dears, i am trying to come up with my own c/++ dll to connect autoit with mongodb or similar (without ADO). The goal is to exchange "unmodified" strings between autoit and my dll, we want to migrate a very large autoit application from using the filesystem as a database to a real database (potentially hundreds of thousands files are written and listed/read...), not yet sure if it really pays off to go the dll approach is worth it. One very simple alternative would be to use some http rest api bridge to talk with the database but this adds a little overhead because of the http protocol. The result of my tests below is as you see in the .au3 comments, the "Add" function call works including parameters and return int but the "Echo" functions parameter wchar_t* is always 0. Any clues be greatly appreciated! Besides some old forum posts i did not really find any examples how to pass string or binary data unmodified between dlls and autoit... test.au3: #AutoIt3Wrapper_UseX64=Y Global Const $g_sFileDll = 'C:\dev\FileBridge++\FileBridge++\x64\Debug\FileBridge.dll' $hdll = DllOpen($g_sFileDll) #this works, $test is 5 $test = DllCall($hdll, "int:cdecl", "Add", "int", 2, "int", 3) #this works, we get a console print in scite and the messagebox initiated by dll pops up $test = DllCall($hdll, "int:cdecl", "Function", "int", 2, "int", 3) #this does not work, we only get 0 pointer int the dll $utf16String = "Hello, World" $ptr = DllStructCreate("wchar_t [" & StringLen($utf16String) + 1 & "]") ; +1 for null terminator DllStructSetData($ptr, 1, $utf16String) DllCall($hdll, "int", "Echo", "ptr", DllStructGetPtr($ptr)) DllClose($hdll) C++ Part: FileBridge++.h: #include <wchar.h> #define DLL_EXPORT #if defined DLL_EXPORT #define DECLDIR __declspec(dllexport) #else #define DECLDIR __declspec(dllimport) #endif int CppAdd(int a, int b); extern "C" { // Declare all functions here DECLDIR void Function(void); DECLDIR int Add(int a, int b); DECLDIR void Echo( wchar_t* utf16String); // Function takes a pointer to MyStructure } FileBridge++.cpp: #include "pch.h" #include <iostream> #include <Windows.h> #include "FileBridge++.h" using namespace std; extern "C" { DECLDIR int Add(int a, int b) { return CppAdd(a, b); } DECLDIR void Function(void) { std::cout << "DLL Called! Hello AutoIt!" << std::endl; MessageBox(0, TEXT("This MsgBox is created by the dll"), TEXT("Simple Dll..."), 0); } DECLDIR void Echo(wchar_t* utf16String) { if (utf16String == nullptr) { std::wcout << L"Received NULL pointer!" << std::endl; return; } std::wcout << L"You wrote: [" << utf16String << L"]" << std::endl; } } int CppAdd(int a, int b) { return(a + b); } Edited December 27, 2024 by emcodem
Solution jchd Posted December 27, 2024 Solution Posted December 27, 2024 1 hour ago, emcodem said: $ptr = DllStructCreate("wchar_t [" & StringLen($utf16String) + 1 & "]") ; +1 for null terminator wchar_t is not a valid type. Use wchar instead and test result of function calls. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
emcodem Posted December 27, 2024 Author Posted December 27, 2024 (edited) @jchd thank you so much for the fast response! It works, in my examples above, only the au3 part needs to be changed as you describe: $ptr = DllStructCreate("wchar [" & StringLen($utf16String) + 1 & "]") ; +1 for null terminator For the way back from the DLL to autoit, we can just return wchar_t* instead of void from the C function and retrieve it like Local $aDLL = DllCall($hdll, "WSTR", "Echo", "ptr", DllStructGetPtr($ptr)) Edited December 27, 2024 by emcodem
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now