Earlier, when I had Delphi 7 , a dll was written on it and an application to it was written on it - everything was fine. The string constant was passed to dll.
Now Delphi 10.2, I had to sharpen dll for it.
I had to change the dll call in the application, to

function TRANSMIT_DATAS_DLL(F_Name_current_application: WideString): Integer; stdcall; far; external 'setting.dll'; 

those. from String to WideString .

Everything is well transmitted if F_Name_current_application is a var .
But I have this parameter const (for example:

const F_Name_current_application = 'Project.exe'

), if I submit it in this form, then the error:

... raised exception class $ C0000005 with message 'access violation at 0x0419a232: write of address 0x004ed6a0'.

Question :
Is it possible to somehow convey a constant, or is it impossible to do without variables?

PS
Well, the same effect, in principle, with an explicit transfer of the string ('TRANSMIT_DATAS_DLL (' Project.exe ');')

  • one
    If in Delphi 7 it was a string , then in 10.2 it should become AnsiString , and not WideString if I do not confuse anything. Because WideString in 7 and 10.2 versions. If you compile the library and the application in 10.2, then why not just leave a String? - Alekcvp
  • - I continue to compile the application in Delphi 7 - I also thought about AnsiString first, but instead of a normal font , hieroglyphs are transferred . And with "WideString" - everything is fine - Konstantin78
  • And yes, if the application is compiled for Delphi 10.2, then nothing needs to be changed, even the constants work - Konstantin78
  • const F_Name_current_application = 'Project.exe' is a UnicodeString, if the settings are not changed. - MBo
  • one
    gunsmoker.ru/2011/12/delphi.html#n5 rule number 6. This is me to "if you compile under 10.2 then you do not need to change anything." Well and - in exe the prototype is changed. And in the dll by chance is not the "delph" string left? There's also a need to change to WideString - kami

1 answer 1

It is best to do this through pointers. Declare the following function in dll:

 function MyFunc(const AStr: PAnsiChar): Integer; 

Function call:

 var VMyStr: string; begin MyFunc(PAnsiChar(VMyStr)); ... 

Getting the string inside the dll:

 function MyFunc(const AStr: PAnsiChar): Integer; var VStr: string; begin VStr := string(AStr); ... end; 

Instead of PAnsiChar, you can use PWideChar. The main condition is to use the same type both in the dll and in the application. Then everything will work regardless of the version of the dll compiler and the application.