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

  • SEARCH
  • Home
  • 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 8349013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:51:48+00:00 2026-06-09T07:51:48+00:00

I previously asked a question about a delphi and a C/C++ DLL. I have

  • 0

I previously asked a question about a delphi and a C/C++ DLL.

I have now another question about a record / struct.
The DLL should be able to dynamically CHANGE the VALUE of the pointer vars from the MainAPP.

My delphi MAINAPP has the following record:

type MyRec = record
 MyInteger    : Pointer;
 MyWideString : pwidechar;
 MyString     : pchar;
 MyBool       : Pointer
end;

type
 TMyFunc = function ( p  : pointer ): pointer; stdcall;

procedure test;
var
 MyFunction  : TMyFunc;
 TheRecord   : MyRec;
 AnInteger   : Integer;
 AWideString : WideString;
 AString     : String;
 ABool       : Bool;
begin
 AnInteger                := 1234;
 AWideString              := 'hello';
 AString                  := 'hello2';
 ABool                    := TRUE;
 TheRecord.MyInteger      := @AnInteger;
 TheRecord.MyWideString   := pwidechar(AWideString);
 TheRecord.AString        := pchar(AString);
 TheRecord.ABool          := @ABool;
 [...]
 @MyFunction := GetProcAddress...
 [...]
 MyFunction  (@TheRecord);  // now the DLL should be able to change the values dynamically.
 MessageBoxW (0, pwidechar(AWideString), '', 0); // Show the results how the DLL changed the String to...
end;

C/C++ Code (just example)

typedef struct _TestStruct{
void    *TheInteger;    // Pointer to Integer
wchar_t *TheWideString; // Pointer to WideString
char    *TheAnsiString; // Pointer to AnsiString  
bool    *TheBool        // Pointer to Bool
}TestStruct;

__declspec(dllexport) PVOID __stdcall MyExportedFunc (TestStruct *PTestStruct)
{
MessageBoxW(0 ,PTestStruct->TheWideString, L"Debug" , 0); // We read the value.
    PTestStruct->TheWideString = L"Let me change the value here.";
return 0;
}

For some reasons it crashes etc.
What do I do wrong?

Thanks 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-06-09T07:51:50+00:00Added an answer on June 9, 2026 at 7:51 am

    You are mismanaging the string fields. PWideChar and PChar are not the same thing as “pointer to WideString” and “pointer to AnsiString”. Delphi has PWideString (WideString*) and PAnsiString (AnsiString*) types for that purpose instead. You should also use Delphi’s PInteger (int*) and PBoolean (bool*) types instead of Pointer (void*). Delphi and C++ are both type-safe languages. Stay away from untyped pointers when possible, your code will be better for it.

    type
      PMyRec = ^MyRec;
      MyRec = record
        MyInteger    : PInteger;
        MyWideString : PWideString;
        MyAnsiString : PAnsiString;
        MyBool       : PBoolean;
      end;
    
      TMyFunc = function ( p  : PMyRec ): Integer; stdcall;
    
    procedure test;
    var
      MyFunction  : TMyFunc;
      TheRecord   : MyRec;
      AnInteger   : Integer;
      AWideString : WideString;
      AAnsiString : AnsiString;
      ABool       : Bool;
    begin
     AnInteger                := 1234;
     AWideString              := 'hello';
     AAnsiString              := 'hello2';
     ABool                    := TRUE;
     TheRecord.MyInteger      := @AnInteger;
     TheRecord.MyWideString   := @AWideString;
     TheRecord.MyAnsiString   := @AAnsiString;
     TheRecord.MyBool         := @ABool;
     [...]
     @MyFunction := GetProcAddress...
     [...]
     MyFunction  (@TheRecord); 
     MessageBoxW (0, PWideChar(AWideString), '', 0);
    end;
    

    .

    typedef struct _MyRec
    {
        int        *MyInteger;    // Pointer to Integer
        WideString *MyWideString; // Pointer to WideString
        AnsiString *MyAnsiString; // Pointer to AnsiString  
        bool       *MyBool;       // Pointer to Bool
    } MyRec, *PMyRec;
    
    __declspec(dllexport) int __stdcall MyExportedFunc (PMyRec PRec)
    {
        MessageBoxW(NULL, PRec->MyWideString->c_bstr(), L"Debug" , 0);
        *(PRec->MyWideString) = L"Let me change the value here.";
        return 0;
    }
    

    With that said, it can be very dangerous to manipulate AnsiString (and UnicodeString) values across a DLL boundary like this, especially if the EXE and DLL are written in different versions of Delphi/C++Builder due to RTL differences, memory manager differences, payload layout differences, etc. WideString is OK to pass around, though, because it’s memory and layout are controlled by the OS, not the RTL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I previously asked a question about chaining conditions in Linq To Entities. Now I
I have previously asked a question about how can I write a code block
I've previously asked a question of about Delegates does anyone have a must have
I've previously asked a question about an issue I have been experiencing with CSharpOptParse
Ok, so I previously asked a question asking about my options, I have looked
Previously I've asked about inserting a column into a dataset . I now have
I previously asked a question about fetching the last 100 mentions for a person
I asked a question about this previously but my database structure has changed, and
I previously asked a question on stack overflow about how to format my date
I previously asked this question about how to make versioned serializaion using boost::serialization::access I

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.