r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Red] Red language group

Kaj
12-Feb-2012
[4900]
Hm, I'm not sure either if the wrapper function can currently be 
written like I said. You'd have to use the specific declaration as 
function! type for the function pointer, but the Red/System type 
checker may not currently support getting the function pointer in 
the right type
Pekr
12-Feb-2012
[4901]
Hmm, so in my case the situation in Red/System is even worse than 
possibly in R2 and World, where I could get such a handle from load/library 
directive, whereas in Red/System, what you describe, is writing completly 
separate layer. In fact, C's LoadLibrary is not difficult to handle, 
but still - a C level coding, which I thought is almost eliminated 
for wrapping purposes ...
Kaj
12-Feb-2012
[4902x4]
All in all, if it's possible to load the functions manually, it should 
also be possible to import them at load time with the regular #import 
method. So something else may be wrong in the binding
The Red/System situation is certainly not worse: it's the other way 
around. You can program manual loading with just a few well defined 
functions, the same that R2 and World use. But normally, you use 
automatic loading by the operating system, which is the standard 
and most efficient way to do it, and which R2 and World can't use 
because they execute bindings at run time
It's just that there is no ready binding for manual loading that, 
so you currently have to program it yourself
loading yet
Pekr
12-Feb-2012
[4906x2]
This is the code from LEDSet program:

bool LoadLedCtrl(void)
{
	g_hLedCtrlInst = LoadLibrary("LedCtrl.dll");
	if (!g_hLedCtrlInst)
	{
		g_hLedCtrlInst = LoadLibrary("..\\LedCtrl.dll");
		return false; 
	}
	//

 fpIsPower		= (LSN_ISPOWER)GetProcAddress(g_hLedCtrlInst, "LSN_IsPower");
So, if I would wrap LoadLibrary and GetProcAddress, I could get what 
you describe? :-)
Kaj
12-Feb-2012
[4908x3]
See, the library handle comes from the standard library loading function 
(well, Windows standard)
Yes, that would make your binding work the same way as the C code, 
so if that doesn't work, you definitely know the problem is elsewhere
Can you show your complete #import specification and the definition 
of a function that doesn't work?
Pekr
12-Feb-2012
[4911x2]
#import [

   "LedCtrl.dll" cdecl [

      led-is-power?: "LSN_IsPower" [
         return: [logic!]
      ]

      led-open-card: "LSN_OpenCard" [
          return: [logic!]
      ]  

      led-get-screen-number: "LSN_GetLedScrNumb" [
          return: [integer!]
      ]

      led-set-power: "LSN_Power" [
         power-on? [logic!]
         return: [logic!]
      ]

      led-get-brightness: "LSN_GetBright" [
         return: [integer!]
      ]
led-get-brightness is the first one to fail ...
Andreas
12-Feb-2012
[4913]
your lib uses stdcall, so better use stdcall instead of cdecl (though 
that's most likely not the culprit here)
Pekr
12-Feb-2012
[4914]
I used that, I changed it later to give it a try ...
Andreas
12-Feb-2012
[4915x2]
rest looks good to me, at first glance
just to be sure: in the code example from the LEDSet program you 
gave earlier, is there maybe some initialisation function called 
_after_ all the proc addrs have been retrieved?
Pekr
12-Feb-2012
[4917x2]
there is plenty of ASSERT(fpIsPower		!= NULL);, for each funcitons, 
then function LoadLedCtrl returns true ...
well, I am bothering you here more, than if I would upload somewhere 
a project file with all sources. The app is really small, but I might 
be missing some obvious setting or something else :-)
Kaj
12-Feb-2012
[4919]
An initialisation function is indeed a good candidate for the problem
Pekr
12-Feb-2012
[4920]
http://xidys.com/pekr/ledset-source.zip
Kaj
12-Feb-2012
[4921]
It's not that obvious, the C code does strange indirections :-)
Pekr
12-Feb-2012
[4922]
Kaj - so I defined following functions. It returns something :-) 
Hopefully I have an integer handle, representing the "address"? of 
the requested function. Now is the last step you described - how 
should I invoke it?

   "Kernel32.dll" stdcall [

      load-library: "LoadLibraryA" [
         name [c-string!]
         return: [integer!]
      ]
       
      get-proc-address: "GetProcAddress" [
         library-handle [integer!]
         function-name  [c-string!]
         return: [integer!] 
       ]

   ]

print ["load-library: " handle: load-library "LedCtrl.dll" lf]

print ["get-proc-address: " get-proc-address handle "LSN_IsPower" 
lf]
Kaj
12-Feb-2012
[4923x4]
led-power?: function [
	handler		[function! [return: [logic!]]]
	return:		[logic!]
][
	handler
]
led-power? as function! get-proc-address handle "LSN_IsPower"
Something like that, but I'm not sure the casting to function! type 
will be accepted
get-proc-address should really be defined as return: [function!] 
but that's not currently supported
Pekr
12-Feb-2012
[4927]
*** Compilation Error: invalid definition for function led-power?: 
[function! [return: [logic!]]]
Kaj
12-Feb-2012
[4928]
Ah, that's only supported in import functions, I think. That's why 
I handle them as integers everywhere, but then you can't call it 
as a function. So game over
Pekr
12-Feb-2012
[4929x2]
I will return to serial communication decoding, although that's not 
much fun :-)
Some Red/System type casting will not help? :-)
Kaj
12-Feb-2012
[4931]
No, because those casts aren't supported yet
Pekr
12-Feb-2012
[4932]
Anyway - it was a good way to learn a few bits in a practical manner 
:-)
Kaj
12-Feb-2012
[4933]
I can't find anything in the C++ code, though, that would necessitate 
manual loading. It looks like the regular #import should work. What 
errors are you getting exactly from those functions?
Pekr
12-Feb-2012
[4934]
*** Runtime Error 99: unknown error
*** at: 7572FC56h
Kaj
12-Feb-2012
[4935]
Doc may need to have a look at it
Pekr
12-Feb-2012
[4936]
R2 and World crash. In fact I think that those functions just try 
to write to serial port ...
Kaj
12-Feb-2012
[4937x2]
If R2 and World crash the same way, manual loading doesn't help
What happens when you call the same functions in C or C++?
Pekr
12-Feb-2012
[4939]
I haven't try. There are their Ex variants, imo related to dialog 
boxes. In fact I haven't tried anywhere in the source call to those 
funcs. So maybe those are called from dialog boxes.
Kaj
12-Feb-2012
[4940x3]
That would be the definitive test whether the problem is in the Red 
binding or not
LedSetDlg.cpp does:
fpSetLanguage(m_nLanguage);//set language  1= chinese english=3
Pekr
12-Feb-2012
[4943]
why would it be a proof?
Kaj
12-Feb-2012
[4944x3]
Maybe there's no default language so that's needed. Or the default 
is Chinese and you don't have Chinese support installed or something 
like that
You don't know if the C functions don't do the very same thing as 
the Red binding
m_nLanguage = 0; //for auto
Pekr
12-Feb-2012
[4947x2]
I wrapped a dialog box call, as DLL can cal e.g. LSN_BrightDlg, and 
it opens in English. When I explicitly call a language setting function, 
later on, set/get brightness still crashes ... I will see moving 
to the PC, where the sending card to LED screen actually is.
OK, enough for this weekend. Thanks for the assistance :-)
Kaj
12-Feb-2012
[4949]
If you haven't tested this on the machine with the hardware, anything 
is possible. That's why calling the C functions would have tested 
such things