Hello,

I'm used to using the unix standard library so I thought I could call a few of cygwin's functions and be all set. However it's not working and it could be because I'm doing something foolish (probably) or assuming that it should work when it will not in fact work at all, etc.

I tried to follow Iczelion's tutorial #17 exactly.

Could someone please take a look at this code? Thank you, if so.
Posted on 2001-12-12 14:09:33 by cmonahan
Sorry, I was failing to figure out how to get it in monospaced.

Here is the code although poorly spaced.

;
; trys to use the cygwin1.dll posix layer.
;
; Based on the UseDLL.asm sample from Iczelion's Win32 asm
; tutorial, number 17.
;

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib

.data
LibName db "cygwin1.dll",0
FunctionName db "printf",0
MallocName db "malloc",0 ; try function taking 1 arg
RandName db "rand",0 ; try function taking no args
DllNotFound db "Cannot load library",0
FunctionNotFound db " function not found",0
errStr db "error code: %d",0
buffer db 1000h dup (?)
crlf db 10,13,0
formStr db "I am a printf format string, %d\n",0
diagnost1 db "about to call malloc",10,13,0
diagnost2 db "done with call to malloc",10,13,0

.data?
hLib dd ? ; the handle of the DLL
printf dd ? ; the address of the external function
malloc dd ? ; ditto
rand dd ?
i DWORD ?

.code
start:
; ////////////////////////////////////////////////////
; // get the cygwin1.dll posix library
; ////////////////////////////////////////////////////
invoke LoadLibrary,addr LibName
.IF eax==NULL
invoke StdOut,ADDR DllNotFound
invoke StdOut,ADDR crlf
invoke ExitProcess,1
.ENDIF
mov hLib,eax

; ////////////////////////////////////////////////////
; // test by using printf
; ////////////////////////////////////////////////////
invoke GetProcAddress,hLib,ADDR FunctionName
.IF eax==NULL
invoke GetLastError
invoke wsprintf,addr buffer,addr errStr,eax
invoke StdOut,addr buffer
invoke StdOut,ADDR crlf
invoke StdOut,ADDR FunctionName
invoke StdOut,ADDR FunctionNotFound
invoke StdOut,ADDR crlf
invoke ExitProcess,1
.ENDIF
mov printf,eax
push i ; push 3 args in reverse (r to l) order
push offset formStr
push offset buffer
;call ; Executing this crashes the program

; ////////////////////////////////////////////////////
; // try malloc (uses only 1 argument)
; ////////////////////////////////////////////////////
invoke GetProcAddress,hLib,ADDR MallocName
.IF eax==NULL
invoke GetLastError
invoke wsprintf,addr buffer,addr errStr,eax
invoke StdOut,addr buffer
invoke StdOut,ADDR crlf
invoke StdOut,ADDR MallocName
invoke StdOut,ADDR FunctionNotFound
invoke StdOut,ADDR crlf
invoke ExitProcess,1
.ENDIF
mov malloc,eax
invoke StdOut,addr diagnost1 ; this gets output
xor eax,eax ; these four lines are trying in paranoid fashion
mov eax,5 ; to make sure to push 4 bytes...
push eax ;
xor eax,eax ;
;call ; Executing this crashes the program
mov i,eax
invoke StdOut,addr diagnost2 ; this is not output, if the malloc
; call is attempted.

; ////////////////////////////////////////////////////
; // try rand (no args)
; ////////////////////////////////////////////////////
invoke GetProcAddress,hLib,ADDR RandName
.IF eax==NULL
invoke GetLastError
invoke wsprintf,addr buffer,addr errStr,eax
invoke StdOut,addr buffer
invoke StdOut,addr crlf
invoke StdOut,addr RandName
invoke StdOut,addr FunctionNotFound
invoke StdOut,addr crlf
invoke ExitProcess,1
.ENDIF
mov rand,eax
call ; this one works.

invoke FreeLibrary,hLib
invoke ExitProcess,0
end start
Posted on 2001-12-12 14:10:50 by cmonahan
If I only had a brain, I could have done this in just one post.


;
; trys to use the cygwin1.dll posix layer.
;
; Based on the UseDLL.asm sample from Iczelion's Win32 asm
; tutorial, number 17.
;

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib

.data
LibName db "cygwin1.dll",0
FunctionName db "printf",0
MallocName db "malloc",0 ; try function taking 1 arg
RandName db "rand",0 ; try function taking no args
DllNotFound db "Cannot load library",0
FunctionNotFound db " function not found",0
errStr db "error code: %d",0
buffer db 1000h dup (?)
crlf db 10,13,0
formStr db "I am a printf format string, %d\n",0
diagnost1 db "about to call malloc",10,13,0
diagnost2 db "done with call to malloc",10,13,0

.data?
hLib dd ? ; the handle of the DLL
printf dd ? ; the address of the external function
malloc dd ? ; ditto
rand dd ?
i DWORD ?

.code
start:
; ////////////////////////////////////////////////////
; // get the cygwin1.dll posix library
; ////////////////////////////////////////////////////
invoke LoadLibrary,addr LibName
.IF eax==NULL
invoke StdOut,ADDR DllNotFound
invoke StdOut,ADDR crlf
invoke ExitProcess,1
.ENDIF
mov hLib,eax

; ////////////////////////////////////////////////////
; // test by using printf
; ////////////////////////////////////////////////////
invoke GetProcAddress,hLib,ADDR FunctionName
.IF eax==NULL
invoke GetLastError
invoke wsprintf,addr buffer,addr errStr,eax
invoke StdOut,addr buffer
invoke StdOut,ADDR crlf
invoke StdOut,ADDR FunctionName
invoke StdOut,ADDR FunctionNotFound
invoke StdOut,ADDR crlf
invoke ExitProcess,1
.ENDIF
mov printf,eax
push i ; push 3 args in reverse (r to l) order
push offset formStr
push offset buffer
;call ; Executing this crashes the program

; ////////////////////////////////////////////////////
; // try malloc (uses only 1 argument)
; ////////////////////////////////////////////////////
invoke GetProcAddress,hLib,ADDR MallocName
.IF eax==NULL
invoke GetLastError
invoke wsprintf,addr buffer,addr errStr,eax
invoke StdOut,addr buffer
invoke StdOut,ADDR crlf
invoke StdOut,ADDR MallocName
invoke StdOut,ADDR FunctionNotFound
invoke StdOut,ADDR crlf
invoke ExitProcess,1
.ENDIF
mov malloc,eax
invoke StdOut,addr diagnost1 ; this gets output
xor eax,eax ; these four lines are trying in paranoid fashion
mov eax,5 ; to make sure to push 4 bytes...
push eax ;
xor eax,eax ;
;call ; Executing this crashes the program
mov i,eax
invoke StdOut,addr diagnost2 ; this is not output, if the malloc
; call is attempted.

; ////////////////////////////////////////////////////
; // try rand (no args)
; ////////////////////////////////////////////////////
invoke GetProcAddress,hLib,ADDR RandName
.IF eax==NULL
invoke GetLastError
invoke wsprintf,addr buffer,addr errStr,eax
invoke StdOut,addr buffer
invoke StdOut,addr crlf
invoke StdOut,addr RandName
invoke StdOut,addr FunctionNotFound
invoke StdOut,addr crlf
invoke ExitProcess,1
.ENDIF
mov rand,eax
call ; this one works.

invoke FreeLibrary,hLib
invoke ExitProcess,0
end start
Posted on 2001-12-12 16:09:18 by cmonahan