Mask
شنبه 12 اسفند 1391, 13:32 عصر
با سلام.
چون این 2 سوال مربوط به dll بود نخاستم 2 پست ایجاد کنم.
سوال اول :
تفاوت دو کد زیر چیست ؟ آیا هر کدام برای منظوری تهیه شده است؟
exports addition name 'addition';
exports addition;
سوال دوم :
چرا در کد زیر وقتی از Stdcall استفاده نکنیم ، جواب اشتباه بر میگردد.
کد Dll :
library Test;
uses
Windows,
SysUtils;
function addition(a, b: Integer): Integer;export;
begin
Result := a + b;
end;
exports addition;
begin
end.
کد بار گزاری در برنامه :
type
TMyAddition = function(a, b: Integer): Integer; stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
MyAddition: TMyAddition;
FunctionPointer: TFarProc;
hModule: THandle;
begin
hModule := LoadLibrary(PChar('test.dll')); // Dll invite
if hModule = 0 then
begin // Dll can not be loaded will leave the procedure
raise Exception.Create('DLL could not be loaded');
end
else
try
FunctionPointer := GetProcAddress(hModule, 'addition');
// Function address can be determined
if FunctionPointer <> Nil then
begin // If the function was then found
@MyAddition := FunctionPointer; // the code is executed in the function
ShowMessage(IntToStr(MyAddition(StrToInt(Edit1.Tex t),
StrToInt(Edit2.Text)))); // and output
FunctionPointer := Nil;
FreeLibrary(hModule);
end
else
ShowMessage('Function could not be found.');
// release of DLL
FreeLibrary(hModule);
except
on E: Exception do
begin // should still be caught in this error occur except
ShowMessage('Error : ' + E.Message);
FreeLibrary(hModule);
end;
end;
end;
در صورتی که اگر کد به شکل زیر شود ، جواب درست بر میگردد:
library Test;
uses
Windows,
SysUtils;
function addition(a, b: Integer): Integer;export;stdcall;
begin
Result := a + b;
end;
exports addition;
begin
end.
چون این 2 سوال مربوط به dll بود نخاستم 2 پست ایجاد کنم.
سوال اول :
تفاوت دو کد زیر چیست ؟ آیا هر کدام برای منظوری تهیه شده است؟
exports addition name 'addition';
exports addition;
سوال دوم :
چرا در کد زیر وقتی از Stdcall استفاده نکنیم ، جواب اشتباه بر میگردد.
کد Dll :
library Test;
uses
Windows,
SysUtils;
function addition(a, b: Integer): Integer;export;
begin
Result := a + b;
end;
exports addition;
begin
end.
کد بار گزاری در برنامه :
type
TMyAddition = function(a, b: Integer): Integer; stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
MyAddition: TMyAddition;
FunctionPointer: TFarProc;
hModule: THandle;
begin
hModule := LoadLibrary(PChar('test.dll')); // Dll invite
if hModule = 0 then
begin // Dll can not be loaded will leave the procedure
raise Exception.Create('DLL could not be loaded');
end
else
try
FunctionPointer := GetProcAddress(hModule, 'addition');
// Function address can be determined
if FunctionPointer <> Nil then
begin // If the function was then found
@MyAddition := FunctionPointer; // the code is executed in the function
ShowMessage(IntToStr(MyAddition(StrToInt(Edit1.Tex t),
StrToInt(Edit2.Text)))); // and output
FunctionPointer := Nil;
FreeLibrary(hModule);
end
else
ShowMessage('Function could not be found.');
// release of DLL
FreeLibrary(hModule);
except
on E: Exception do
begin // should still be caught in this error occur except
ShowMessage('Error : ' + E.Message);
FreeLibrary(hModule);
end;
end;
end;
در صورتی که اگر کد به شکل زیر شود ، جواب درست بر میگردد:
library Test;
uses
Windows,
SysUtils;
function addition(a, b: Integer): Integer;export;stdcall;
begin
Result := a + b;
end;
exports addition;
begin
end.