flat assembler
Message board for the users of flat assembler.

Index > Windows > Problem mit DLL und CONSOLE_SCREEN_BUFFER_INFO

Author
Thread Post new topic Reply to topic
RedEagle



Joined: 07 Oct 2005
Posts: 8
RedEagle
Hi
Ich habe folgenden code:
Code:
format PE GUI 4.0 DLL
entry DllEntryPoint

include 'd:\fasm\include\win32a.inc'

section '.code' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE
        ret
endp

;void color(DWORD color);
 proc _color color
   ;local csbi:CONSOLE_SCREEN_BUFFER_INFO
   ;local hout:HANDLE
   csbi CONSOLE_SCREEN_BUFFER_INFO
   hout HANDLE

    invoke GetStdHandle,STD_OUTPUT_HANDLE
     mov [hout],eax
    invoke GetConsoleScreenBufferInfo,[hout],[csbi]
     mov eax,[csbi.wAttributes]
     or  eax,0xFF
     xor eax,[color]
     mov [csbi.wAttributes],eax
    invoke SetConsoleTextAttribute,[hout],[csbi]

   ;invoke LocalFree
 endp
;---

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL';,\
;          user,'USER32.DLL'

  import kernel,\
         GetStdHandle,'GetStdHandle',\
         GetConsoleScreenBufferInfo,'GetConsoleScreenBufferInfo',\
         SetConsoleTextAttribute,'SetConsoleTextAttribute'
;         LocalFree,'LocalFree'

;  import user,\


section '.edata' export data readable

  export 'FCF.DLL',\
         _color,'_color'

section '.reloc' fixups data discardable    


Es tritt beim Compilieren folgender Fehler auf:
illegal instruction
csbi CONSOLE_SCREEN_BUFFER_INFO


Was hab ich vergessen??
UND Wie kann ich jetzt auf in C/C++ auf diese Funktion zugreifen??
UND das 2. Byte von csbi.wAttributes, wof├╝r ist das Wichtig?? Kann man das missachten??
[/i]
Post 07 Oct 2005, 11:57
View user's profile Send private message Reply with quote
RedEagle



Joined: 07 Oct 2005
Posts: 8
RedEagle
This is the new code:

Code:
format PE GUI 4.0 DLL
entry DllEntryPoint

include 'd:\fasm\include\win32a.inc'
include 'fcf.inc'

section '.code' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE
        ret
endp

;void color(WORD color);
 proc _color color
   ;local csbi:CONSOLE_SCREEN_BUFFER_INFO
   ;local hout:HANDLE
   csbi CONSOLE_SCREEN_BUFFER_INFO
   hout dd ? ;HANDLE

    invoke GetStdHandle,STD_OUTPUT_HANDLE
     mov [hout],eax
    invoke GetConsoleScreenBufferInfo,[hout],[csbi]
     mov ax,[csbi.wAttributes]
     or  ax,0xFF
     xor ax,0xFF
     or  ax,WORD[color]
     mov [csbi.wAttributes],ax
    invoke SetConsoleTextAttribute,[hout],[csbi]

   ;invoke LocalFree
 endp
;---

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL';,\
;          user,'USER32.DLL'

  import kernel,\
         GetStdHandle,'GetStdHandle',\
         GetConsoleScreenBufferInfo,'GetConsoleScreenBufferInfo',\
         SetConsoleTextAttribute,'SetConsoleTextAttribute'
;         LocalFree,'LocalFree'

;  import user,\


section '.edata' export data readable

  export 'FCF.DLL',\
         _color,'_color'

section '.reloc' fixups data discardable      


fcf.inc:
Code:
struct CONSOLE_SCREEN_BUFFER_INFO
  dwSize              dd ?
  dwCursorPosition    dd ?
  wAttributes         dw ?
  srWindow            dd ?
  dwMaximumWindowSize dd ?
ends    


now, I can compile it, but how can I use it in my C/C++ programms??

I tryed it with "GetProcAddress" but it doesn't work:
Code:
#include <windows.h>
#include <stdio.h>

void Error(int point);

int main()
{
 HINSTANCE hdll = (HINSTANCE) LoadLibrary(TEXT(".\\fcf.dll"));
 if(hdll == NULL) Error(1);
 else
 { 
  FARPROC fproc = (FARPROC) GetProcAddress(hdll, TEXT("color"));
  if(fproc == NULL) Error(2);
 }
 
 system("PAUSE");
 return 0;
}

void Error(int point)
{
 printf("Fehler an Punkt %i: %i\n",point,GetLastError);
}    

I can load the DLL with LoadLibrary, but GetProcAddress returns NULL.
GetLastError returs 4215440. But there exists no Error with this number.


Last edited by RedEagle on 07 Oct 2005, 15:33; edited 1 time in total
Post 07 Oct 2005, 15:10
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
sorry, could you write in English?
Post 07 Oct 2005, 15:19
View user's profile Send private message Visit poster's website Reply with quote
RedEagle



Joined: 07 Oct 2005
Posts: 8
RedEagle
OK, now (I think) I can load the function:
Code:
#include <windows.h>
#include <stdio.h>

void Error(int point);
void (*color)(int);

int main()
{
 HINSTANCE hdll = (HINSTANCE) LoadLibrary(TEXT(".\\fcf.dll"));
 if(hdll == NULL) Error(1);
 else
 { 
  color = (void(*)(int)) GetProcAddress(hdll, TEXT("_color"));
  if(color == NULL) Error(2);
  else
  {
   color(0xcf); //Here it crashes
   printf("IT WORKS!!");
  }
 }
 
 system("PAUSE");
 return 0;
}    


But if I call the function, the programm crashes:

DLL_TEST caused an error by an invalid page in module FCF.DLL at 01b7:00c7100f. (translated by Google) Wink

