1. Noob, learning how to use dll's.

hi
i'm trying to get my program to adjust its self according to how much memory is
available and cpu usage. so far i plan to use kernel32.dll's
GlobalMemoryStatus/ex for memory calling, but i've never used any dlls.
Could anyone give me a simple run down pref with an example using euphoria?
and a dll for cpu callings?
i've visited msdn but i don't speak winapi.
i've learned so far that i have to open_dll the kernel and define_c_?
plz help, thanks in advance!

new topic     » topic index » view message » categorize

2. Re: Noob, learning how to use dll's.

See http://msdn.microsoft.com/en-us/library/aa366589(VS.85).aspx for
GlobalMemoryStatusEx.

DLL = open_dll("kernel.dll")
GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
mem = allocate(64)
result = c_func(GMSx,{mem})
free(mem)

I can't remember the exact size of DWORD and DWORDLONG, I think it's 4 and 8.
You'll have to peek(mem+offset,num_bytes) to get the information you want,
and multiply it out to convert it from a sequence of bytes to a number.

Hope this helps,
Mike

new topic     » goto parent     » topic index » view message » categorize

3. Re: Noob, learning how to use dll's.

Since we are on the subject of NOOB's and dll's, I herewith post a link to a
thread I started a few months ago about using a Euphoria DLL from within Excel:

http://www.openeuphoria.org/cgi-bin/esearch.exu?thread=1&fromMonth=C&fromYear=C&toMonth=2&toYear=D&keywords=%22Excel%22

Is there anybody reading this at this time that can extend the discussion a bit?
 Just to clarify, I'm not trying to:

1) write to an Excel sheet from a Euphoria program

2) create something that mimics an ActiveX object that therefore needs a
wrapper.

I'm interested in creating a DLL that exposes a function which can be utilized
within an Excel spreadsheet.

For example, say my DLL has a function which is called specialaverage and it
takes a series of potentially 10 numbers and calculates a "special average" (say,
something like the highest 3 consecutive out of 5, using a floor approach whereby
the next in the series can never be less than 20% beneath what its predecessor
is).  I want to put:

=specialaverage(a1..a10)

in the cell and have the result populated.

I have looked at the notes on MSDN on creating a DLL for use with excel
(referred to as an xll, I believe) but I'm not able to understand whether doing
so in Euphoria is even possible.

Thanks

Mike

new topic     » goto parent     » topic index » view message » categorize

4. Re: Noob, learning how to use dll's.

Mike777 wrote:
>
> I'm interested in creating a DLL that exposes a function which can be utilized
> within an Excel spreadsheet.  
> 
> For example, say my DLL has a function which is called specialaverage and it
> takes a series of potentially 10 numbers and calculates a "special average"
> (say, something like the highest 3 consecutive out of 5, using a floor 
> approach whereby the next in the series can never be less than 20% beneath
> what its predecessor is).  I want to put:
> 
> =specialaverage(a1..a10)
> 
> in the cell and have the result populated.
> 
> I have looked at the notes on MSDN on creating a DLL for use with excel
> (referred to as an xll, I believe) but I'm not able to understand whether
> doing so in Euphoria is even possible.

Do you have a live link for these notes?

Matt

new topic     » goto parent     » topic index » view message » categorize

5. Re: Noob, learning how to use dll's.

See http://msdn.microsoft.com/en-us/library/aa366589(VS.85).aspx for
GlobalMemoryStatusEx.

DLL = open_dll("kernel.dll")
GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
mem = allocate(64)
result = c_func(GMSx,{mem})
free(mem)

I can't remember the exact size of DWORD and DWORDLONG, I think it's 4 and 8.
You'll have to peek(mem+offset,num_bytes) to get the information you want,
and multiply it out to convert it from a sequence of bytes to a number.

Hope this helps,
Mike

Thanks for the help, exactally what I needed!
DWORD is 8 bytes so a DWORDLONG is also 8 but unsigned...right and why do you
ask?
P.S. I copied and pasted your message cause the quote didn't come out well.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Noob, learning how to use dll's.

Sorry, never mind, I finally found out why you asked about DWORD and DWORDLONG.

new topic     » goto parent     » topic index » view message » categorize

7. Re: Noob, learning how to use dll's.



without warning
include file.e
include dll.e
include safe.e

atom file1,DLL,GMSx,mem,result
object temp
file1 = open("D:\obscured\test.txt","w")

DLL = open_dll("kernel.dll")
GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
mem = allocate(64)
result = c_func(GMSx,{mem})   ----faults here
printf(file1,"%d",peek(mem+1))
free(mem)
close(file1)
abort(0)

I get:
C:\EUPHORIA\include\safe.e:516 in function original_c_func() 
c_proc/c_func: bad routine number (-1) 
    i = -1
    s = {832176}

... called from C:\EUPHORIA\include\safe.e:522 in function c_func()  
    i = -1
    s = {832176}
    r = <no value>

... called from D:\obscured\test.exw:13 

