complex!!

Posted by creeek on February 24, 2006

In Reply to how to invoke C# function in delphi code posted by Bijal on January 30, 2006

Hi, I did this about 8 years or more ago and just looked at the code as a reminder!

My project involved Delphi communicating with the DLL, and the the DLL code (written in C++) communicating to a DSP, low level interface.
Interfacing a DLL to Delphi is a little more difficult than Visual Basic (spit!!). Due to Pascal calling.

To give you a start, create the windows DLL correctly, cant quite remember the important issues, but assume you have already done this.


Delphi example code:

try
  dll_handle := LoadLibrary(StrC(dll_name)); // cant remember what the StrC function does? Forcing the 'type' i guess.
  if dll_handle < HINSTANCE_ERROR then // handle is a 'cardinal', get the address from the DLL
  begin
    error := DLL_NOT_FOUND;
    ShowError(error);
    raise EInitialiseError.Create('DLL library not found);
  end;

  {the following procedure pointers need to be pre-defined}
  @AnswerCall := GetProcAddress(dll_handle, 'AnswerCall'); // this needs to link in to the DLL, check if valid
  if @AnswerCall = NIL then
  begin
    error := DLL_ERROR;
    ShowMessage ('AnswerCall function missing');
    ShowError(error);
    raise EInitialiseError.Create('AnswerCall function missing in ' + dll_name);
  end;


Somehow, the following line would need to be included (as for this example), the 'stdcall' being the important point:

AnswerCall : function(base_address : Integer; error_code : PWord): Word; stdcall;

The 2 parameters are unique to this example and the return type is different from the standard C++ type, so the need to specify 'stdcall'

***********
Hope it make sense, if it dosnt, I am sure more intelligent programmers can help :)

If I am wrong anywhere, then shoot me!

Related Articles and Replies