What's worng??
Post 07 Oct 2005, 15:48
View user's profile Send private message Reply with quote
james



Joined: 07 Sep 2005
Posts: 45
Location: Australia
james
Try looking at the dll example in fasm examples called DLL.
Modify this as it works. Add your function and slowly take away the other code.
Post 08 Oct 2005, 00:03
View user's profile Send private message MSN Messenger Reply with quote
RedEagle



Joined: 07 Oct 2005
Posts: 8
RedEagle
Code:
format PE GUI 4.0 DLL
entry DllEntryPoint

include 'd:\fasm\include\win32a.inc'
include 'fcf.inc'

section '.code' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE
        ret
endp

;void color(WORD color);
 proc _color hWnd,wcolor

   local csbi:CONSOLE_SCREEN_BUFFER_INFO

    invoke GetConsoleScreenBufferInfo,[hWnd],[csbi]
     lea ax,[csbi.wAttributes]
     or  ax,0xFF
     xor ax,0xFF
     or  ax,WORD[wcolor]
     mov [csbi.wAttributes],ax
    invoke SetConsoleTextAttribute,[hWnd],[csbi]
    invoke LocalFree,[csbi]
   ret

 endp

;---

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel,\
         GetStdHandle,'GetStdHandle',\
         GetConsoleScreenBufferInfo,'GetConsoleScreenBufferInfo',\
         SetConsoleTextAttribute,'SetConsoleTextAttribute',\
         LocalFree,'LocalFree'

;  import user,\


section '.edata' export data readable

  export 'FCF.DLL',\
         _color,'_color'

section '.reloc' fixups data discardable    


I've taken the code of the Example DLL....
I've change some things, and now, I can load and use the function.
BUT I get two Bluescreens AND it's another color, and not the color, I want. AND the system crashs after a few secounds Sad
Post 08 Oct 2005, 07:07
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
What about calling convetion? Maybe your C code assumes that imported function is a C function?
Post 08 Oct 2005, 07:24
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
try using the cproc macro to define a function in your dll.
Post 08 Oct 2005, 09:12
View user's profile Send private message Reply with quote
RedEagle



Joined: 07 Oct 2005
Posts: 8
RedEagle
I don't find the cproc macro Sad
Where can I download the *.inc - file??
Post 08 Oct 2005, 10:00
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
in updated macro package proc syntax is different. Try:

Code:
proc _color c, hWnd,wcolor    
Post 08 Oct 2005, 13:04
View user's profile Send private message Visit poster's website Reply with quote
james



Joined: 07 Sep 2005
Posts: 45
Location: Australia
james
to test out if the bug is a calling convention issue, try using invoke and then try ccall.

also, you might try asking the same question in the masm forum as they concentrate on win32 more then this forum.
Post 08 Oct 2005, 22:44
View user's profile Send private message MSN Messenger Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
RedEagle:
Your right, sorry about that, it's not in the latest release. but It WAS in an earlier release. Tomasz you need to return this macro define with the current release. Here is the macro, don't know if it will work though.

Use like this:
cproc function arg1, arg2, arg3
mov eax, [arg1]
mov ecx, [arg2]
mov edx, [arg3]
cret 0
endp

and to call it:
cinvoke function, 1, 2, 3

Code:
macro cproc [params]                     ; define CDECL procedure
 { common
    local args,size,current
    match name arg, params
    \{ if used name
       name:
       virtual at ebp+8
       match =uses regs=,args, arg \\{ defargs@proc args
                                       regs@proc equ regs \\}
       match =,args, arg \\{ defargs@proc args \\}
       match =args@proc, args@proc \{ defargs@proc arg \\}
       args = $ - (ebp+8)
       end virtual \}
    match =args@proc, args@proc
    \{ if used params
       params:
       args = 0
       args@proc equ \}
    match =regs@proc, regs@proc
    \{ regs@proc equ \}
    all@vars equ
    current = 0
    if args | size
     push ebp
     mov ebp,esp
     if size
      sub esp,size
     end if
    end if
    match regs, regs@proc
    \{ push regs
       defpop@proc regs \}
    match , regs@proc
    \{ pop@proc equ \}
    macro locals
    \{ virtual at ebp-size+current
       macro label . \\{ deflocal@proc .,:, \\}
       struc db val \\{ deflocal@proc .,db,val \\}
       struc dw val \\{ deflocal@proc .,dw,val \\}
       struc dp val \\{ deflocal@proc .,dp,val \\}
       struc dd val \\{ deflocal@proc .,dd,val \\}
       struc dt val \\{ deflocal@proc .,dt,val \\}
       struc dq val \\{ deflocal@proc .,dq,val \\}
       struc rb cnt \\{ deflocal@proc .,rb cnt, \\}
       struc rw cnt \\{ deflocal@proc .,rw cnt, \\}
       struc rp cnt \\{ deflocal@proc .,rp cnt, \\}
       struc rd cnt \\{ deflocal@proc .,rd cnt, \\}
       struc rt cnt \\{ deflocal@proc .,rt cnt, \\}
       struc rq cnt \\{ deflocal@proc .,rq cnt, \\} \}
    macro endl
    \{ purge label
       restruc db,dw,dp,dd,dt,dq
       restruc rb,rw,rp,rd,rt,rq
       restruc byte,word,dword,pword,tword,qword
       current = $-(ebp-size)
       end virtual \}
    macro cret retval
    \{ pop@proc
       if ~ retval eq
         mov eax, retval
       end if
       if args | size
        leave
       end if
       retn \}
    macro finish@proc \{ size = (((current-1) shr 2)+1) shl 2
                         end if \}
}
    
Post 09 Oct 2005, 09:54
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.