Do I have to use machine.e for this, I was using safe.e since I'm new to dll's.

new topic     » goto parent     » topic index » view message » categorize

8. Re: Noob, learning how to use dll's.

sorry, had it calling kernel.dll, forgot 32

new topic     » goto parent     » topic index » view message » categorize

9. Re: Noob, learning how to use dll's.

Archarios wrote:
> 
> }}}
<eucode>
> without warning
> include file.e
> include dll.e
> include safe.e
> 
> atom file1,DLL,GMSx,mem,result
> object temp
> file1 = open("D:\obscured\test.txt","w")
> 
> DLL = open_dll("kernel.dll")
> GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
> mem = allocate(64)
> result = c_func(GMSx,{mem})   ----faults here
> printf(file1,"%d",peek(mem+1))
> free(mem)
> close(file1)
> abort(0)
> </eucode>
{{{

> I get:
> C:\EUPHORIA\include\safe.e:516 in function original_c_func() 
> c_proc/c_func: bad routine number (-1) 
>     i = -1
>     s = {832176}
> 
> ... called from C:\EUPHORIA\include\safe.e:522 in function c_func()  
>     i = -1
>     s = {832176}
>     r = <no value>
> 
> ... called from D:\obscured\test.exw:13 
> 
> Do I have to use machine.e for this, I was using safe.e since I'm new to
> dll's.

Are you sure you need the Ex ?

CChris

new topic     » goto parent     » topic index » view message » categorize

10. Re: Noob, learning how to use dll's.

Not really.
I simply copied Mikes code to learn from.  I read that GlobalMemoryStatus is
known to give false results but thats what I seem to be getting with ex. Heres my
hex dump: 
12000000,68010000,730100001B000000,650100006C010000,6B01000073010000,6A0100009B000000,6A0100004D010000,7301000007000000,7301000073010000
I thought i was just having trouble due to having a dual core, but I'll try w/o
ex.

new topic     » goto parent     » topic index » view message » categorize

11. Re: Noob, learning how to use dll's.

Got similar results:
20000000,24000000,00000080FFDFA58,FFFFFFFFF00406EF8,0000FE7F00C0F67D,6A0100009B000000,6A0100004D010000,7301000007000000,7301000073010000
?,36% cpu load, 554048660879 total physical memory, 18446744069418807032
available
I don't know, am I reading the results wrong?
using printf("%d",peek()) using variations of peek and peek4u/s
my processor hardly goes above 15%, I have 4gb ram installed but only 2882028 is
shown, 1980160 available

new topic     » goto parent     » topic index » view message » categorize

12. Re: Noob, learning how to use dll's.

CChris wrote:
> 
> Archarios wrote:
> > 
> > }}}
<eucode>
> > without warning
> > include file.e
> > include dll.e
> > include safe.e
> > 
> > atom file1,DLL,GMSx,mem,result
> > object temp
> > file1 = open("D:\obscured\test.txt","w")
> > 
> > DLL = open_dll("kernel.dll")
> > GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
> > mem = allocate(64)
> > result = c_func(GMSx,{mem})   ----faults here
> > printf(file1,"%d",peek(mem+1))
> > free(mem)
> > close(file1)
> > abort(0)
> > </eucode>
{{{

> > I get:
> > C:\EUPHORIA\include\safe.e:516 in function original_c_func() 
> > c_proc/c_func: bad routine number (-1) 
> >     i = -1
> >     s = {832176}
> > 
> > ... called from C:\EUPHORIA\include\safe.e:522 in function c_func()  
> >     i = -1
> >     s = {832176}
> >     r = <no value>
> > 
> > ... called from D:\obscured\test.exw:13 
> > 
> > Do I have to use machine.e for this, I was using safe.e since I'm new to
> > dll's.
> 
> Are you sure you need the Ex ?

CChris:

Ex is correct. 

The structure being passed contains some DWORDLONG,s ( 64-bit int types ). 


mem = allocate(64) << is WRONG

mem = allocate(512) << CORRECT VALUE

}}}



Bernie

My files in archive: WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API

Can be downloaded here: http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan }}}

new topic     » goto parent     » topic index » view message » categorize

13. Re: Noob, learning how to use dll's.

Matt Lewis wrote:
> 
> Mike777 wrote:
> >
> > I'm interested in creating a DLL that exposes a function which can be
> > utilized
> > within an Excel spreadsheet.  
> > 
> > For example, say my DLL has a function which is called specialaverage and it
> > takes a series of potentially 10 numbers and calculates a "special average"
> > (say, something like the highest 3 consecutive out of 5, using a floor 
> > approach whereby the next in the series can never be less than 20% beneath
> > what its predecessor is).  I want to put:
> > 
> > =specialaverage(a1..a10)
> > 
> > in the cell and have the result populated.
> > 
> > I have looked at the notes on MSDN on creating a DLL for use with excel
> > (referred to as an xll, I believe) but I'm not able to understand whether
> > doing so in Euphoria is even possible.
> 
> Do you have a live link for these notes?

