New -> other -> DllWizard код длл:
| Код | library mydll;
uses SysUtils, Classes; const n=100; {$R *.res}
function Prostoe4islo(n: integer):boolean; stdcall; var ok:boolean;
i,j:integer;
begin ok:=true; if (n<=0)or(n=2) then ok:=false;
if n>=3 then for i:=2 to (n-1) do begin if round(n/i)-(n/i)=0 then ok:=false; end;
if ok=false then result:=false else result:=true; end;
{Директива exports указывает, какие функции будут экспортированы этой DLL} exports Prostoe4islo;
begin end.
|
нажимаю F9 вылазит: Dll создается
далее close
new aplication код:
| Код | program MainWindow;
uses Windows, Messages, SysUtils;
const id_Button = 1; id_Edit = 2;
function prostoe4islo(n:integer):boolean; stdcall; external 'MYDLL.DLL';
function PlainWinProc (hWnd: THandle; nMsg: UINT; wParam, lParam: Cardinal): Cardinal; export; stdcall; var Rect: TRect; s1, s2, s_c: array [0..127] of Char; n1, n2:integer;
begin
Result := 0; case nMsg of wm_Create: begin
GetClientRect (hWnd, Rect);
CreateWindowEx (0, 'button', '&OK', ws_Child or ws_Visible or ws_Border or bs_PushButton, Rect.Left + 120, Rect.Top + 10, 100, 20, hwnd, id_Button, hInstance, nil);
CreateWindowEx (0, 'Edit', '', ws_Child or ws_Visible or ws_Border or es_Number, Rect.Left + 10, Rect.Top + 10, 100, 20, hwnd, id_Edit, hInstance, nil); SetWindowText(GetDlgItem (hWnd, id_Edit),'0'); end; wm_Command: begin if LoWord (wParam) = id_Button then if HiWord (wParam) = bn_Clicked then begin GetWindowText(GetDlgItem (hWnd, id_Edit), s1, GetWindowTextLength(GetDlgItem (hWnd, id_Edit))+1); //MessageBox (hWnd, '', 'Ðåçóëüòàò', MB_OK)
if Prostoe4islo(strtoint(s1))then MessageBox (hWnd, 'Простое!', 'Результат', MB_OK); end; end; wm_Destroy: PostQuitMessage (0); else Result := DefWindowProc (hWnd, nMsg, wParam, lParam); end; end;
procedure WinMain; var hWnd: THandle; Msg: TMsg; WndClassEx: TWndClassEx; begin
WndClassEx.cbSize := sizeOf (TWndClassEx); WndClassEx.lpszClassName := 'PlainWindow'; WndClassEx.style := cs_VRedraw or cs_HRedraw; WndClassEx.hInstance := HInstance; WndClassEx.lpfnWndProc := @PlainWinProc; WndClassEx.cbClsExtra := 0; WndClassEx.cbWndExtra := 0; WndClassEx.hIcon := LoadIcon (hInstance, MakeIntResource ('MAINICON')); WndClassEx.hIconSm := LoadIcon (hInstance, MakeIntResource ('MAINICON')); WndClassEx.hCursor := LoadCursor (0, idc_Arrow);; WndClassEx.hbrBackground := GetStockObject (white_Brush); WndClassEx.lpszMenuName := nil;
if RegisterClassEx (WndClassEx) = 0 then MessageBox (0, 'Invalid class registration', 'Plain API', MB_OK) else begin hWnd := CreateWindowEx ( WS_SIZEBOX, WndClassEx.lpszClassName, 'Ïóñòîå îêíî', ws_OverlappedWindow, cw_UseDefault, cw_UseDefault, 240, 300, 0, 0, HInstance, nil); if hWnd = 0 then MessageBox (0, 'Window not created', 'Plain API', MB_OK) else begin ShowWindow (hWnd, sw_ShowNormal); while GetMessage (Msg, 0, 0, 0) do begin TranslateMessage (Msg); DispatchMessage (Msg); end; end; end; end;
begin WinMain; end.
|
подключаю длл статически через:
| Код | function prostoe4islo(n:integer):boolean; stdcall; external 'MYDLL.DLL';
|
и вызываю ф-ию через:
| Код | if Prostoe4islo(strtoint(s1))then MessageBox (hWnd, 'Простое!', 'Результат', MB_OK);
|
вылазит:

