線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1871
推到 Plurk!
推到 Facebook!

VC調用Delphi DLL在Freelibrary 時會出現記憶體讀取異常

尚未結案
bill534
一般會員


發表:38
回覆:51
積分:17
註冊:2007-02-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-12-29 11:00:55 IP:211.20.xxx.xxx 訂閱
Dear all
但在VB及VC下使用時,雖然功能執行都正常,唯獨在最後
下列是我的dll程式內容

library extendmemobus;
{ 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
embSetupFormUnt in 'embSetupFormUnt.pas' {embSetupForm};
var
memobus: TIdMemoBusClient;
procedure loadparam(param: Integer = 0); stdcall;
var
s1, s2: string;
ini: Tinifile;
begin
s1 := 'param' inttostr(param);
s2 := copy(ParamStr(0), 0, length(ParamStr(0)) - length(ExtractFileName(ParamStr(0)))) 'extendmemobus.ini';
ini := Tinifile.Create(s2);
try
memobus.BoundIP := ini.ReadString(s1, 'BoundIP', '192.168.1.2'); // pc
memobus.BoundPort := ini.ReadInteger(s1, 'BoundPort', 520); // pc
memobus.Host := ini.ReadString(s1, 'Host', '192.168.1.1'); // plc
memobus.Port := ini.ReadInteger(s1, 'Port', 520); // plc
memobus.ConnectTimeOut := ini.ReadInteger(s1, 'ConnectTimeOut', 3000);
memobus.ReadTimeout := ini.ReadInteger(s1, 'ReadTimeout', 1000);
memobus.TimeOut := ini.ReadInteger(s1, 'TimeOut', 15000); // Wait for data from the PLC
//showmessage(memobus.Host ',' inttostr(memobus.port) ' ' memobus.BoundIP ',' inttostr(memobus.BoundPort));
finally
ini.Free;
end;
end;
function embClose: smallint; stdcall;
begin
try
if assigned(memobus) then
begin
if memobus.Connected then
memobus.Disconnect;
freeandnil(memobus);
showmessage('end');
end;
sleep(100);
except
result := -2;
end;
end;
function embConnected: smallint; stdcall;
begin
if Assigned(memobus) then
begin
try
result := smallint(memobus.Connected);
except
result := -1; // connection error
end;
end
else
result := -2; // object access error
end;
function embReadDeviceBlock(addr, size: smallint; buffer: pInteger): smallint;
stdcall;
begin
Result := 0;
if assigned(memobus) then
begin
try
result := smallint(memobus.ReadRegisters(addr, size, buffer));
except
result := -1;
end;
end;
end;
function embWriteDeviceBlock(addr, Size: smallint; buffer: pInteger): smallint;
stdcall;
begin
Result := 0;
if assigned(memobus) then
begin
try
result := smallint(memobus.WriteRegisters(addr, size, buffer));
except
result := -1;
end;
end;
end;
function embOpen(param: smallint): smallint; stdcall;
begin
result := 1;
if not assigned(memobus) then
begin
showmessage('create');
try
// 整個DLL的初始化代碼
memobus := TIdMemoBusClient.Create(nil);
loadparam(0);
sleep(100);
except
result := -4; // create memobus error
exit;
end;
end
else
begin
try
if embConnected = 0 then
begin
loadparam(param);
memobus.Connect(memobus.ConnectTimeOut);
result := smallint(memobus.Connected);
end;
except
result := -1; // connection error
end;
end
end;
function embConnect(param: smallint): smallint; stdcall;
begin
if Assigned(memobus) then
begin
try
loadparam(param);
memobus.Connect(1000);
result := smallint(memobus.Connected);
except
result := -1; // connection error
end;
end
else
result := -2; // object access error
end;
function embDisconnect: smallint; stdcall;
begin
if Assigned(memobus) then
begin
try
memobus.Disconnect;
sleep(100);
result := smallint(memobus.Connected);
except
result := -3; // disconnection error
end;
end
else
result := -2; // object access error
end;
function embSetup(param: smallint): smallint; stdcall;
var
embSetupForm: TembSetupForm;
begin
try
embSetupForm := TembSetupForm.Create(nil);
embSetupForm.param := param;
if embSetupForm.ShowModal = 1 then
result := 1
else
result := 0;
embSetupForm:=nil;

sleep(100);
except
Result := -1;
end;
end;
exports
embOpen,
embClose,
embConnected,
embConnect,
embDisconnect,
embReadDeviceBlock,
embWriteDeviceBlock,
embSetup;
begin
// library initialization code
// These statements are executed once every time the library is loaded.
// memobus := nil;
// DLLProc := @MyDLLHandler;
// MyDLLHandler(DLL_Process_Attach);
end.

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyProjectDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//typedef UINT(__cdecl *embOpenProc)(UINT param);
typedef UINT(__stdcall *embOpenProc)(UINT param);
//typedef UINT(__cdecl *embCloseProc)();
typedef UINT(__stdcall *embCloseProc)();
typedef UINT(__stdcall *embSetupProc)(UINT param);

void CMyProjectDlg::OnButton2()
{
// TODO: Add your control notification handler code here
//UINT(*embClose)();
UINT(__stdcall *embClose)();

void CMyProjectDlg::OnButton6()
{
// TODO: Add your control notification handler code here
UINT(__stdcall *embSetup)(UINT param);

FreeLibrary(hInstance);
}



系統時間:2024-09-29 0:54:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!