Page 1 of 1

как правильно вернуть из DLL строку

Posted: Wed Apr 24, 2019 6:04 pm
by cutecode
Имею свой exe-шник и свой DLL, который вызывается динамически
мне надо вызвать функцию из DLL

Code: Select all

LONG_PTR __cdecl KKM_open(wchar_t** err)
как правильно в переменную err "залить" строку символов?

в самой DLL я делаю примерно так

Code: Select all

LONG_PTR __cdecl KKM_open(wchar_t** err)
{
	wxString sz = getErrorDescription();
	*err = new wchar_t[sz.Length() + 1];
	lstrcpy(*err, sz);
	return NULL;
}
затем, вызывавшая функция эту функцию, заливает значение err в другую переменную, и освобождает память

Code: Select all

delete [] *err;
с компилятором от Vsual studio это работает.
Но для mingw32, если я освобождаю память, то прога вылетает, если память не освобождать, то все работает нормально.

Re: как правильно вернуть из DLL строку

Posted: Wed Apr 24, 2019 7:11 pm
by Kvaz1r
Не специалист, но нашел на SO вот такой вопрос:
Is it bad practice to allocate memory in a DLL and give a pointer to it to a client app?

Если подытожить то рекомендуют освобождать память там-же где и выделять.

Re: как правильно вернуть из DLL строку

Posted: Wed Apr 24, 2019 9:02 pm
by cutecode
ндаас,

походу у меня действительно разные версии alloc/free
придется все переделывать
СПС

Re: как правильно вернуть из DLL строку

Posted: Thu Apr 25, 2019 12:31 pm
by doublemax
Does the calling function also use wxWidgets? If yes, why not pass a wxString* or wxString& to the function and write into that?

Re: как правильно вернуть из DLL строку

Posted: Thu Apr 25, 2019 12:53 pm
by cutecode
doublemax wrote: Thu Apr 25, 2019 12:31 pm Does the calling function also use wxWidgets?
hi,

EXE - yes,
but DLL can't use wxWidgets

I've rewritten my code by allocating memory in EXE and passing this pointer to DLL

TY

Re: как правильно вернуть из DLL строку

Posted: Thu Apr 25, 2019 12:56 pm
by doublemax
EXE - yes,
but DLL can't use wxWidgets
But there is a wxString in that DLL code.

Re: как правильно вернуть из DLL строку

Posted: Thu Apr 25, 2019 7:18 pm
by cutecode
doublemax wrote: Thu Apr 25, 2019 12:56 pm
EXE - yes,
but DLL can't use wxWidgets
But there is a wxString in that DLL code.

oooops, Yes it is.))))

But I want to solve this problem not using wxWidgets.

This is an intergration DLL for my program, for other programmers. Not sure all programers can use wxWidgets

Re: как правильно вернуть из DLL строку

Posted: Thu Apr 25, 2019 8:27 pm
by ONEEYEMAN
Hi,
Why even use "wchar_t *"?
Why not simply "char *"?

Or even better - std::{w}string? We are talking C++ here...

Thank you.

Re: как правильно вернуть из DLL строку

Posted: Fri Apr 26, 2019 5:49 am
by cutecode
hello,

I remaked it to std::wstring&

Thank you