There is the following code that simply reads the word file and saves it, and then opens it - everything worked well.
But after moving lines of code (working with a word document)

Words_Templs: variant; implementation {$R *.dfm} function CreateWord: boolean; //create word application begin CreateWord := true; try Words_Templs := CreateOleObject('Word.Application'); except begin // showmessage('Пакет Microsoft Office не найден'); Application.MessageBox('', 'Warning', MB_OK+MB_ICONEXCLAMATION); CreateWord := false; end; end; End; function Open_Doc_Template(APath_file: string): boolean; //open template file var LDoc: variant; begin CreateWord; Open_Doc_Template := true; try LDoc := Words_Templs.Documents; LDoc.Open(APath_file); except Open_Doc_Template := false; end; End; function CloseDoc:boolean; begin CloseDoc := true; try Words_Templs.ActiveDocument.Close; except CloseDoc := false; end; end; function Close_Document(AMode: integer): boolean; export; {close document 0 - MS Word 1 - MS Excel} begin if AMode = 0 then begin Close_Document := true; try Words_Templs.Quit; except Close_Document := false; end; end end; procedure OPEN_MAKE_DOC(ANamePath, ANameFile, ATypeFiles: string; AMode: integer; var APathFile: string); export; //open make document {LNameFile - name file + extension file} {LMode 0 - without open dialog window 1 - with open dialog window} var handle: hwnd; begin case AMode of 0: shellexecute(handle,'open',pchar(ANamePath+'result_doc.rtf'),'','',1); end; end; function SaveDocAs(ANamePath, ANameFile: string; AOpenDoc: integer):boolean; export; //save created doc { LOpenDoc 0 - not open created doc 1 - open created doc } var LPath: string; begin SaveDocAs := true; try begin Words_Templs.ActiveDocument.SaveAs(ANamePath+'result_doc.rtf'); CloseDoc; Close_Document(0); if AOpenDoc = 1 then OPEN_MAKE_DOC(ANamePath, 'result_doc.rtf', '', 0, LPath); end; except SaveDocAs := false; end; end; 

to the library and closing the application (which caused the dll) which successfully read and re - save the file, in DEBUG mode, an error began to occur:

Debugger Fault Notification --------------------------- Project D: \ Prog_Example \ Example application \ PEx_application.exe faulted with message: 'system exception ( code 0xc000000d) at 0x77417fdc '. Process Stopped. Use Step or Run to continue.

Application (caused dll)

  procedure Open_Doc_Template(LPath_file: string); far; external 'SPF'; function SaveDocAs(LNamePath, LNameFile: string; LOpenDoc: integer):boolean; far; external 'SPF'; procedure TForm2.Button1Click(Sender: TObject); begin Open_Doc_Template(ExtractFileDir(application.ExeName)+'\Template\Template_example.rtf'); SaveDocAs(ExtractFileDir(application.ExeName)+'\Result output\', 'result_doc.rtf', 1); end; 

In the mode of simply starting and closing the application, after it is closed, something happens for a second of course, but in the end everything ends successfully.

What could be wrong?

  • You again did not include the first unit in the projects and SharedMem libraries. What is the use of an uninitialized handle in OPEN_MAKE_DOC ? - Igor
  • SharedMem - both in the project ( Prdll.dpr ) and in the calling application ( PEx_application.dpr ) are included. This dll was under delphi7, and there I did not see such a problem .. Now I have delphi 10.2 - Konstantin78
  • In which version of Delphi is the DLL compiled, and in which version? - Igor
  • Understood not really. If these functions are in one module of the library, then everything works. But I have everything scattered in two modules. In the first module : CreateWord , Open_Doc_Template , CloseDoc . In the second module : OPEN_MAKE_DOC , SaveDocAs , Close_Document - Konstantin78
  • those. amendment, if all functions are in dpr , then everything works, And if in two pas , then - an error - Konstantin78

0