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.
You are mismanaging the string fields.
PWideCharandPCharare not the same thing as “pointer to WideString” and “pointer to AnsiString”. Delphi hasPWideString(WideString*) andPAnsiString(AnsiString*) types for that purpose instead. You should also use Delphi’sPInteger(int*) andPBoolean(bool*) types instead ofPointer(void*). Delphi and C++ are both type-safe languages. Stay away from untyped pointers when possible, your code will be better for it..
With that said, it can be very dangerous to manipulate
AnsiString(andUnicodeString) 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.WideStringis OK to pass around, though, because it’s memory and layout are controlled by the OS, not the RTL.