функция из библиотеки не работает!!!

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by VintProg, 17 Jan 2012.

  1. VintProg

    VintProg New Member

    Joined:
    29 Oct 2011
    Messages:
    38
    Likes Received:
    0
    Reputations:
    0
    сама библиотека написан на delphi

    вот ее код

    library scr;



    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs,jpeg, StdCtrls;

    {$R *.res}

    procedure ScreenShotJPEG(const FileName : pchar); cdecl;
    var
    bmp : TBitmap;
    DC: HDC;
    Jpg: TJpegImage;

    Begin

    Try
    bmp:= TBitmap.Create;
    Jpg:= TJpegImage.Create;
    bmp.Height:=Screen.Height;
    bmp.Width:=Screen.Width;
    DC:=GetDC(0);
    bitblt(bmp.Canvas.Handle, 0, 0, Screen.Width, Screen.Height,
    DC, 0, 0, SRCCOPY);
    Jpg.Assign(Bmp);
    Jpg.SaveToFile(FileName);
    ReleaseDC(0, DC);
    finally
    Bmp.Free;
    jpg.Free;
    End;
    End;


    exports
    ScreenShotJPEG name 'ScreenShotJPEG';

    begin

    end.






    и я пытаюсь вызвать ее их MSVC 2010

    HMODULE hLib; //DLL
    typedef void (__cdecl *TScreenShotJPEG) (const char*);
    TScreenShotJPEG ScreenShotJPEG = NULL;


    и вот

    hLib = LoadLibrary(L"scr.dll");
    ScreenShotJPEG = (TScreenShotJPEG)GetProcAddress(hLib, "ScreenShotJPEG");

    и фик, проект от студии виснит, капетально!!!
     
  2. Kaimi

    Kaimi Well-Known Member

    Joined:
    23 Aug 2007
    Messages:
    1,732
    Likes Received:
    811
    Reputations:
    231
    Ну так выложи свой "висящий капетально" проект или хотя бы dll'ку, чтобы было что тестировать.

    В приведенном примере правда ты ничего не вызываешь, а просто адрес функции получаешь и определяешь её прототип.
     
    _________________________
  3. DooD

    DooD Elder - Старейшина

    Joined:
    30 Sep 2010
    Messages:
    1,168
    Likes Received:
    450
    Reputations:
    288
    DLL
    Code:
    library Project2;
    uses
    Windows,Graphics,jpeg;
    
    procedure snap; export;
    var
    bm:Tbitmap;
    dc,desk:HDC;
    jpg:TJPEGIMAGE;
    h,w:integer;
    Handle: HWND;
    begin
    bm:=tbitmap.create;
    jpg:=TJPEGIMAGE.Create;
    Handle:=GetDesktopWindow;
    desk:=GetDC(Handle);
    h:=GetDeviceCaps(desk, HORZRES);
    w:=GetDeviceCaps(desk, VERTRES);
    bm.Height:=w;
    bm.Width:=h;
    dc:=getdc(0);
    bitblt(bm.Canvas.Handle, 0, 0, h, w,DC, 0, 0, SRCCOPY);
    Jpg.Assign(bm);
    Jpg.SaveToFile('snapshot.jpg');
    ReleaseDC(0, DC);
    bm.Free;
    jpg.Free;
    end;
    exports
    snap;
    end.
    LOADER
    Code:
     
       .386
       .model flat,stdcall
       option casemap:none
       include include\windows.inc
       include include\user32.inc
       include include\kernel32.inc
    
       includelib lib\kernel32.lib
       includelib lib\user32.lib
    
       .data
       LibName db "Project2.dll",0
       FunctionName db "snap",0
       DllNotFound db "Cannot load library",0
       AppName db "Load Library",0
       FunctionNotFound db "function not found",0
    
       .data?
       hLib dd ?
       snap dd ?
    
       .code
       start:
    
     invoke LoadLibrary,addr LibName
    .if eax==NULL
      invoke MessageBox,NULL,addr DllNotFound,addr AppName,MB_OK
    .else
    
     mov hLib,eax
     invoke GetProcAddress,hLib,addr FunctionName
    
     .if eax==NULL
       invoke MessageBox,NULL,addr FunctionNotFound,addr AppName,MB_OK
       .else
          mov snap,eax
          call snap
    
         .endif
    
          invoke FreeLibrary,hLib
    
         .endif
    
          invoke ExitProcess,NULL
    
          end start
    все работает.
     
    #3 DooD, 17 Jan 2012
    Last edited: 17 Jan 2012