-3
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.

1 Answers1

4

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.

Graymatter
  • 6,529
  • 2
  • 30
  • 50
  • I am able to call this loadfile method of Dll in my application and load the TclientDataSet at the DLL end. Now I want the content of the TClientDataSet at the DLL to be visible at the application end. Please help me regarding it. – Akash_Kumar May 13 '14 at 05:02
  • @user3615007 You should put that request into a new question. It would help to give a bit of background to your question as you are trying to share a TClientDataSet between a DLL and a Delphi application. I am not sure that is the best way to do things. Some background would help people to understand why and they may have a better solution for you. – Graymatter May 13 '14 at 06:23
  • Thanks Graymatter I will certainly put my request in a new question so as to have better understanding of my problem so that others can help me easily. – Akash_Kumar May 13 '14 at 06:41