Question

unit dll_dmunit;

interface

uses
 System.SysUtils, System.Classes, Data.DB, Datasnap.DBClient, Vcl.Dialogs,Vcl.DBGrids;

type
  TStudentModule = class(TDataModule)
    StudentSet: TClientDataSet;
    StudentSource: TDataSource;
    StudentSetNAME: TStringField;
    StudentSetID: TIntegerField;
    StudentSetAGE: TIntegerField;
    StudentSetSLNo: TAutoIncField;
    dlgOpen: TOpenDialog;
    dlgSave: TSaveDialog;
    private
    { Private declarations }
  public
  end;

procedure loadfile;stdcall;
procedure set_file(name_of_file:string);stdcall;

var
  StudentModule: TStudentModule;
  filename:string;

implementation

procedure set_file(name_of_file: string);stdcall;
   begin
     filename:=name_of_file;
   end;

 procedure loadfile;stdcall;
   begin
     StudentModule.StudentSet.LoadFromFile(filename);
   end;
 end.

This is the unit that I have included in the DLL and I have exported the function loadfile in export clause. When I use this function in a program I get an error read of address violation. I need to perform operation on TClientDataSet like load and save in a Dll and later use those in the programs. First I am calling the set_file method to initialise the filename Please help me regarding this. Thanking you in anticipation.

Was it helpful?

Solution

You would need to create your data module first. You are trying to use an object that doesn't yet exist. That is why you are seeing access violation. You also don't have a value in filename. What you should be doing is something like this:

procedure loadfile; stdcall;
var
  studentDataModule: TStudentModule;
  fileToLoad: string;
begin
  studentDataModule := TStudentModule.Create(nil);
  try
    // Set filename to something
    fileToLoad := 'Myfile.dat';
    // Load the file
    StudentModule.StudentSet.LoadFromFile(fileToLoad);
    // Do something else
    ...
  finally
    studentDataModule.Free;
  end;
end;

I didn't use your two global variables on purpose. There is nothing to initialize these.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top