An error occurred while calling txmldocument in DLL! Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiBase/html/delphi_20061218133656127.html
When I use XML to read and write data in a DLL, such as configuration, an error occurs after each static call: access violation at address 00000000. read of address 00000000 ., what is the problem of NO release? Please check it out.
Code :
Library doitlib;
Uses
Sysutils, xmldom, xmlintf, msxmldom, xmldoc,
Classes, forms, ActiveX;
{$ R *. Res}
//////////////////////////////////////// ////////////////////////////////////////
/// // Getconfig: Read the configuration parameters. Return Value: pchar //////////////////
//////// Parameter section: group node; Type: pchar //////////////////////////////////
//////// Parameter key: configuration item node: Type: pchar ///////////////////////////////////
/////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
Function getconfig (section, key: pchar): pchar; stdcall;
VaR
Mypath: string; // file path
Configfile: txmldocument; // configuration file
Thenode: ixmlnode; // temporary storage node variable
Begin
Mypath: = extractfiledir (getmodulename (0) + '\ config. xml'; // obtain the file path
Coinitialize (NiL );
Configfile: = txmldocument. Create (application); // create XML
// Configfile. domvendor: = openxmlfactory; // specify the parser
Configfile. Active: = true;
If not fileexists (mypath) Then {// if the file does not exist} begin
Result: = pchar (''); // if the object does not exist, null is returned.
Exit;
End
Else
Configfile. loadfromfile (mypath); // if the file exists, the content is loaded from the file
Thenode: = configfile. documentelement; // get the root node
If thenode. childnodes. findnode (section) <> nil then
Thenode: = thenode. childnodes. findnode (section) // if the group node exists, obtain the group Node
Else begin
Result: = pchar (''); // if the group node does not exist, null is returned.
Exit;
End;
If thenode. childnodes. findnode (key) <> nil then
Thenode: = thenode. childnodes. findnode (key) // If the configuration item node exists, obtain the configuration item Node
Else begin
Result: = pchar (''); // If the config map node does not exist, null is returned.
Exit;
End;
Result: = pchar (thenode. Text );
// Configfile. Active: = false;
Configfile. Free;
Couninitialize;
End;
Function setconfig (section, key, newvalue: pchar): integer; stdcall;
VaR
Mypath: string; // file path
Configfile: txmldocument; // configuration file
Thenode: ixmlnode; // temporary storage node variable
Temprst: integer;
Begin
Temprst: = 1;
Coinitialize (NiL );
Mypath: = extractfiledir (getmodulename (0) + '\ config. xml'; // obtain the file path
Try
Configfile: = txmldocument. Create (application); // create XML
// Configfile. domvendor: = openxmlfactory; // specify the parser
Configfile. Active: = true;
// Set the automatic line feed of the file
If not fileexists (mypath) Then {// if the file does not exist} begin
Configfile. Version: = '1. 0 ';
Configfile. encoding: = 'gb2312 ';
Configfile. Options: = configfile. Options + [donodeautoindent];
Thenode: = configfile. createnode ('doitfaxconfig ');
Configfile. documentelement: = thenode;
End
Else begin
Configfile. loadfromfile (mypath); // if the file exists, the content is loaded from the file
Configfile. Version: = '1. 0 ';
Configfile. encoding: = 'gb2312 ';
Configfile. Options: = configfile. Options + [donodeautoindent];
End;
Thenode: = configfile. documentelement; // get the root node
If thenode. childnodes. findnode (section) <> nil then
Thenode: = thenode. childnodes. findnode (section) // if the group node exists, obtain the group Node
Else
Thenode: = thenode. addchild (section); // if the group node does not exist, create
If thenode. childnodes. findnode (key) <> nil then
Thenode: = thenode. childnodes. findnode (key) // If the configuration item node exists, obtain the configuration item Node
Else
Thenode: = thenode. addchild (key );
Thenode. Text: = newvalue;
Configfile. savetofile (mypath );
Temprst: = 0;
Finally
Configfile. Active: = false;
Configfile. Free;
Configfile. Destroy;
Couninitialize;
Result: = temprst;
End;
End;
Exports
Getconfig, setconfig;
Begin
End.
Library project1;
{Important Note about dll Memory Management: sharemem must be
First unit in your library's uses clause and your project's (select
Project-View Source) uses clause if your DLL exports any procedures or
Functions that pass strings as parameters or function results. This
Applies to all strings passed to and from your DLL -- even those that
Are nested in records and classes. sharemem is the interface unit
The borlndmm. dll Shared Memory Manager, which must be deployed along
With your DLL. To avoid using borlndmm. dll, pass string Information
Using pchar or parameter string parameters .}
Uses
Sysutils, xmldoc, xmlintf, ActiveX, forms,
Classes;
{$ R *. Res}
Function getconfig (section, key: pchar): pchar; stdcall;
VaR
Mypath: string; // file path
Configfile: txmldocument; // configuration file
Thenode: ixmlnode; // temporary storage node variable
Begin
Mypath: = extractfiledir (getmodulename (0) + '\ config. xml'; // obtain the file path
Coinitialize (NiL );
Configfile: = txmldocument. Create (application); // create XML
// Configfile. domvendor: = openxmlfactory; // specify the parser
Configfile. Active: = true;
If not fileexists (mypath) Then {// if the file does not exist} begin
Result: = pchar (''); // if the object does not exist, null is returned.
Exit;
End
Else
Configfile. loadfromfile (mypath); // if the file exists, the content is loaded from the file
Thenode: = configfile. documentelement; // get the root node
If thenode. childnodes. findnode (section) <> nil then
Thenode: = thenode. childnodes. findnode (section) // if the group node exists, obtain the group Node
Else begin
Result: = pchar (''); // if the group node does not exist, null is returned.
Exit;
End;
If thenode. childnodes. findnode (key) <> nil then
Thenode: = thenode. childnodes. findnode (key) // If the configuration item node exists, obtain the configuration item Node
Else begin
Result: = pchar (''); // If the config map node does not exist, null is returned.
Exit;
End;
Result: = pchar (thenode. Text );
// Configfile. Active: = false;
Configfile. Free;
Couninitialize;
End;
Function setconfig (section, key, newvalue: pchar): integer; stdcall;
VaR
Mypath: string; // file path
Configfile: txmldocument; // configuration file
Thenode: ixmlnode; // temporary storage node variable
Temprst: integer;
Begin
Temprst: = 1;
Coinitialize (NiL );
Mypath: = extractfiledir (getmodulename (0) + '\ config. xml'; // obtain the file path
Try
Configfile: = txmldocument. Create (application); // create XML
// Configfile. domvendor: = openxmlfactory; // specify the parser
Configfile. Active: = true;
// Set the automatic line feed of the file
If not fileexists (mypath) Then {// if the file does not exist} begin
Configfile. Version: = '1. 0 ';
Configfile. encoding: = 'gb2312 ';
Configfile. Options: = configfile. Options + [donodeautoindent];
Thenode: = configfile. createnode ('doitfaxconfig ');
Configfile. documentelement: = thenode;
End
Else begin
Configfile. loadfromfile (mypath); // if the file exists, the content is loaded from the file
Configfile. Version: = '1. 0 ';
Configfile. encoding: = 'gb2312 ';
Configfile. Options: = configfile. Options + [donodeautoindent];
End;
Thenode: = configfile. documentelement; // get the root node
If thenode. childnodes. findnode (section) <> nil then
Thenode: = thenode. childnodes. findnode (section) // if the group node exists, obtain the group Node
Else
Thenode: = thenode. addchild (section); // if the group node does not exist, create
If thenode. childnodes. findnode (key) <> nil then
Thenode: = thenode. childnodes. findnode (key) // If the configuration item node exists, obtain the configuration item Node
Else
Thenode: = thenode. addchild (key );
Thenode. Text: = newvalue;
Configfile. savetofile (mypath );
Temprst: = 0;
Finally
Configfile. Active: = false;
Configfile. Free;
Configfile. Destroy;
Couninitialize;
Result: = temprst;
End;
End;
Exports
Getconfig, setconfig;
Begin
End.
Try to create a new DLL and replace it with the above Code ~~
There will still be that error. I cannot see that the following code is different from the above Code.
There is nothing different. The DLL compilation error is correct.
To makeProgramCall the DLL you wrote
Understand?