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

Это русская секция форума wxWidjets. В этой секции вы можете обсуждать любые вопросы, связанные с wxWidgets на вашем родном языке.
Post Reply
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Post 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, если я освобождаю память, то прога вылетает, если память не освобождать, то все работает нормально.
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

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

Post by Kvaz1r »

Не специалист, но нашел на SO вот такой вопрос:
Is it bad practice to allocate memory in a DLL and give a pointer to it to a client app?

Если подытожить то рекомендуют освобождать память там-же где и выделять.
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Post by cutecode »

ндаас,

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

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post 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?
Use the source, Luke!
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Post 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
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax »

EXE - yes,
but DLL can't use wxWidgets
But there is a wxString in that DLL code.
Use the source, Luke!
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Post 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
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

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

Post 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.
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Post by cutecode »

hello,

I remaked it to std::wstring&

Thank you
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
Post Reply