Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:05:18+00:00 2026-05-26T12:05:18+00:00

i don’t work well with C++ but now i need build a function that

  • 0

i don’t work well with C++ but now i need build a function that call Delphi DLL and pass a string to DLL and get return new string.

here is my Delphi DLL code:

library testdll;
uses
  System.Classes,Winapi.Windows,System.SysUtils;
{$R *.res}

function hello(name : PWideChar):PWideChar;
var
rs:PWideChar;
begin
  rs:=PWideChar('Hello '+rs);
  Result:=rs;
end;

exports
hello;
begin
end.

Anyone can help me create simple code in C++ to call and get result form hello function, thank for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:05:19+00:00Added an answer on May 26, 2026 at 12:05 pm

    You are trying to concat a PWideChar to a String literal and return it as another PWideChar. That will not work as-is. You should not be returning a PWideChar anyway. That leads to memory management nightmares. A better design is to let the caller pass a buffer into the DLL to fill in instead, eg:

    library testdll;
    
    uses
      System.Classes,
      Winapi.Windows,
      System.SysUtils;
    
    {$R *.res}
    
    function hello(name, buffer : PWideChar; buflen: Integer): Integer; stdcall;
    var
      rs: UnicodeString;
    begin
      rs := 'Hello '+UnicodeString(name);
      if buffer = nil then
      begin
        Result := Length(rs) + 1;
      end else
      begin
        Result := Min(buflen, Length(rs));
        Move(rs[1], buffer^, Result * SizeOf(WideChar));
      end;
    end;
    
    exports
      hello;
    
    begin
    end.
    

    Then, given this C++ declaration::

    int __stdcall hello(wchar_t* name, wchar_t* buffer, int buflen);
    

    You can call it all kinds of different ways, depending on your needs:

    wchar_t str[256];
    int len = hello(L"joe", str, 255);
    str[len] = 0;
    ...
    
    int len = hello(L"joe", NULL, 0);
    wchar_t *str = new wchar_t[len];
    len = hello(L"joe", str, len);
    str[len] = 0;
    ...
    delete[] str;
    
    int len = hello(L"joe", NULL, 0);
    std::wstring str(len-1);
    str.resize(hello(L"joe", &str[0], len));
    ...
    
    int len = hello(L"joe", NULL, 0);
    UnicodeString str;
    str.SetLength(len-1);
    str.SetLength(hello(L"joe", str.c_str(), len));
    ...
    

    The same kind of code can be translated to Pascal very easily if you ever need to use the same DLL in Delphi:

    function hello(name, buffer: PWideChar, buflen: Integer): Integer; stdcall; extern 'testdll.dll';
    
    
    var
      str: array[0..255] of WideChar;
      len: Integer;
    begin
      len := hello('joe', str, 255);
      str[len] := #0;
      ...
    end;
    
    
    var
      str; PWideChar
      len; Integer;
    begin
      len := hello('joe', nil, 0);
      GetMem(str, len];
      len := hello('joe', str, len);
      str[len] := #0;
      ...
      FreeMem(str);
    end;
    
    
    var
      str; UnicodeString;
      len; Integer;
    begin
      len := hello('joe', nil, 0);
      SetLength(str, len-1);
      SetLength(str, hello('joe', PWideChar(str), len));
      ...
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't need to do this right now but thinking about the future... What would
Don't ask why but I need to add class zebra to the <li> elements
DON'T ASK WHY but... I have a regex that needs to be case insensitive
Don't ask me why but i can't use this method because I need to
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
I don't edit CSS very often, and almost every time I need to go
I don't want PHP errors to display /html, but I want them to display
Don't think that I'm mad, I understand how php works! That being said. I
Don't know the difference between the System.Window.Controls.TextBox and System.Windows.Forms.TextBox . Noticed that the System.Windows.Forms.TextBox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.