Сломал голову, в чем ошибка? Да еще если стереть строчку | Код | if Prostoe4islo(strtoint(s1))then MessageBox (hWnd, 'Простое!', 'Результат', MB_OK);
|
то прога комплитица
Сейчас попробую еще подключить динамически.
Динамически: Код длл такойже
Мэйн:
| Код | program MainWindowsDin;
uses Windows, Messages, SysUtils;
const id_Button = 1; id_Edit = 2;
function PlainWinProc (hWnd: THandle; nMsg: UINT; wParam, lParam: Cardinal): Cardinal; export; stdcall; var Rect: TRect; s1, s2, s_c: array [0..127] of Char; n1, n2:integer; prostoe4islo: function(n:integer):boolean; LibHandle: THandle;
begin
Result := 0; case nMsg of wm_Create: begin
GetClientRect (hWnd, Rect);
CreateWindowEx (0, 'button', '&OK', ws_Child or ws_Visible or ws_Border or bs_PushButton, Rect.Left + 120, Rect.Top + 10, 100, 20, hwnd, id_Button, hInstance, nil);
CreateWindowEx (0, 'Edit', '', ws_Child or ws_Visible or ws_Border or es_Number, Rect.Left + 10, Rect.Top + 10, 100, 20, hwnd, id_Edit, hInstance, nil); SetWindowText(GetDlgItem (hWnd, id_Edit),'0'); end; wm_Command: begin if LoWord (wParam) = id_Button then if HiWord (wParam) = bn_Clicked then begin GetWindowText(GetDlgItem (hWnd, id_Edit), s1, GetWindowTextLength(GetDlgItem (hWnd, id_Edit))+1);
//MessageBox (hWnd, '', 'Результат', MB_OK)
@Prostoe4islo := nil; LibHandle := LoadLibrary('MYDLL.DLL'); {Если все OK} if LibHandle <= 32 then MessageBox (hWnd, 'Ошибка 1', 'ошибка', MB_OK);
@Prostoe4islo := GetProcAddress(LibHandle,'Prostoe4islo'); {Если и здесь все OK} if @Prostoe4islo <> nil then begin {...то вызываем эту функцию и показываем результат}
if Prostoe4islo(strtoint(s1)) then MessageBox (hWnd, 'Число простое!', 'Результат', MB_OK) else MessageBox (hWnd, 'Число НЕ простое!', 'Результат', MB_OK); end else MessageBox (hWnd, 'Ошибка 2', 'ошибка', MB_OK);
FreeLibrary(LibHandle);
end; end; wm_Destroy: PostQuitMessage (0); else Result := DefWindowProc (hWnd, nMsg, wParam, lParam); end; end;
procedure WinMain; var hWnd: THandle; Msg: TMsg; WndClassEx: TWndClassEx; begin
WndClassEx.cbSize := sizeOf (TWndClassEx); WndClassEx.lpszClassName := 'PlainWindow'; WndClassEx.style := cs_VRedraw or cs_HRedraw; WndClassEx.hInstance := HInstance; WndClassEx.lpfnWndProc := @PlainWinProc; WndClassEx.cbClsExtra := 0; WndClassEx.cbWndExtra := 0; WndClassEx.hIcon := LoadIcon (hInstance, MakeIntResource ('MAINICON')); WndClassEx.hIconSm := LoadIcon (hInstance, MakeIntResource ('MAINICON')); WndClassEx.hCursor := LoadCursor (0, idc_Arrow);; WndClassEx.hbrBackground := GetStockObject (white_Brush); WndClassEx.lpszMenuName := nil;
if RegisterClassEx (WndClassEx) = 0 then MessageBox (0, 'Invalid class registration', 'Plain API', MB_OK) else begin hWnd := CreateWindowEx ( WS_SIZEBOX, WndClassEx.lpszClassName, 'Пустое окно', ws_OverlappedWindow, cw_UseDefault, cw_UseDefault, 240, 300, 0, 0, HInstance, nil); if hWnd = 0 then MessageBox (0, 'Window not created', 'Plain API', MB_OK) else begin ShowWindow (hWnd, sw_ShowNormal); while GetMessage (Msg, 0, 0, 0) do begin TranslateMessage (Msg); DispatchMessage (Msg); end; end; end; end;
begin WinMain; end.
|
объявляю длл и ф-ию:
| Код | prostoe4islo: function(n:integer):boolean; LibHandle: THandle;
|
подключаю длл через:
| Код | @Prostoe4islo := nil; LibHandle := LoadLibrary('MYDLL.DLL'); if LibHandle <= 32 then MessageBox (hWnd, 'Ошибка 1', 'ошибка', MB_OK); @Prostoe4islo := GetProcAddress(LibHandle,'Prostoe4islo'); if @Prostoe4islo <> nil then
|
обращаюсь к ф-ии
| Код | begin if Prostoe4islo(strtoint(s1)) then MessageBox (hWnd, 'Число простое!', 'Результат', MB_OK) else MessageBox (hWnd, 'Число НЕ простое!', 'Результат', MB_OK); end else MessageBox (hWnd, 'Ошибка 2', 'ошибка', MB_OK);
|
отключаю длл
| Код | FreeLibrary(LibHandle);
|
ошибок никаких не выдает, но ккаое бы число я не вписал то всегда ответ одинаковый: 'Число НЕ простое!' код проверки числа на простоту проверял создав обычный проект, числа распознает.
в чем здесь косяк?? |