Hi all, I know its not a real ole matter, but treats also on
exchanging information between Delphi and Excel using sockets.
Im developing one DLL interface for Excel to allow sockets to
transfer data to and from Excel from and to other apps.
Im stuck at the point that my DLL is getting worksheets changes and
sending to other app correcly, but on oposite side, the DLL is getting
the messages from other app and buffering OK, but Excel is getting the
messages from the dll by poling only.
Im working with Delphi and copy parts of the code bellow where Im
trying to pass the VBA callback routine address to DLL and execute it.
When I try the code bellow, occurs one exception when the dll tries to
call the callback routine.
Do you have any suggestion on whats wrong ?
Copy of code and files at
www.geocities.com/CapeCanaveral/3505/sock.zip
Thanks in advance.
Ricardo Dunna
**************
At DLL:
Type
Tproc=procedure;stdcall;
Var
Proc:^Tproc; {callback routine pointer}
Function setcallback(p:longint):longint;stdcall; {exported function}
begin
proc:=pointer(p); {points proc to VBA callback
routine}
setcallback:=1;
end;
procedure Tmyobj.OnSockRead(Sender: TObject; Socket:
TCustomWinSocket);
var
sBuf : string;
pData : pointer;
Size : integer;
begin
SockRead( socket, pData, Size );
if Size > 0 then
begin
showmessage("message received"); {just for debug pourposes}
SetLength( sBuf, Size );
move(pdata^,sbuf[1],size);
list.add(sbuf); {adds string message to list
buffer}
if proc<>nil then
proc^; {*** I get error here, trying to execute the callback
routine}
FreeMem(pData);
end;
end;
function getxfer: Pchar;stdcall; {exported to Excel VBA}
begin
if list.count=0 then result:=pchar(sNIL) else
Result := pchar(list.strings[0]); {returns first TCP message from
VBA}
end;
*****************
At VBA : (open/close sockets routines not shown)
Public Declare Function getxfer Lib "testedll" () As String
Public Declare Function setcallback Lib "testedll" (l As Long) As Long
Sub Gets() ' this should be the callback routine
Dim s As String
s = getxfer() ' here it gets the string from list in DLL
cells(1,1)= s ' tranfer s to Excel cells
End Sub
Public Sub Setacb() {runs once at worksheet and socket open only}
Dim ret As Long
ret = setcallback(AddressOf Gets)
End Sub