Forum > General
Lazarus vs Delphi pointers
morknot:
I am converting an old Delphi application to Lazarus. All has gone well until I had to start dealing with pointers in one unit. The unit works in Delphi mode {$MODE Delphi} but I would like it to work in {$mode objfpc}{$H+} mode. The application uses Windows MMSystem and :
TMidiHdr = record
lpData: pchar; { pointer to locked data block }
dwBufferLength: Longint; { length of data in data block }
dwBytesRecorded: Longint; { used for input only }
dwUser: Longint; { for client's use }
dwFlags: Longint; { assorted flags (see defines) }
lpNext: PMidiHdr; { reserved for driver }
reserved: Longint; { reserved for driver }
and
midioutprepareheader(HMIDIOOUT hmo, LPMIDIHDR lpMidiOutHdr, UINT cbMidiOutHdr)
The following works fine
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- unit sysex; {$MODE Delphi} interface uses MMSystem…..; type PMidiHdr = ^TMidiHdr; procedure SendSysex(sysexdata:string); implementation procedure SendSysex(sysexdata:string);varmidih:PMidiHdr;rc:integer;Psysex:^Pchar; begin //sysexdata is in the form #$F0#$00#$F7..... //lphmo is the address of a handle Psysex:=@sysexdata; new(midih); GetMem(midih.lpData, length(sysexdata)+1); StrMove(midih.lpData, Psysex^, length(sysexdata)+1); midih^.dwBufferLength := length(sysexdata); midih^.dwFlags := 0; rc := midiOutPrepareHeader(lphmo, @midih.lpData, sizeof(Tmidihdr));If rc<>0 then ShowMessage('Header Not Prepared'); rc := midiOutLongMsg(lphmo, @midih.lpData, sizeof(Tmidihdr));IF rc<>0 then Showmessage('Sysex Data Not Sent');rc := midiOutUnprepareHeader(lphmo, @midih.lpData, sizeof(Tmidihdr)); FreeMem(midih.lpData);dispose(midih);sysexdata:='';end;
I have tried many changes to get the above to work under FPC mode but always get the error "may be pointer derefence is missing" and Fatal: Syntax error, ")" expected but "identifier LPDATA" found.
I would be grateful if someone could explain what the problem is. Thanks
Blaazen:
I hope this will help you: http://wiki.freepascal.org/Code_Conversion_Guide#When_accessing_values_in_a_pointer_to_a_record_you_must_dereference_first
lucamar:
Other notes:
* With Psysex:^Pchar you're creating a pointer to a pointer to char but LPData is a pointer to char
* AnsiString (the default string in $H+ mode) is really a pointer so to make a pointer to the string chars you need to make it point to the first char: Psysex:=@(sysexdata[1])
or maybe use a hard cast: Psysex := PChar(sysexdata)
morknot:
Thank you for your help and suggestions, Blaazen and lucamar. I will continue to experiment.
Thaddy:
You should stick to the original code and not change anything. That way it is far easier to debug for us.
I expect it is a very simple fix because everything points to a windows api declaration.
And those are not always compatible between FPC and Delphi for several reasons.
This is not a language dialect issue: if we have a good example it should compile both ways.
Also note that the Delphi versions of the Windows API units contain many mistakes (missing ; etc) that can cause your issue.
E.g.: I had to correct a lot for KOL but FPC is really near 100% compatible to D7. Even D2007.
So if that code does not compile, focus on your existing code and show us where it fails.
Navigation
[0] Message Index
[#] Next page