About Monkey 2 › Forums › Monkey 2 Programming Help › Using external libraries.
This topic contains 6 replies, has 5 voices, and was last updated by 
 abakobo
 1 year, 6 months ago.
- 
		AuthorPosts
 - 
		
			
				
September 22, 2017 at 9:23 am #10690
I have a library that I have tested and successfully used in C. The library has a function:
void swe_set_ephe_path(char *path)
I am importing the library where this function is stored into monkey2, but I am not sure about the extern declaration. I also am not sure on how to properly use the function in Monkey2. What could I be doing wrong?
[/crayon]Monkey1234567891011121314151617[crayon-5cba89fa79a6d418484895 inline="true" ]#Import "<mojo>"#Import "<std>"Using mojo..Using std..#Import "../../sweph/lib/libswe.a"#Import "../../sweph/src/swephexp.h"ExternFunction _swe_set_ephe_path:Void(path:Byte Ptr) 'This line compiles fine.PublicFunction Main()Print "This is the beginning of EphTest.monkey2..."_swe_set_ephe_path("../../ephemeris") 'When I try to use the function, I get this error. How would I convert?End FunctionSeptember 22, 2017 at 9:44 am #10691Hi, take a look at other modules that use c libs:
http://monkeycoder.co.nz/forums/topic/tinyaes-wrapper/
September 22, 2017 at 10:00 am #10692Can’t see anything obviously wrong, but also of course can’t test it. Might be worth trying to change path:Byte Ptr to path:CString, though I suspect they work out the same…
This was my earlier effort at importing an external function, though slightly different circumstances… yours looks pretty much the same setup, though:
http://monkeycoder.co.nz/forums/topic/simple-win32-api-call-attempt/
What was the error message?
September 22, 2017 at 10:02 am #10693Mx2 strings are not Cstrings. Checkout the toCString method.
September 22, 2017 at 11:30 am #10701Use the built-in CString type for extern char* type strings, eg:
Monkey123456789101112#Import “../../sweph/lib/libswe.a”#Import “../../sweph/src/swephexp.h”externfunction swe_set_ephe_path( path:CString )publicfunction Main()swe_set_ephe_path( "blah" )endSeptember 22, 2017 at 12:53 pm #10704Thank you all! I set the name of the function to the C++ equivalent (no underscore at the beginning), and changed the Byte Ptr to CString in the extern, and now it all works.
[/crayon]Monkey12345678910111213141516171819[crayon-5cba89fa856a9284684059 inline="true" ]#Import "<mojo>"#Import "<std>"Using mojo..Using std..#Import "../../sweph/lib/libswe.a"#Import "../../sweph/src/swephexp.h"ExternFunction swe_set_ephe_path:Void(path:CString)PublicFunction Main()Print "This is the beginning of EphTest.monkey2..."Local pth:String = "../../ephemeris"swe_set_ephe_path(pth)End FunctionSeptember 22, 2017 at 1:10 pm #10705Wow though I had to convert it myself!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.