Shared library interface to import 'C' functions? How?

Started by HPW, October 17, 2003, 03:59:10 AM

Previous topic - Next topic

HPW

On the homepage is this feature listed:



Shared library interface to import 'C' functions



How is it done. I surfed the doku, but could not find a related command.

Did I read it right, that I can call a functions in a DLL and get a return value?
Hans-Peter

HPW

#1
Finaly found the "import" command.

So I could try it out now.
Hans-Peter

Lutz

#2
Its fun, its powerful and it is dangerous (if you don't know what you are doing) ;-)



Have a look into odbc.lsp, which accesses the odbc DLLs from Windows to interface to databases, like MS Access files or Excel spreadsheets.



See also mysgl.lsp fo another 'import' example interfacing to LINUX libs.



(both files are only in the source distribution)



The whole 'import' stuff is much too underrated, but there are people doing wonderful stuff with this. A user from Arizona State University is using newLISP on a Mac to control music equipment and access related library functions.



Lutz

HPW

#3
I have a DLL which I have called from other enviroments:



So I test the following:



newLISP v7.2.3 Copyright (c) 2003 Lutz Mueller. All rights reserved.



> (import "DllTest.dll" "TestFunc")

TestFunc <3787A4>

> (TestFunc "Parameterstring from newlisp")





Then it hang up and want to sent the problem to MS.

It should return a new string concatenated with the parameterstring.



The API-calls from the docu works for me so far.
Hans-Peter

HPW

#4
Is the API not called with calling pascal convention?



My DLL uses CDECL, so may be thats the problem?
Hans-Peter

Lutz

#5
Your functions should be prefixed with 'CALLBACK' or some other thing and your compiler must be told to compile Windows DLL's. (yes, i think its Pscal calling conventions)



When you function returns a string pointer (which I have never seen in the Windows API) then your function would return a number in newLISP which is the address of the string. You would then use 'get-string' to access it.



Most of the time in the Windows API when a string is returned, you have to pass a string pointer with allocated memory, where windows can put the string into. I.e.



(set 'str "                    ")  ;; allocate memory buffer for str



(WinFunc str 20)



This is some imaginary windows function which would put a string of up to 20 characters into str. (don't have WIn32 API reference at hand for a real example)



Lutz