Use the DLL created by Visual C++2017 with C++Builder(Win64).

Posted by on in Programming

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.

////
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.

 

//---------------------------------------------------------------------------
#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.

 


 

 



About
Gold User, No rank,
Delphi / C++Builder blogger

Comments

  • Luis Felipe G2244
    Luis Felipe G2244 Friday, 31 March 2017

    Hello haruyuki. Do you test with bcc32c (clang WIn32)?

  • haruyuki mohri
    haruyuki mohri Friday, 31 March 2017

    Hi Luis, I have not tried it with bcc32c. I will try bcc32c later.
    (thanks for your retweet.)

  • haruyuki mohri
    haruyuki mohri Sunday, 2 April 2017

    strange in bcc32c.

  • Jason DeB
    Jason DeB Friday, 31 March 2017

    It is not valid to allocate the wchar_t in the Visual C++ DLL and then free it in C++ Builder. They are separate allocation pools.

  • haruyuki mohri
    haruyuki mohri Friday, 31 March 2017

    Thank you, fix it.

  • Jason DeB
    Jason DeB Friday, 31 March 2017

    You can find a detailed discussion of allocating memory in a DLL here.

    https://blogs.msdn.microsoft.com/oldnewthing/20060915-04/?p=29723

    In general the DLL should supply a method to free the memory since the caller won't be able to access the same runtime library that allocated the memory.

  • haruyuki mohri
    haruyuki mohri Friday, 31 March 2017

    thanks Jason DeB.

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: