Author: h.mohri
Made a DLL with Visual C++. And I tried to use it in C++Builder. Common is 64 bits.
First I will make a VC++ side DLL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//// extern "C" CONSOLEAPPLICATION1_API int test1(int i1) { ///This function only increments. ++i1; return i1; } extern "C" CONSOLEAPPLICATION1_API void test2(int i1, wchar_t* a) { ///Convert from int to wstring. std::wstring s{}; s = std::to_wstring(i1); ///It is not valid to allocate the wchar_t in the Visual C++ DLL and then free it in C++ Builder. ///wchar_t *a = new wchar_t[s.length() + 1]; wcscpy(a, s.c_str()); ///Finally cast to wchar_t* and return. ///return a; } |
Built it and made a DLL(ConsoleApplication1.dll).
It allows you to use this DLL with C++Builder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//--------------------------------------------------------------------------- #include <memory> void __fastcall TForm1::Button1Click(TObject *Sender) { HMODULE _dll=LoadLibraryA("ConsoleApplication1.dll"); if(_dll != nullptr) { FARPROC test1=GetProcAddress(_dll,"test1"); FARPROC test2=GetProcAddress(_dll,"test2"); using _test1 = int WINAPI (*)(int); using _test2 = void WINAPI (*)(int, wchar_t*); _test1 __inc=reinterpret_cast<_test1>(test1); _test2 __int_to_str=reinterpret_cast<_test2>(test2); std::unique_ptr<wchar_t> a{new wchar_t[4]}; int i1 = 100; int i = __inc(i1); __int_to_str(i, a.get()) ; ShowMessage(a.get()); FreeLibrary(_dll); } } //--------------------------------------------------------------------------- |
Loading(LoadLibrary) with button event.
It increments with the first function and converts it to wchar_t* with the second one.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition