4

I need to use a DLL created using .NET framework. This DLL is made COM visible.

I want to use this DLL in an application created using Delphi 2006. I have followed following steps:

  1. Registered the DLL using regscr32.
  2. Imported the type library using Delphi IDE. It created _TLB.pas file. Following signature was created in TLB file.

    function TValidationRequest.Process(var meterBSN: WideString; var NICSerial: WideString; 
                                        var partNumber: WideString; var otherConfig: WideString; 
                                        out returnMessage: WideString): Smallint;
    begin
      Result := DefaultInterface.Process(meterBSN, NICSerial, partNumber, otherConfig, returnMessage);
    end;
    
  3. I tried to call the method using following code snippet.

procedure TForm1.buttonClick(Sender: TObject);
var
  valReq: TValidationRequest;
  s1, s2, s3, s4, s5: WideString;
  o: WideString;
begin
  valReq := TValidationRequest.Create (Self);
  try
    valReq.Process (s1, s2, s3, s4, o);
    MessageDlg(o, mtInformation, [mbOK], 0);
  finally
    valReq := nil;
  end;
end;

But I get following error when Process method is called. alt text

Please can you suggest any solution?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Hemant
  • 19,486
  • 24
  • 91
  • 127

2 Answers2

7

Try initializing the WideStrings (s1,s2,s3,s4, and maybe even o). If I recall correctly, they are dynamic in Delphi and nil (000000000) before you set them up.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
MarkusQ
  • 21,814
  • 3
  • 56
  • 68
  • You rock MarkusQ! Thanks a lot. I would have given you 100 votes if I could. :) – Hemant Feb 25 '09 at 06:56
  • Nice job! That was what I spotted when I looked at it, too. @Hermant: For future use, remember that local vars (declared in a method body) are for the most part (strings excepted) not initialized and can contain anything. Vars declared globally or as member fields of a class are set to defaults. – Ken White Feb 25 '09 at 12:02
2

In addition to what MarkusQ said, note that your memory management of the TValidationRequest object is not so good. It would be cleaner to use Create(nil) and then in the finally-block write FreeAndNil(valReq). The way it is now you create one TValidationRequest object every time you click the button and they will all stay in memory until you destroy the form. At least you won't get memory leaks because you passed Self to the constructor so at least the form will take care of destroying those objects. In your example there is really no need to keep the object alive after the TForm1.buttonClick method has ended.

Oliver Giesen
  • 9,129
  • 6
  • 46
  • 82
  • Fully agree with your point. Actually I created the code snippet for better understanding of the question. It is not part of my production code :) Thanks for pointing out though! – Hemant Feb 26 '09 at 05:06