-1

Using Delphi 2010, I am trying to write a dll for mIRC. Here is some information from the mIRC help file

Technical notes

This section contains technical information for programmers who want to create DLLs for use with mIRC.

The routine in the DLL being called must be of the form:

int __stdcall procname(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)

mWnd is the handle to the main mIRC window.

aWnd is the handle of the window in which the command is being issued, this might not be the currently active window if the command is being called by a remote script.

data is the information that you wish to send to the DLL. On return, the DLL can fill this variable with the command it wants mIRC to perform if any.

The DLL can return an integer to indicate what it wants mIRC to do:

0 means that mIRC should /halt processing

1 means that mIRC should continue processing

2 means that it has filled the data variable with a command which it wants mIRC to perform, and has filled parms with the parameters to use, if any, when performing the command.

3 means that the DLL has filled the data variable with the result that $dll() as an identifier should return.

The following code, which is supposed to reverse input text, compiles OK, but the results of using the dll are not what I expected.

function Reverse(mWnd, aWnd: hWnd; Data, Parms: PChar; Show, NoPause: Boolean): Integer; stdcall;
var
  Temp: String;
  I: Integer;
begin
  SetLength(Temp, Length(Data));
  for I := 0 to Length(Data) do
  begin
    Temp[I] := Data[Length(Data)- I]; // Reversing string by retrieving characters from Data one at a time
  end;
  StrCopy(Data, PChar(Temp)); // Putting reversed string back into data
  Result := 3;
end;
exports
    Reverse;
begin
end.

For example calling the dll from mIRC as //say $dll(mydll,Reverse,hello) gives an output of just 'o'. If the parameter is 'hello sailor' the output is 'orilsao llhe'. I have tried unsuccessfully to trace the operation of the code, but I cannot make sense of how the Data value changes. I suspect it my own limited knowledge of PChar behaviour is a contributing factor. I welcome any advice that may help. Cheers

Community
  • 1
  • 1
  • Boolean is wrong. Use LongBool. Or BOOL. The strings need to be PAnsiChar rather than PWideChar. This has been discussed so many times here. – David Heffernan Feb 18 '17 at 02:54
  • It may have been discussed so many times before, but one's searches missed it all. At the time of my post, I had not searched here for PChar behaviour in Delphi versions post Delphi7, and why would I? Initially I thought that I must be doing something wrong. I don't have your knowledge which is why I pasted the code. I thought the problem was me. The little that I did recall about memory and pointers focussed me on how the debugging watches behave so differently in Delphi 7 vs Delphi 2010. Cheers. – grasshopper Feb 18 '17 at 10:04
  • You should read the documentation. The Unicode change was huge and very heavily documented. Don't skimp on keeping up to date. – David Heffernan Feb 19 '17 at 17:50

1 Answers1

0

I changed PChar to PAnsiChar in to function definition. function Reverse(mWnd, aWnd: hWnd; Data, Parms: PAnsiChar; Show, NoPause: Boolean): Integer; stdcall;

I changed PChar to PAnsiChar in the line StrCopy(Data, PChar(Temp)); // Putting reversed string back into data and now my problem is no more. Just a tad more googling enable me to make the changes. Actually writing down my problem in this forum helped me zero in a solution.

Cheers, and thanks.

  • You still need to change `Boolean` to `BOOL`, like David told you. `Boolean` is 1 byte, `BOOL` is 4 bytes. When translating between languages. always use the same data type names whenever possible. Delphi has a definition for `BOOL` for compatibility with other APIs that also use `BOOL`, as it is a [standard Win32 data type](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751.aspx). – Remy Lebeau Feb 18 '17 at 19:25