0

According to Delphi to C++ types mapping, Delphi equivalent of wchar_t* is PWideChar.

DLL Header File:

#include <s2wc.h>
#include <wc2s.h>
#include <windows.h>
using namespace std;    

#ifdef __cplusplus
extern "C"
{
#endif

wchar_t* DLL_EXPORT Fonksiyon(wchar_t* wchtAlinanMesaj);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

DLL Cpp File:

#include "main.h"

//String
string strMesaj;

//wchar_t*
wchar_t* wchtGonderilecekMesaj;

// a sample exported function
wchar_t* DLL_EXPORT Fonksiyon(wchar_t* wchtAlinanMesaj)
{
    WC2S(strMesaj, wchtAlinanMesaj);

    strMesaj = strMesaj + "Kaya";

    wchtGonderilecekMesaj = S2WC(strMesaj);

    return wchtGonderilecekMesaj;
}

Delphi Codes:

//type
//TFonksiyon = function(pwchMetin: PWideChar): PWideChar; cdecl;
procedure TForm1.Button1Click(Sender: TObject);
var
  dll: THandle;
  fonksiyonum: TFonksiyon;
  pwchAlinanMesaj, pwchGonderilecekMesaj: PWideChar;
begin
  dll := LoadLibrary('deneme.dll');

  if(dll >= 32) then begin
    fonksiyonum := GetProcAddress(dll, 'Fonksiyon');

    pwchGonderilecekMesaj := PWideChar(Edit1.Text);

    pwchAlinanMesaj := fonksiyonum(pwchGonderilecekMesaj);

    Edit1.Text := WideCharToString(pwchAlinanMesaj);
  end;
end;

But the problem is Edit1 does not respond when clicking the button.

So, what should I do?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I would start by debugging the application. Check: first, if the DLL is loaded correctly (otherwise, have a look at the error, it may be the DLL is not found, or the binary is not right...); then, if the exported DLL function is found. Finally, if the expected function behaves as you want. It all points to an early problem in your code though. – rturrado Oct 21 '21 at 13:52
  • For debugging in Delphi, if you don't want to do a step by step process, it is very useful to show a message box with some text. In your case, I would make one pop up after every line of your code (e.g. after the load library, the get proc address, and the fonksiyonum calls). – rturrado Oct 21 '21 at 13:56
  • Can we see how you've defined DLL_EXPORT? – Weston McNamara Oct 21 '21 at 14:18
  • You are not validating that GetProcAddress succeeds. Also, why is the DLL converting a wide string to a narrow string just to convert it back to a wide string? Just replace `std::string` with `std::wstring` to avoid that conversion. – Remy Lebeau Oct 21 '21 at 14:58
  • @rturrado Debugging shows nothing at all. Literally nothing! – Ignacio Ponseti Oct 21 '21 at 15:07
  • @rturrado Do you know the function ``OutputDebugString``? – Delphi Coder Oct 21 '21 at 15:20
  • Usefull read about DLLs: http://www.rvelthuis.de/articles/articles-dlls.html – Delphi Coder Oct 21 '21 at 15:20
  • What's `S2WC`? Does it produce a value that lives beyond leaving `Fonksiyon`? If it does, who's cleaning it up? – IInspectable Oct 21 '21 at 15:53
  • Likely the answer to your question can be found in the code we can't see. Show [mcve] if you want us to answer. Also, please stop checking LoadLibrary return value >32. That's a bug that gets copied over and over again. Read the documentation of LoadLibrary to learn how it signals errors. Proper error checking is always useful. – David Heffernan Oct 21 '21 at 15:55
  • @IInspectable That code converts string to wchar_t*. – Ignacio Ponseti Oct 21 '21 at 15:56
  • @rturrado I do not know the function which you said. – Ignacio Ponseti Oct 21 '21 at 15:57
  • @IgnacioPonseti `WC2S()` and `S2WC()` are not standard C++ library functions, so they must be user-defined functions. Please show their actual implementations, so we can validate whether `Fonksiyon()` is even returning a valid `wchar_t*` pointer. – Remy Lebeau Oct 21 '21 at 16:41
  • Why won't you show a [mcve]. We know how to do this. Let us guide you. – David Heffernan Oct 21 '21 at 18:53
  • @DavidHeffernan I share a video with you in order to show you all of my work: [link](https://youtu.be/HWiNFj4PDe0) – Ignacio Ponseti Oct 23 '21 at 13:55
  • We don't want a video. Please provide [mcve]. – David Heffernan Oct 23 '21 at 20:52

0 Answers0