http://msdn.microsoft.com/en-us/library/bb687883.aspx

Mike

new topic     » goto parent     » topic index » view message » categorize

14. Re: Noob, learning how to use dll's.

Still stuck, nothing is reporting anything close to what I really have.  Gotta
go for now, I'll leave you with my code at the moment.
without warning
include file.e
include dll.e
include safe.e

atom file1,DLL,GMSx,mem,result
object temp
file1 = open("D:\\obscured\\test.txt","w")

DLL = open_dll("kernel32.dll")
GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
mem = allocate(512)
result = c_func(GMSx,{mem})
for count = mem+8 by 8 do
printf(file1,"%d\n",float64_to_atom(peek({count,8})))
end for
puts(file1,peek({mem,512}))
free(mem)
close(file1)
abort(0)


new topic     » goto parent     » topic index » view message » categorize

15. Re: Noob, learning how to use dll's.



without warning
include file.e
include dll.e
include safe.e

atom file1,DLL,GMSx,mem,result
object temp
file1 = open("D:\\obscured\\test.txt","w")

DLL = open_dll("kernel32.dll")
GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
mem = allocate(512)

-- dwLength: The size of the structure, in bytes.
-- You must set this member before calling GlobalMemoryStatusEx.
poke4(mem,512)

result = c_func(GMSx,{mem})
for count = mem+8 by 8 do
printf(file1,"%d\n",float64_to_atom(peek({count,8})))
end for


puts(file1,peek({mem,512}))
free(mem)
close(file1)
abort(0)


Bernie

My files in archive:
WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API 

Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan

new topic     » goto parent     » topic index » view message » categorize

16. Re: Noob, learning how to use dll's.

<snip>

unless you're wrapping this purely as an exercise, it's been done already

see http://www.rapideuphoria.com/memmon.zip

Yours, OtterDad

Don't sweat it -- it's not real life. It's only ones and zeroes. Gene Spafford

new topic     » goto parent     » topic index » view message » categorize

17. Re: Noob, learning how to use dll's.

Archarios wrote:
> 
> Still stuck, nothing is reporting anything close to what I really have.  Gotta
> go for now, I'll leave you with my code at the moment.
> }}}
<eucode>
> without warning
> include file.e
> include dll.e
> include safe.e
> 
> atom file1,DLL,GMSx,mem,result
> object temp
> file1 = open("D:\\obscured\\test.txt","w")
> 
> DLL = open_dll("kernel32.dll")
> GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
> mem = allocate(512)
> result = c_func(GMSx,{mem})
> for count = mem+8 by 8 do
> printf(file1,"%d\n",float64_to_atom(peek({count,8})))
> end for
> puts(file1,peek({mem,512}))
> free(mem)
> close(file1)
> abort(0)
> </eucode>
{{{


There are a number of problems here.
safe.e can not be included this way as it will cause name space errors. As
documented you must rename safe.e to machine.e and include machine.e. You would
also have to rename the original machine.e before doing this. safe.e is intended
for debugging only, not general use.

The size of the structure need not be larger than 64 bytes. You must also poke
this value into the first entry.

The structure entries are 64 bit integers, not floating point numbers.
float64_to_atom() can not be used here.

This revised program should work:

without warning
include file.e
include dll.e
include machine.e

atom file1,DLL,GMSx,mem,result
object temp
file1 = open("D:\\obscured\\test.txt","w")

DLL = open_dll("kernel32.dll")
GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
mem = allocate(64)
poke4(mem,64)
result = c_func(GMSx,{mem})
for count = mem+8 to mem+48 by 8 do
	printf(file1,"%d\n",peek4u(count)+peek4u(count+4)*#100000000)
end for
free(mem)
close(file1)
abort(0)


Larry Miller

new topic     » goto parent     » topic index » view message » categorize

18. Re: Noob, learning how to use dll's.

Program should be:

without warning
include file.e
include dll.e
include machine.e

atom file1,DLL,GMSx,mem,result
object temp
file1 = open("D:\\obscured\\test.txt","w")

DLL = open_dll("kernel32.dll")
GMSx = define_c_func(DLL,"GlobalMemoryStatusEx",{C_POINTER},C_INT)
mem = allocate(64)
poke4(mem,64)
result = c_func(GMSx,{mem})
for count = mem+8 to mem+56 by 8 do
	printf(file1,"%d\n",peek4u(count)+peek4u(count+4)*#100000000)
end for
free(mem)
close(file1)
abort(0)

The program in the prior post would miss the final structure entry.

Larry Miller

new topic     » goto parent     » topic index » view message » categorize

19. Re: Noob, learning how to use dll's.

I finally found the problem.  Before the last code I showed I had been
offsetting my offsets by 1(made sense at the time).  Funny that a little thing
can be such a big pain.  Everything is working fine now, thanks for all your
help, hope I wasn't too big a pain, I usually don't ask for so much help.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu