之前用TClientSocket都是把這個控制元件放在form上使用,
現在想寫個dll庫,純粹執行socket的連接,資料收發,不要界面顯示,該怎樣做?
uj5u.com熱心網友回復:
o:=TClientSocket.Create(nil);....
o.Free;
uj5u.com熱心網友回復:
我根據你說的,好像不行啊,呼叫的時候提示Access violation at address 0311.... write of address 0000008c
unit socketunit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Win.ScktComp, SHELLAPI,
Web.Win.Sockets, Vcl.ExtCtrls;
var
Csocket: TClientSocket;
function connectphone(port: integer):boolean;stdcall;export
implementation
function connectphone(port: integer):boolean;stdcall;export;
begin
Csocket :=TClientSocket.Create(nil);
csocket.Free;
result :=true;
end;
end.
library landidll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
System.SysUtils,
System.Classes,
windows,
messages,
socketunit in 'socketunit.pas';
{$R *.res}
exports
connectphone;
begin
end.
uj5u.com熱心網友回復:
江湖救急啊。。。。。。。。。。。uj5u.com熱心網友回復:
你就在dll中把創建,接收,發送,關閉函式暴露給呼叫者就行了,不必把tclientsocket暴露出來。在你的創建接收發送關閉函式里使用tclientsocket處理通訊就行了。
uj5u.com熱心網友回復:
我把這段代碼放在函式connectphone里也是不行啊
var
Csocket: TClientSocket;
uj5u.com熱心網友回復:
From也可放入Dll中。uj5u.com熱心網友回復:
這應該是別的地方引起的問題。你檢查一下其他代碼。
Csocket應該是個dll中全域變數,這樣在dll中的多個函式都可以使用,或者創建個單例模式的類。
uj5u.com熱心網友回復:
function connectphone(port: integer):boolean;stdcall;export;begin
Csocket :=TClientSocket.Create(nil);
csocket.Free;
result :=true;
end;
不使用Csocket了才釋放啊,怎么剛創建就釋放了啊。。。
uj5u.com熱心網友回復:
呼叫dll的工程里,參考時,沒加stdcall導致uj5u.com熱心網友回復:
呼叫dll的工程里,參考時,沒加stdcall導致
嗯,有可能
uj5u.com熱心網友回復:
用這種方式創建看也TClientDataSet.Create(Application);
uj5u.com熱心網友回復:
還有一種方式:1、將TClientSocket放在呼叫DLL的Form上;
2、Dll通過共用記憶體,呼叫From上的 TClientSocket;
uj5u.com熱心網友回復:
這個不算事吧,放到data module 里面,然后DLL呼叫DM.Create 就可以了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/80213.html
標籤:網絡通信/分布式開發
