How to make a C-extension using the C-API compatible with previous SU versions?

With ruby it is easy if you only use methods which exists in previous Sketchup versions your plugin works in that version also.
Now a are also using the C-api in a C extension. How does this work with previous SketchUp versions?

1 Like

@bugra - can you sanity check my assumption here?

I’d think you should be able to use the same build with older versions - but you’d have to be careful to only use functions that are present in the oldest version you are targeting. Then there’s the extra complication that filenames changed in SU2016 due to the introduction of the LayOut C API. (Basically, SLAPI got split into SketchUp C API and LayOut C API.)

Small caveat in that I’m not 100% sure what you can expect with this. I’ve not tried this myself.

This is tricky. What I would do is load the DLL dynamically. (On Windows you can use LoadLibrary). You’ll have to use the right DLL name based on SketchUp version (slapi.dll or SketchUpAPI.dll). You could try both since only one of them should be available.

Then I would load all the functions I need to use dynamically (using GetProcAddress) and store them in function pointers. If a function doesn’t exist (maybe because it’s an older version of SketchUp), then the pointer will be NULL so you can check it before calling it.

1 Like

Sorry to bring this up, as I am having the same problem.

In this case, I am making a C++ wrapper for the Sketchup C API. I need to know the version of Sketchup that is running at run time.

Are there any other suggestions that doesn’t involve DLLs (which I guess would not work on a Mac anyway)?

I found this function that looked promising, but I can’t even compile it.
SU_RESULT SUGetVersionStringUtf8 (size_t length, char * version )
http://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/sketchup__info_8h.html

Since you would need to know before you load a C extension why not check on the Ruby-Side:

Sketchup::version

You can afterward pass the value over to the C-side.


Tommy, it says on that page, clearly:

This is exported only by the SketchUp executable. It is not part of the standalone SDK.

So I think, you’d need to treat “sketchup.exe” as a library, and do what @bugra says above.

I’m not sure if this is correct, but here’s my thought.

In the Windows SDK there is a sketchup.lib file. It is an import library which just contains stub functions which call or point to the real functions. So I think linking to that file may be all you need. Of course SketchUp needs to be running in order to load the real functions.

1 Like

Following up in my last comment, you can use the lib or dumpobj from Visual Studio to inspect sketchup.lib if I remember from yesterday it does contain a stub for SUGetVersionStringUtf8

> lib /list sketchup.lib | more

From memory so…

1 Like

Yea, I wasn’t sure, but it does make sense that it is only available within SketchUp.

Would it be correct to think that this C-side function is related to that which is exposed via the ruiby method I mention above ?

The correct command is dumpbin /exports sketchup.lib which gets you these 3 functions. They do appear to reference Sketchup.exe and not some other .dll and so can only be used when SketchUp.exe is running.

     Exports

       ordinal    name

                  _SUGetEdition
                  _SUGetExtensionLicense
                  _SUGetVersionStringUtf8
1 Like

Useful to know. Hopefully I can find something similar for Mac.

It would be good to abstract it somehow in my c++ wrapper.

As of SU2016 you have SUGetVersionStringUtf8
http://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/sketchup__info_8h.html

Yes, I saw this, but I couldn’t work out how to use it, as I couldn’t compile a program with this function in it. I am not sure what to make of this line in the docs: “This is exported only by the SketchUp executable. It is not part of the standalone SDK.”
If it is not part of the SDK, then how do we use it?
[EDIT] this question has been asked above, but I haven’t been able to solve it still. I am using Xcode on Mac